Skip to content

Commit 93297e0

Browse files
authored
BUG: Series.where with IntervalDtype when no-op (#44585)
1 parent 2b53ea8 commit 93297e0

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ Strings
593593
Interval
594594
^^^^^^^^
595595
- 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`)
596+
- Bug in :meth:`Series.where` with ``IntervalDtype`` incorrectly raising when the ``where`` call should not replace anything (:issue:`44181`)
596597
-
597598

598599
Indexing

pandas/core/internals/blocks.py

+4
Original file line numberDiff line numberDiff line change
@@ -1634,6 +1634,10 @@ def where(self, other, cond) -> list[Block]:
16341634
# attribute "na_value"
16351635
other = self.dtype.na_value # type: ignore[union-attr]
16361636

1637+
icond, noop = validate_putmask(self.values, ~cond)
1638+
if noop:
1639+
return self.copy()
1640+
16371641
try:
16381642
result = self.values._where(cond, other)
16391643
except TypeError:

pandas/tests/frame/indexing/test_where.py

+10
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,16 @@ def test_where_ea_other(self):
688688
result = df.where(mask, ser2, axis=1)
689689
tm.assert_frame_equal(result, expected)
690690

691+
def test_where_interval_noop(self):
692+
# GH#44181
693+
df = DataFrame([pd.Interval(0, 0)])
694+
res = df.where(df.notna())
695+
tm.assert_frame_equal(res, df)
696+
697+
ser = df[0]
698+
res = ser.where(ser.notna())
699+
tm.assert_series_equal(res, ser)
700+
691701

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

0 commit comments

Comments
 (0)