Skip to content

PERF: avoid double-verify of take indices #40391

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 3 commits into from
Mar 12, 2021
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
11 changes: 7 additions & 4 deletions pandas/core/indexers.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def validate_indices(indices: np.ndarray, n: int) -> None:
# Indexer Conversion


def maybe_convert_indices(indices, n: int):
def maybe_convert_indices(indices, n: int, verify: bool = True):
"""
Attempt to convert indices into valid, positive indices.

Expand All @@ -248,6 +248,8 @@ def maybe_convert_indices(indices, n: int):
Array of indices that we are to convert.
n : int
Number of elements in the array that we are indexing.
verify : bool, default True
Check that all entries are between 0 and n - 1, inclusive.

Returns
-------
Expand All @@ -273,9 +275,10 @@ def maybe_convert_indices(indices, n: int):
indices = indices.copy()
indices[mask] += n

mask = (indices >= n) | (indices < 0)
if mask.any():
raise IndexError("indices are out-of-bounds")
if verify:
mask = (indices >= n) | (indices < 0)
if mask.any():
raise IndexError("indices are out-of-bounds")
return indices


Expand Down
9 changes: 2 additions & 7 deletions pandas/core/internals/array_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ def _reindex_indexer(

return type(self)(new_arrays, new_axes, verify_integrity=False)

def take(self, indexer, axis: int = 1, verify: bool = True, convert: bool = True):
def take(self: T, indexer, axis: int = 1, verify: bool = True) -> T:
"""
Take items along any axis.
"""
Expand All @@ -1034,12 +1034,7 @@ def take(self, indexer, axis: int = 1, verify: bool = True, convert: bool = True
)

n = self.shape_proper[axis]
if convert:
indexer = maybe_convert_indices(indexer, n)

if verify:
if ((indexer == -1) | (indexer >= n)).any():
raise Exception("Indices must be nonzero and less than the axis length")
indexer = maybe_convert_indices(indexer, n, verify=verify)

new_labels = self._axes[axis].take(indexer)
return self._reindex_indexer(
Expand Down
20 changes: 13 additions & 7 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1491,23 +1491,29 @@ def _make_na_block(self, placement, fill_value=None):
block_values.fill(fill_value)
return new_block(block_values, placement=placement, ndim=block_values.ndim)

def take(self, indexer, axis: int = 1, verify: bool = True, convert: bool = True):
def take(self: T, indexer, axis: int = 1, verify: bool = True) -> T:
"""
Take items along any axis.

indexer : np.ndarray or slice
axis : int, default 1
verify : bool, default True
Check that all entries are between 0 and len(self) - 1, inclusive.
Pass verify=False if this check has been done by the caller.

Returns
-------
BlockManager
"""
# We have 6 tests that get here with a slice
indexer = (
np.arange(indexer.start, indexer.stop, indexer.step, dtype="int64")
if isinstance(indexer, slice)
else np.asanyarray(indexer, dtype="int64")
)

n = self.shape[axis]
if convert:
indexer = maybe_convert_indices(indexer, n)

if verify:
if ((indexer == -1) | (indexer >= n)).any():
raise Exception("Indices must be nonzero and less than the axis length")
indexer = maybe_convert_indices(indexer, n, verify=verify)

new_labels = self.axes[axis].take(indexer)
return self.reindex_indexer(
Expand Down