Skip to content

BUG: Series.where with IntervalDtype when no-op #44585

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

Merged
merged 2 commits into from
Nov 25, 2021
Merged
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ Strings
Interval
^^^^^^^^
- Bug in :meth:`IntervalIndex.get_indexer_non_unique` returning boolean mask instead of array of integers for a non unique and non monotonic index (:issue:`44084`)
- Bug in :meth:`Series.where` with ``IntervalDtype`` incorrectly raising when the ``where`` call should not replace anything (:issue:`44181`)
-

Indexing
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1632,6 +1632,10 @@ def where(self, other, cond) -> list[Block]:
# attribute "na_value"
other = self.dtype.na_value # type: ignore[union-attr]

icond, noop = validate_putmask(self.values, ~cond)
if noop:
return self.copy()

try:
result = self.values._where(cond, other)
except TypeError:
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/frame/indexing/test_where.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,16 @@ def test_where_ea_other(self):
result = df.where(mask, ser2, axis=1)
tm.assert_frame_equal(result, expected)

def test_where_interval_noop(self):
# GH#44181
df = DataFrame([pd.Interval(0, 0)])
res = df.where(df.notna())
tm.assert_frame_equal(res, df)

ser = df[0]
res = ser.where(ser.notna())
tm.assert_series_equal(res, ser)


def test_where_try_cast_deprecated(frame_or_series):
obj = DataFrame(np.random.randn(4, 3))
Expand Down