Skip to content

Commit a49be7f

Browse files
mroeschkejreback
authored andcommitted
BUG: DataFrame respects dtype with masked recarray (#24874)
1 parent 12bb6d0 commit a49be7f

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

doc/source/whatsnew/v0.24.0.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -1692,8 +1692,8 @@ Missing
16921692
- Bug in :func:`Series.hasnans` that could be incorrectly cached and return incorrect answers if null elements are introduced after an initial call (:issue:`19700`)
16931693
- :func:`Series.isin` now treats all NaN-floats as equal also for ``np.object``-dtype. This behavior is consistent with the behavior for float64 (:issue:`22119`)
16941694
- :func:`unique` no longer mangles NaN-floats and the ``NaT``-object for ``np.object``-dtype, i.e. ``NaT`` is no longer coerced to a NaN-value and is treated as a different entity. (:issue:`22295`)
1695-
- :func:`DataFrame` and :func:`Series` now properly handle numpy masked arrays with hardened masks. Previously, constructing a DataFrame or Series from a masked array with a hard mask would create a pandas object containing the underlying value, rather than the expected NaN. (:issue:`24574`)
1696-
1695+
- :class:`DataFrame` and :class:`Series` now properly handle numpy masked arrays with hardened masks. Previously, constructing a DataFrame or Series from a masked array with a hard mask would create a pandas object containing the underlying value, rather than the expected NaN. (:issue:`24574`)
1696+
- Bug in :class:`DataFrame` constructor where ``dtype`` argument was not honored when handling numpy masked record arrays. (:issue:`24874`)
16971697

16981698
MultiIndex
16991699
^^^^^^^^^^

pandas/core/internals/construction.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def masked_rec_array_to_mgr(data, index, columns, dtype, copy):
9393
if columns is None:
9494
columns = arr_columns
9595

96-
mgr = arrays_to_mgr(arrays, arr_columns, index, columns)
96+
mgr = arrays_to_mgr(arrays, arr_columns, index, columns, dtype)
9797

9898
if copy:
9999
mgr = mgr.copy()

pandas/tests/frame/test_constructors.py

+11
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,17 @@ def test_constructor_maskedarray_hardened(self):
787787
dtype=float)
788788
tm.assert_frame_equal(result, expected)
789789

790+
def test_constructor_maskedrecarray_dtype(self):
791+
# Ensure constructor honors dtype
792+
data = np.ma.array(
793+
np.ma.zeros(5, dtype=[('date', '<f8'), ('price', '<f8')]),
794+
mask=[False] * 5)
795+
data = data.view(ma.mrecords.mrecarray)
796+
result = pd.DataFrame(data, dtype=int)
797+
expected = pd.DataFrame(np.zeros((5, 2), dtype=int),
798+
columns=['date', 'price'])
799+
tm.assert_frame_equal(result, expected)
800+
790801
def test_constructor_mrecarray(self):
791802
# Ensure mrecarray produces frame identical to dict of masked arrays
792803
# from GH3479

0 commit comments

Comments
 (0)