Skip to content

Commit d84166e

Browse files
committed
Small masked fix for structured scalars
1 parent 5d4c20d commit d84166e

2 files changed

Lines changed: 27 additions & 8 deletions

File tree

‎astropy/utils/masked/function_helpers.py‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,10 +1269,24 @@ def array_str(a, max_line_width=None, precision=None, suppress_small=None):
12691269
# Override to change special treatment of array scalars, since the numpy
12701270
# code turns the masked array scalar into a regular array scalar.
12711271
# By going through MaskedFormat, we can replace the string as needed.
1272-
if a.shape == () and a.dtype.names is None:
1273-
return MaskedFormat(_array_str_scalar)(a), None, None
1274-
else:
1275-
return array2string(a, max_line_width, precision, suppress_small, " ", "")
1272+
if a.shape == ():
1273+
if a.dtype.names is None:
1274+
return MaskedFormat(_array_str_scalar)(a), None, None
1275+
elif not NUMPY_LT_2_0:
1276+
from numpy._core.arrayprint import StructuredVoidFormat, _format_options
1277+
1278+
# Following numpy._core.arrayprint._void_scalar_to_string
1279+
options = _format_options.copy()
1280+
if options.get("formatter") is None:
1281+
options["formatter"] = {}
1282+
options["formatter"].setdefault("float_kind", str)
1283+
return (
1284+
MaskedFormat(StructuredVoidFormat.from_data(a, **options))(a),
1285+
None,
1286+
None,
1287+
)
1288+
1289+
return array2string(a, max_line_width, precision, suppress_small, " ", "")
12761290

12771291

12781292
# For the nanfunctions, we just treat any nan as an additional mask.

‎astropy/utils/masked/tests/test_masked.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from astropy import units as u
1515
from astropy.coordinates import Longitude
1616
from astropy.units import Quantity
17+
from astropy.utils.compat import NUMPY_LT_2_0
1718
from astropy.utils.compat.optional_deps import HAS_PLT
1819
from astropy.utils.masked import Masked, MaskedNDArray
1920

@@ -1395,12 +1396,16 @@ def test_masked_str_explicit_structured():
13951396
sa = np.array([(1.0, 2.0), (3.0, 4.0)], dtype="f8,f8")
13961397
msa = Masked(sa, [(False, True), (False, False)])
13971398
assert str(msa) == "[(1., ——) (3., 4.)]"
1398-
assert str(msa[0]) == "(1., ——)"
1399-
assert str(msa[1]) == "(3., 4.)" == str(sa[1])
1399+
assert str(msa[0]) == ("(1., ——)" if NUMPY_LT_2_0 else "(1.0, ———)")
1400+
assert str(msa[1]) == str(sa[1]) == ("(3., 4.)" if NUMPY_LT_2_0 else "(3.0, 4.0)")
14001401
with np.printoptions(precision=3, floatmode="fixed"):
14011402
assert str(msa) == "[(1.000, ———) (3.000, 4.000)]"
1402-
assert str(msa[0]) == "(1.000, ———)"
1403-
assert str(msa[1]) == "(3.000, 4.000)" == str(sa[1])
1403+
assert str(msa[0]) == ("(1.000, ———)" if NUMPY_LT_2_0 else "(1.0, ———)")
1404+
assert (
1405+
str(msa[1])
1406+
== str(sa[1])
1407+
== ("(3.000, 4.000)" if NUMPY_LT_2_0 else "(3.0, 4.0)")
1408+
)
14041409

14051410

14061411
def test_masked_repr_explicit_structured():

0 commit comments

Comments
 (0)