Skip to content

Commit 719c9e2

Browse files
phoflmeeseeksmachine
authored andcommitted
Backport PR pandas-dev#51689: BUG: mask/where raising for mixed dtype frame and no other
1 parent 463c5a9 commit 719c9e2

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

pandas/core/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6069,8 +6069,8 @@ def _is_mixed_type(self) -> bool_t:
60696069
def _check_inplace_setting(self, value) -> bool_t:
60706070
"""check whether we allow in-place setting with this type of value"""
60716071
if self._is_mixed_type and not self._mgr.is_numeric_mixed_type:
6072-
# allow an actual np.nan thru
6073-
if is_float(value) and np.isnan(value):
6072+
# allow an actual np.nan through
6073+
if is_float(value) and np.isnan(value) or value is lib.no_default:
60746074
return True
60756075

60766076
raise TypeError(

pandas/tests/frame/indexing/test_mask.py

+9
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,12 @@ def test_mask_return_dtype():
141141
excepted = Series([1.0, 0.0, 1.0, 0.0], dtype=ser.dtype)
142142
result = ser.mask(cond, other)
143143
tm.assert_series_equal(result, excepted)
144+
145+
146+
def test_mask_inplace_no_other():
147+
# GH#51685
148+
df = DataFrame({"a": [1, 2], "b": ["x", "y"]})
149+
cond = DataFrame({"a": [True, False], "b": [False, True]})
150+
df.mask(cond, inplace=True)
151+
expected = DataFrame({"a": [np.nan, 2], "b": ["x", np.nan]})
152+
tm.assert_frame_equal(df, expected)

pandas/tests/frame/indexing/test_where.py

+9
Original file line numberDiff line numberDiff line change
@@ -1025,3 +1025,12 @@ def test_where_int_overflow(replacement):
10251025
expected = DataFrame([[1.0, 2e25, "nine"], [replacement, 0.1, replacement]])
10261026

10271027
tm.assert_frame_equal(result, expected)
1028+
1029+
1030+
def test_where_inplace_no_other():
1031+
# GH#51685
1032+
df = DataFrame({"a": [1, 2], "b": ["x", "y"]})
1033+
cond = DataFrame({"a": [True, False], "b": [False, True]})
1034+
df.where(cond, inplace=True)
1035+
expected = DataFrame({"a": [1, np.nan], "b": [np.nan, "y"]})
1036+
tm.assert_frame_equal(df, expected)

0 commit comments

Comments
 (0)