-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
WIP: avoid zip and unzip to improve the speed of get_indexer #41177
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
Closed
+38
−7
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -715,6 +715,24 @@ def _values(self) -> np.ndarray: | |
arr = lib.fast_zip(values) | ||
return arr | ||
|
||
@cache_readonly | ||
def _values_for_indexer(self): | ||
values = [] | ||
|
||
for i in range(self.nlevels): | ||
vals = self._get_level_values(i) | ||
if is_categorical_dtype(vals.dtype): | ||
vals = cast("CategoricalIndex", vals) | ||
vals = vals._data._internal_get_values() | ||
if isinstance(vals.dtype, ExtensionDtype): | ||
vals = vals.astype(object) | ||
# error: Incompatible types in assignment (expression has type "ndarray", | ||
# variable has type "Index") | ||
vals = np.array(vals, copy=False) # type: ignore[assignment] | ||
values.append(vals) | ||
|
||
return values | ||
|
||
@property | ||
def values(self) -> np.ndarray: | ||
return self._values | ||
|
@@ -2666,6 +2684,15 @@ def _get_partial_string_timestamp_match_key(self, key): | |
|
||
return key | ||
|
||
def _get_engine_target(self) -> np.ndarray: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. annotation is inaccurate? |
||
""" | ||
Override base | ||
Get the ndarray that we can pass to the IndexEngine constructor. | ||
""" | ||
# error: Incompatible return value type (got "Union[ExtensionArray, | ||
# ndarray]", expected "ndarray") | ||
return self._values_for_indexer # type: ignore[return-value] | ||
|
||
def _get_indexer( | ||
self, | ||
target: Index, | ||
|
@@ -2686,7 +2713,7 @@ def _get_indexer( | |
|
||
# let's instead try with a straight Index | ||
if method is None: | ||
return Index(self._values).get_indexer( | ||
return Index(self._values_for_indexer).get_indexer( | ||
target, method=method, limit=limit, tolerance=tolerance | ||
) | ||
|
||
|
@@ -2701,15 +2728,18 @@ def _get_indexer( | |
# TODO: get_indexer_with_fill docstring says values must be _sorted_ | ||
# but that doesn't appear to be enforced | ||
indexer = self._engine.get_indexer_with_fill( | ||
target=target._values, values=self._values, method=method, limit=limit | ||
target=target._values_for_indexer, | ||
values=self._values_for_indexer, | ||
method=method, | ||
limit=limit, | ||
) | ||
elif method == "nearest": | ||
raise NotImplementedError( | ||
"method='nearest' not implemented yet " | ||
"for MultiIndex; see GitHub issue 9365" | ||
) | ||
else: | ||
indexer = self._engine.get_indexer(target._values) | ||
indexer = self._engine.get_indexer(target._values_for_indexer) | ||
|
||
# Note: we only get here (in extant tests at least) with | ||
# target.nlevels == self.nlevels | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for e.g. dt64tz wouldnt we want to just use dt64?