Skip to content

BUG fix where inconsistency between inplace=True and inplace=False #57083 #58576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9622,8 +9622,7 @@ def _where(
raise ValueError("Array conditional must be same shape as self")
cond = self._constructor(cond, **self._construct_axes_dict(), copy=False)

# make sure we are boolean
fill_value = bool(inplace)
fill_value = False
cond = cond.fillna(fill_value)
cond = cond.infer_objects()

Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/frame/indexing/test_where.py
Original file line number Diff line number Diff line change
Expand Up @@ -1064,3 +1064,23 @@ def test_where_inplace_no_other():
df.where(cond, inplace=True)
expected = DataFrame({"a": [1, np.nan], "b": [np.nan, "y"]})
tm.assert_frame_equal(df, expected)


def test_where_consistent_inplace():
# GH#57083
s = Series(range(5))
t = Series([True, False])
expected_where = Series([0, 99, 99, 99, 99])
# where
w = s.where(t, 99)
tm.assert_series_equal(w, expected_where)
w_inplace = s.copy()
w_inplace.where(t, 99, inplace=True)
tm.assert_series_equal(w_inplace, expected_where)
# mask
expected_mask = Series([99, 1, 99, 99, 99])
m = s.mask(t, 99)
tm.assert_series_equal(m, expected_mask)
m_inplace = s.copy()
m_inplace.mask(t, 99, inplace=True)
tm.assert_series_equal(m_inplace, expected_mask)
Loading