-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: ExtensionArray.searchsorted #24350
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ac94414
ENH: ExtensionArray.searchsorted
TomAugspurger 58418ab
PR number
TomAugspurger fee9c1a
32-bit compat
TomAugspurger ff8bbc3
updates
TomAugspurger e6adcd9
Merge remote-tracking branch 'upstream/master' into ea-searchsorted
TomAugspurger a91fcec
v -> value
TomAugspurger 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 |
---|---|---|
|
@@ -68,6 +68,7 @@ class ExtensionArray(object): | |
* unique | ||
* factorize / _values_for_factorize | ||
* argsort / _values_for_argsort | ||
* searchsorted | ||
|
||
The remaining methods implemented on this class should be performant, | ||
as they only compose abstract methods. Still, a more efficient | ||
|
@@ -518,6 +519,54 @@ def unique(self): | |
uniques = unique(self.astype(object)) | ||
return self._from_sequence(uniques, dtype=self.dtype) | ||
|
||
def searchsorted(self, value, side="left", sorter=None): | ||
""" | ||
Find indices where elements should be inserted to maintain order. | ||
|
||
.. versionadded:: 0.24.0 | ||
|
||
Find the indices into a sorted array `self` (a) such that, if the | ||
corresponding elements in `v` were inserted before the indices, the | ||
order of `self` would be preserved. | ||
|
||
Assuming that `a` is sorted: | ||
|
||
====== ============================ | ||
`side` returned index `i` satisfies | ||
====== ============================ | ||
left ``self[i-1] < v <= self[i]`` | ||
right ``self[i-1] <= v < self[i]`` | ||
====== ============================ | ||
|
||
Parameters | ||
---------- | ||
value : array_like | ||
Values to insert into `self`. | ||
side : {'left', 'right'}, optional | ||
If 'left', the index of the first suitable location found is given. | ||
If 'right', return the last such index. If there is no suitable | ||
index, return either 0 or N (where N is the length of `self`). | ||
sorter : 1-D array_like, optional | ||
Optional array of integer indices that sort array a into ascending | ||
order. They are typically the result of argsort. | ||
|
||
Returns | ||
------- | ||
indices : array of ints | ||
Array of insertion points with the same shape as `value`. | ||
|
||
See Also | ||
-------- | ||
numpy.searchsorted : Similar method from NumPy. | ||
""" | ||
# Note: the base tests provided by pandas only test the basics. | ||
# We do not test | ||
# 1. Values outside the range of the `data_for_sorting` fixture | ||
# 2. Values between the values in the `data_for_sorting` fixture | ||
# 3. Missing values. | ||
arr = self.astype(object) | ||
return arr.searchsorted(value, side=side, sorter=sorter) | ||
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. IIRC you have an issue to show a warning here for EA's that don't redefined this I think? |
||
|
||
def _values_for_factorize(self): | ||
# type: () -> Tuple[ndarray, Any] | ||
""" | ||
|
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.