From d443137704fc6f144c8f59531e542ba90c25caa6 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 22 Nov 2021 19:00:41 -0800 Subject: [PATCH 1/2] BUG: Series.where with IntervalDtype when no-op --- pandas/core/internals/blocks.py | 4 ++++ pandas/tests/frame/indexing/test_where.py | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index a6fa2a9e3b2c1..76bea5ca61266 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -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: diff --git a/pandas/tests/frame/indexing/test_where.py b/pandas/tests/frame/indexing/test_where.py index b675e9d703f44..0906186418c0a 100644 --- a/pandas/tests/frame/indexing/test_where.py +++ b/pandas/tests/frame/indexing/test_where.py @@ -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)) From 7ec3ac69432510e7b0db97536f2cffa7402a4634 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 22 Nov 2021 19:02:26 -0800 Subject: [PATCH 2/2] whatsnew --- doc/source/whatsnew/v1.4.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 1f656f267783f..698a43acb4e37 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -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