Skip to content

Backport PR #43251 on branch 1.3.x (Bug in RangeIndex.where raising AssertionError when result is not from RangeIndex) #43259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.3.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)

.. ---------------------------------------------------------------------------

Expand Down
3 changes: 1 addition & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4723,8 +4723,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:
"""
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/indexes/ranges/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)