Skip to content

Commit 953d8e8

Browse files
Backport PR #43251: Bug in RangeIndex.where raising AssertionError when result is not from RangeIndex (#43259)
Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 5512bc4 commit 953d8e8

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

doc/source/whatsnew/v1.3.3.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Fixed regressions
1717
- Fixed regression in :class:`DataFrame` constructor failing to broadcast for defined :class:`Index` and len one list of :class:`Timestamp` (:issue:`42810`)
1818
- Performance regression in :meth:`core.window.ewm.ExponentialMovingWindow.mean` (:issue:`42333`)
1919
- Fixed regression in :meth:`.GroupBy.agg` incorrectly raising in some cases (:issue:`42390`)
20-
-
20+
- Fixed regression in :meth:`RangeIndex.where` and :meth:`RangeIndex.putmask` raising ``AssertionError`` when result did not represent a :class:`RangeIndex` (:issue:`43240`)
2121

2222
.. ---------------------------------------------------------------------------
2323

pandas/core/indexes/base.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -4723,8 +4723,7 @@ def putmask(self, mask, value) -> Index:
47234723
values, mask.sum(), converted # type: ignore[arg-type]
47244724
)
47254725
np.putmask(values, mask, converted)
4726-
4727-
return type(self)._simple_new(values, name=self.name)
4726+
return self._shallow_copy(values)
47284727

47294728
def equals(self, other: Any) -> bool:
47304729
"""

pandas/tests/indexes/ranges/test_setops.py

+14
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,17 @@ def test_symmetric_difference(self):
354354
result = left.symmetric_difference(right[1:])
355355
expected = Int64Index([1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14])
356356
tm.assert_index_equal(result, expected)
357+
358+
def test_putmask_range_cast(self):
359+
# GH#43240
360+
idx = RangeIndex(0, 5, name="test")
361+
result = idx.putmask(np.array([True, True, False, False, False]), 10)
362+
expected = Index([10, 10, 2, 3, 4], name="test")
363+
tm.assert_index_equal(result, expected)
364+
365+
def test_where_range_cast(self):
366+
# GH#43240
367+
idx = RangeIndex(0, 5, name="test")
368+
result = idx.where(np.array([False, False, True, True, True]), 10)
369+
expected = Index([10, 10, 2, 3, 4], name="test")
370+
tm.assert_index_equal(result, expected)

0 commit comments

Comments
 (0)