diff --git a/doc/source/whatsnew/v1.3.3.rst b/doc/source/whatsnew/v1.3.3.rst index 3dee3aa5e7c7a..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:`43240`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 2615bfffb3cd9..41355b9a44b85 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..210bcd300b1b0 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, name="test") + result = idx.putmask(np.array([True, True, False, False, False]), 10) + 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, name="test") + result = idx.where(np.array([False, False, True, True, True]), 10) + expected = Index([10, 10, 2, 3, 4], name="test") + tm.assert_index_equal(result, expected)