Skip to content

PERF: Fix performance regression in get_loc of IntervalIndex #51339

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
merged 4 commits into from
Feb 14, 2023
Merged
Changes from 1 commit
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
9 changes: 6 additions & 3 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,9 @@ def _validate(cls, left, right, dtype: IntervalDtype) -> None:
msg = "left side of interval must be <= right side"
raise ValueError(msg)

def _shallow_copy(self: IntervalArrayT, left, right) -> IntervalArrayT:
def _shallow_copy(
self: IntervalArrayT, left, right, verify_integrity: bool = True
) -> IntervalArrayT:
"""
Return a new IntervalArray with the replacement attributes

Expand All @@ -675,7 +677,8 @@ def _shallow_copy(self: IntervalArrayT, left, right) -> IntervalArrayT:
"""
dtype = IntervalDtype(left.dtype, closed=self.closed)
left, right, dtype = self._ensure_simple_new_inputs(left, right, dtype=dtype)
self._validate(left, right, dtype=dtype)
if verify_integrity:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so is this still necessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for this regression, but I think we should look through all usages to see if it's necessary everywhere

self._validate(left, right, dtype=dtype)

return self._simple_new(left, right, dtype=dtype)

Expand Down Expand Up @@ -727,7 +730,7 @@ def __getitem__(
if np.ndim(left) > 1:
# GH#30588 multi-dimensional indexer disallowed
raise ValueError("multi-dimensional indexing not allowed")
return self._shallow_copy(left, right)
return self._shallow_copy(left, right, verify_integrity=False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couldnt we also skip _ensure_simple_new_inputs, i.e. just do

return self._simple_new(left, right, dtype=self.dtype)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's see if it works, probably not necessary


def __setitem__(self, key, value) -> None:
value_left, value_right = self._validate_setitem_value(value)
Expand Down