summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--numpy/ma/core.py15
-rw-r--r--numpy/ma/tests/test_core.py15
2 files changed, 30 insertions, 0 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py
index e24cb956c..5c446252b 100644
--- a/numpy/ma/core.py
+++ b/numpy/ma/core.py
@@ -6385,6 +6385,21 @@ class MaskedConstant(MaskedArray):
# it's a subclass, or something is wrong, make it obvious
return object.__repr__(self)
+ def __format__(self, format_spec):
+ # Replace ndarray.__format__ with the default, which supports no format characters.
+ # Supporting format characters is unwise here, because we do not know what type
+ # the user was expecting - better to not guess.
+ try:
+ return object.__format__(self, format_spec)
+ except TypeError:
+ # 2020-03-23, NumPy 1.19.0
+ warnings.warn(
+ "Format strings passed to MaskedConstant are ignored, but in future may "
+ "error or produce different behavior",
+ FutureWarning, stacklevel=2
+ )
+ return object.__format__(self, "")
+
def __reduce__(self):
"""Override of MaskedArray's __reduce__.
"""
diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py
index 3bfb42555..98fc7dd97 100644
--- a/numpy/ma/tests/test_core.py
+++ b/numpy/ma/tests/test_core.py
@@ -447,6 +447,21 @@ class TestMaskedArray:
assert_equal(copied.mask, [0, 0, 0])
assert_equal(a.mask, [0, 1, 0])
+ def test_format(self):
+ a = array([0, 1, 2], mask=[False, True, False])
+ assert_equal(format(a), "[0 -- 2]")
+ assert_equal(format(masked), "--")
+ assert_equal(format(masked, ""), "--")
+
+ # Postponed from PR #15410, perhaps address in the future.
+ # assert_equal(format(masked, " >5"), " --")
+ # assert_equal(format(masked, " <5"), "-- ")
+
+ # Expect a FutureWarning for using format_spec with MaskedElement
+ with assert_warns(FutureWarning):
+ with_format_string = format(masked, " >5")
+ assert_equal(with_format_string, "--")
+
def test_str_repr(self):
a = array([0, 1, 2], mask=[False, True, False])
assert_equal(str(a), '[0 -- 2]')