Skip to content

Commit 7839305

Browse files
authored
DEPR: DataFrame(MaskedRecords) (#40363)
1 parent 79d7159 commit 7839305

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ Deprecations
369369
- Deprecated casting ``datetime.date`` objects to ``datetime64`` when used as ``fill_value`` in :meth:`DataFrame.unstack`, :meth:`DataFrame.shift`, :meth:`Series.shift`, and :meth:`DataFrame.reindex`, pass ``pd.Timestamp(dateobj)`` instead (:issue:`39767`)
370370
- Deprecated :meth:`.Styler.set_na_rep` and :meth:`.Styler.set_precision` in favour of :meth:`.Styler.format` with ``na_rep`` and ``precision`` as existing and new input arguments respectively (:issue:`40134`, :issue:`40425`)
371371
- Deprecated allowing partial failure in :meth:`Series.transform` and :meth:`DataFrame.transform` when ``func`` is list-like or dict-like; will raise if any function fails on a column in a future version (:issue:`40211`)
372+
- Deprecated support for ``np.ma.mrecords.MaskedRecords`` in the :class:`DataFrame` constructor, pass ``{name: data[name] for name in data.dtype.names}`` instead (:issue:`40363`)
372373

373374
.. ---------------------------------------------------------------------------
374375

pandas/core/frame.py

+7
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,13 @@ def __init__(
599599
copy,
600600
typ=manager,
601601
)
602+
warnings.warn(
603+
"Support for MaskedRecords is deprecated and will be "
604+
"removed in a future version. Pass "
605+
"{name: data[name] for name in data.dtype.names} instead.",
606+
FutureWarning,
607+
stacklevel=2,
608+
)
602609

603610
# a masked array
604611
else:

pandas/tests/frame/test_constructors.py

+19-4
Original file line numberDiff line numberDiff line change
@@ -993,10 +993,19 @@ def test_constructor_maskedrecarray_dtype(self):
993993
np.ma.zeros(5, dtype=[("date", "<f8"), ("price", "<f8")]), mask=[False] * 5
994994
)
995995
data = data.view(mrecords.mrecarray)
996-
result = DataFrame(data, dtype=int)
996+
997+
with tm.assert_produces_warning(FutureWarning):
998+
# Support for MaskedRecords deprecated
999+
result = DataFrame(data, dtype=int)
1000+
9971001
expected = DataFrame(np.zeros((5, 2), dtype=int), columns=["date", "price"])
9981002
tm.assert_frame_equal(result, expected)
9991003

1004+
# GH#40363 check that the alternative suggested in the deprecation
1005+
# warning behaves as expected
1006+
alt = DataFrame({name: data[name] for name in data.dtype.names}, dtype=int)
1007+
tm.assert_frame_equal(result, alt)
1008+
10001009
def test_constructor_mrecarray(self):
10011010
# Ensure mrecarray produces frame identical to dict of masked arrays
10021011
# from GH3479
@@ -1024,18 +1033,24 @@ def test_constructor_mrecarray(self):
10241033
# fill the comb
10251034
comb = {k: (v.filled() if hasattr(v, "filled") else v) for k, v in comb}
10261035

1036+
with tm.assert_produces_warning(FutureWarning):
1037+
# Support for MaskedRecords deprecated
1038+
result = DataFrame(mrecs)
10271039
expected = DataFrame(comb, columns=names)
1028-
result = DataFrame(mrecs)
10291040
assert_fr_equal(result, expected)
10301041

10311042
# specify columns
1043+
with tm.assert_produces_warning(FutureWarning):
1044+
# Support for MaskedRecords deprecated
1045+
result = DataFrame(mrecs, columns=names[::-1])
10321046
expected = DataFrame(comb, columns=names[::-1])
1033-
result = DataFrame(mrecs, columns=names[::-1])
10341047
assert_fr_equal(result, expected)
10351048

10361049
# specify index
1050+
with tm.assert_produces_warning(FutureWarning):
1051+
# Support for MaskedRecords deprecated
1052+
result = DataFrame(mrecs, index=[1, 2])
10371053
expected = DataFrame(comb, columns=names, index=[1, 2])
1038-
result = DataFrame(mrecs, index=[1, 2])
10391054
assert_fr_equal(result, expected)
10401055

10411056
def test_constructor_corner_shape(self):

0 commit comments

Comments
 (0)