Skip to content

Commit 7389f03

Browse files
authored
BUG: mask/where raising for mixed dtype frame and no other (#51689)
1 parent ff9c8f4 commit 7389f03

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
@@ -6122,8 +6122,8 @@ def _is_mixed_type(self) -> bool_t:
61226122
def _check_inplace_setting(self, value) -> bool_t:
61236123
"""check whether we allow in-place setting with this type of value"""
61246124
if self._is_mixed_type and not self._mgr.is_numeric_mixed_type:
6125-
# allow an actual np.nan thru
6126-
if is_float(value) and np.isnan(value):
6125+
# allow an actual np.nan through
6126+
if is_float(value) and np.isnan(value) or value is lib.no_default:
61276127
return True
61286128

61296129
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
@@ -1024,3 +1024,12 @@ def test_where_int_overflow(replacement):
10241024
expected = DataFrame([[1.0, 2e25, "nine"], [replacement, 0.1, replacement]])
10251025

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

0 commit comments

Comments
 (0)