Skip to content

Commit

Permalink
Backport PR #51689 on branch 2.0.x (BUG: mask/where raising for mixed…
Browse files Browse the repository at this point in the history
… dtype frame and no other) (#51848)

Backport PR #51689: BUG: mask/where raising for mixed dtype frame and no other

Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and phofl committed Mar 8, 2023
1 parent a5019e0 commit cd69cfd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6078,8 +6078,8 @@ def _is_mixed_type(self) -> bool_t:
def _check_inplace_setting(self, value) -> bool_t:
"""check whether we allow in-place setting with this type of value"""
if self._is_mixed_type and not self._mgr.is_numeric_mixed_type:
# allow an actual np.nan thru
if is_float(value) and np.isnan(value):
# allow an actual np.nan through
if is_float(value) and np.isnan(value) or value is lib.no_default:
return True

raise TypeError(
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/frame/indexing/test_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,12 @@ def test_mask_return_dtype():
excepted = Series([1.0, 0.0, 1.0, 0.0], dtype=ser.dtype)
result = ser.mask(cond, other)
tm.assert_series_equal(result, excepted)


def test_mask_inplace_no_other():
# GH#51685
df = DataFrame({"a": [1, 2], "b": ["x", "y"]})
cond = DataFrame({"a": [True, False], "b": [False, True]})
df.mask(cond, inplace=True)
expected = DataFrame({"a": [np.nan, 2], "b": ["x", np.nan]})
tm.assert_frame_equal(df, expected)
9 changes: 9 additions & 0 deletions pandas/tests/frame/indexing/test_where.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,3 +1025,12 @@ def test_where_int_overflow(replacement):
expected = DataFrame([[1.0, 2e25, "nine"], [replacement, 0.1, replacement]])

tm.assert_frame_equal(result, expected)


def test_where_inplace_no_other():
# GH#51685
df = DataFrame({"a": [1, 2], "b": ["x", "y"]})
cond = DataFrame({"a": [True, False], "b": [False, True]})
df.where(cond, inplace=True)
expected = DataFrame({"a": [1, np.nan], "b": [np.nan, "y"]})
tm.assert_frame_equal(df, expected)

0 comments on commit cd69cfd

Please sign in to comment.