-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
PERF/REF: improve performance of Series.searchsorted, PandasArray.searchsorted, collect functionality #22034
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 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6ad3f12
improve performance of Series.searchsorted
60742c3
added explanation
topper-123 672802d
Make common impl. with Index.searchsorted
topper-123 c1a337c
Simplify implementation
topper-123 686a0a1
rebase
topper-123 ea8280e
collect into one function
topper-123 a9905fd
move searchsorted to algorithms.py
topper-123 9e6ed43
Guard against IntegerArray + cleanups
topper-123 bcbe226
cleanups
topper-123 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
move searchsorted to algorithms.py
- Loading branch information
commit a9905fd69d79788ca0859e3aa002588c0c663403
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
ensure_float64, ensure_int64, ensure_object, ensure_platform_int, | ||
ensure_uint64, is_array_like, is_bool_dtype, is_categorical_dtype, | ||
is_complex_dtype, is_datetime64_any_dtype, is_datetime64tz_dtype, | ||
is_datetimelike, is_extension_array_dtype, is_float_dtype, | ||
is_datetimelike, is_extension_array_dtype, is_float_dtype, is_integer, | ||
is_integer_dtype, is_interval_dtype, is_list_like, is_numeric_dtype, | ||
is_object_dtype, is_period_dtype, is_scalar, is_signed_integer_dtype, | ||
is_sparse, is_timedelta64_dtype, is_unsigned_integer_dtype, | ||
|
@@ -1724,6 +1724,88 @@ def func(arr, indexer, out, fill_value=np.nan): | |
return out | ||
|
||
|
||
# ---- # | ||
# searchsorted # | ||
# ---- # | ||
|
||
def searchsorted(arr, value, side="left", sorter=None): | ||
""" | ||
Find indices where elements should be inserted to maintain order. | ||
|
||
.. versionadded:: 0.25.0 | ||
|
||
Find the indices into a sorted array `self` (a) such that, if the | ||
corresponding elements in `value` were inserted before the indices, | ||
the order of `self` would be preserved. | ||
|
||
Assuming that `self` is sorted: | ||
|
||
====== ================================ | ||
`side` returned index `i` satisfies | ||
====== ================================ | ||
left ``self[i-1] < value <= self[i]`` | ||
right ``self[i-1] <= value < self[i]`` | ||
====== ================================ | ||
|
||
Parameters | ||
---------- | ||
arr: numpy.array or ExtensionArray | ||
array to search in. Cannot be Index, Series or PandasArray, as that | ||
would cause a RecursionError. | ||
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. not sure what this is referring. why is this not an array-like here? 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. Yes this text is wrong now, it can be indeed be array-like. |
||
value : array_like | ||
Values to insert into `arr`. | ||
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 | ||
------- | ||
array of ints | ||
Array of insertion points with the same shape as `value`. | ||
|
||
See Also | ||
-------- | ||
numpy.searchsorted : Similar method from NumPy. | ||
""" | ||
if sorter is not None: | ||
sorter = ensure_platform_int(sorter) | ||
|
||
if is_integer_dtype(arr) and ( | ||
is_integer(value) or is_integer_dtype(value)): | ||
from .arrays.array_ import array | ||
# if `arr` and `value` have different dtypes, `arr` would be | ||
# recast by numpy, causing a slow search. | ||
# Before searching below, we therefore try to give `value` the | ||
# same dtype as `arr`, while guarding against integer overflows. | ||
iinfo = np.iinfo(arr.dtype.type) | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
value_arr = np.array([value]) if is_scalar(value) else np.array(value) | ||
if (value_arr >= iinfo.min).all() and (value_arr <= iinfo.max).all(): | ||
# value within bounds, so no overflow, so can convert value dtype | ||
# to dtype of arr | ||
dtype = arr.dtype | ||
else: | ||
dtype = value_arr.dtype | ||
|
||
if is_scalar(value): | ||
value = dtype.type(value) | ||
else: | ||
value = array(value, dtype=dtype) | ||
elif not (is_object_dtype(arr) or is_numeric_dtype(arr) or | ||
is_categorical_dtype(arr)): | ||
from pandas.core.series import Series | ||
# E.g. if `arr` is an array with dtype='datetime64[ns]' | ||
# and `value` is a pd.Timestamp, we may need to convert value | ||
value_ser = Series(value)._values | ||
value = value_ser[0] if is_scalar(value) else value_ser | ||
|
||
result = arr.searchsorted(value, side=side, sorter=sorter) | ||
return result | ||
|
||
|
||
# ---- # | ||
# diff # | ||
# ---- # | ||
|
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.
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.
can yo u make the 1st and 3rd lines match here