From cdf0fce83eedc6ec945286d1c0b9f5e54a7479d7 Mon Sep 17 00:00:00 2001 From: phofl Date: Fri, 27 Aug 2021 16:23:00 +0200 Subject: [PATCH 1/3] Fix promote issue in range putmask --- pandas/core/indexes/base.py | 3 +-- pandas/tests/indexes/ranges/test_setops.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 8c32ccc1fa74c..fa838871f999f 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -4890,8 +4890,7 @@ def putmask(self, mask, value) -> Index: values, mask.sum(), converted # type: ignore[arg-type] ) np.putmask(values, mask, converted) - - return type(self)._simple_new(values, name=self.name) + return self._shallow_copy(values) def equals(self, other: Any) -> bool: """ diff --git a/pandas/tests/indexes/ranges/test_setops.py b/pandas/tests/indexes/ranges/test_setops.py index ba938f82e9d89..eae8bbbfae521 100644 --- a/pandas/tests/indexes/ranges/test_setops.py +++ b/pandas/tests/indexes/ranges/test_setops.py @@ -354,3 +354,17 @@ def test_symmetric_difference(self): result = left.symmetric_difference(right[1:]) expected = Int64Index([1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14]) tm.assert_index_equal(result, expected) + + def test_putmask_range_cast(self): + # GH#43240 + idx = RangeIndex(0, 5) + result = idx.putmask(np.array([True, True, False, False, False]), 10) + expected = Index([10, 10, 2, 3, 4]) + tm.assert_index_equal(result, expected) + + def test_where_range_cast(self): + # GH#43240 + idx = RangeIndex(0, 5) + result = idx.where(np.array([False, False, True, True, True]), 10) + expected = Index([10, 10, 2, 3, 4]) + tm.assert_index_equal(result, expected) From 79c9f309593d8be98041b0498ee3c88a4dde3c47 Mon Sep 17 00:00:00 2001 From: phofl Date: Fri, 27 Aug 2021 16:28:37 +0200 Subject: [PATCH 2/3] Improve test and add whatsnew --- doc/source/whatsnew/v1.3.3.rst | 2 +- pandas/tests/indexes/ranges/test_setops.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v1.3.3.rst b/doc/source/whatsnew/v1.3.3.rst index 3dee3aa5e7c7a..5df75317c389f 100644 --- a/doc/source/whatsnew/v1.3.3.rst +++ b/doc/source/whatsnew/v1.3.3.rst @@ -17,7 +17,7 @@ Fixed regressions - Fixed regression in :class:`DataFrame` constructor failing to broadcast for defined :class:`Index` and len one list of :class:`Timestamp` (:issue:`42810`) - Performance regression in :meth:`core.window.ewm.ExponentialMovingWindow.mean` (:issue:`42333`) - Fixed regression in :meth:`.GroupBy.agg` incorrectly raising in some cases (:issue:`42390`) -- +- Fixed regression in :meth:`RangeIndex.where` and :meth:`RangeIndex.putmask` raising ``AssertionError`` when result did not represent a :class:`RangeIndex` (:issue:`43244`) .. --------------------------------------------------------------------------- diff --git a/pandas/tests/indexes/ranges/test_setops.py b/pandas/tests/indexes/ranges/test_setops.py index eae8bbbfae521..210bcd300b1b0 100644 --- a/pandas/tests/indexes/ranges/test_setops.py +++ b/pandas/tests/indexes/ranges/test_setops.py @@ -357,14 +357,14 @@ def test_symmetric_difference(self): def test_putmask_range_cast(self): # GH#43240 - idx = RangeIndex(0, 5) + idx = RangeIndex(0, 5, name="test") result = idx.putmask(np.array([True, True, False, False, False]), 10) - expected = Index([10, 10, 2, 3, 4]) + expected = Index([10, 10, 2, 3, 4], name="test") tm.assert_index_equal(result, expected) def test_where_range_cast(self): # GH#43240 - idx = RangeIndex(0, 5) + idx = RangeIndex(0, 5, name="test") result = idx.where(np.array([False, False, True, True, True]), 10) - expected = Index([10, 10, 2, 3, 4]) + expected = Index([10, 10, 2, 3, 4], name="test") tm.assert_index_equal(result, expected) From add3a7870e4f88002291783d9dfab0eccdd5fc9b Mon Sep 17 00:00:00 2001 From: phofl Date: Fri, 27 Aug 2021 16:29:24 +0200 Subject: [PATCH 3/3] Change issue reference --- doc/source/whatsnew/v1.3.3.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.3.3.rst b/doc/source/whatsnew/v1.3.3.rst index 5df75317c389f..9aac0a9ad9681 100644 --- a/doc/source/whatsnew/v1.3.3.rst +++ b/doc/source/whatsnew/v1.3.3.rst @@ -17,7 +17,7 @@ Fixed regressions - Fixed regression in :class:`DataFrame` constructor failing to broadcast for defined :class:`Index` and len one list of :class:`Timestamp` (:issue:`42810`) - Performance regression in :meth:`core.window.ewm.ExponentialMovingWindow.mean` (:issue:`42333`) - Fixed regression in :meth:`.GroupBy.agg` incorrectly raising in some cases (:issue:`42390`) -- Fixed regression in :meth:`RangeIndex.where` and :meth:`RangeIndex.putmask` raising ``AssertionError`` when result did not represent a :class:`RangeIndex` (:issue:`43244`) +- Fixed regression in :meth:`RangeIndex.where` and :meth:`RangeIndex.putmask` raising ``AssertionError`` when result did not represent a :class:`RangeIndex` (:issue:`43240`) .. ---------------------------------------------------------------------------