Skip to content

Commit cd69cfd

Browse files
Backport PR pandas-dev#51689 on branch 2.0.x (BUG: mask/where raising for mixed dtype frame and no other) (pandas-dev#51848)
Backport PR pandas-dev#51689: BUG: mask/where raising for mixed dtype frame and no other Co-authored-by: Patrick Hoefler <[email protected]>
1 parent a5019e0 commit cd69cfd

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
@@ -6078,8 +6078,8 @@ def _is_mixed_type(self) -> bool_t:
60786078
def _check_inplace_setting(self, value) -> bool_t:
60796079
"""check whether we allow in-place setting with this type of value"""
60806080
if self._is_mixed_type and not self._mgr.is_numeric_mixed_type:
6081-
# allow an actual np.nan thru
6082-
if is_float(value) and np.isnan(value):
6081+
# allow an actual np.nan through
6082+
if is_float(value) and np.isnan(value) or value is lib.no_default:
60836083
return True
60846084

60856085
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)