-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
API: Make Series.searchsorted return a scalar, when supplied a scalar #23801
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1338,8 +1338,8 @@ def factorize(self, sort=False, na_sentinel=-1): | |
|
||
Parameters | ||
---------- | ||
value : array_like | ||
Values to insert into `self`. | ||
value : scalar or array_like | ||
Value(s) 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 | ||
|
@@ -1350,8 +1350,14 @@ def factorize(self, sort=False, na_sentinel=-1): | |
|
||
Returns | ||
------- | ||
indices : array of ints | ||
Array of insertion points with the same shape as `value`. | ||
int or array of ints | ||
A scalar or array of insertion points with the | ||
same shape as `value`. | ||
|
||
.. versionchanged :: 0.24.0 | ||
Ìf `value`is a scalar, an int is now always returned. | ||
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.
|
||
Previously, scalar inputs returned an 1-item array for | ||
:class:`Series` and :class:`Categorical`. | ||
|
||
See Also | ||
-------- | ||
|
@@ -1372,7 +1378,7 @@ def factorize(self, sort=False, na_sentinel=-1): | |
dtype: int64 | ||
|
||
>>> x.searchsorted(4) | ||
array([3]) | ||
3 | ||
|
||
>>> x.searchsorted([0, 4]) | ||
array([0, 3]) | ||
|
@@ -1389,7 +1395,7 @@ def factorize(self, sort=False, na_sentinel=-1): | |
Categories (4, object): [apple < bread < cheese < milk] | ||
|
||
>>> x.searchsorted('bread') | ||
array([1]) # Note: an array, not a scalar | ||
1 | ||
|
||
>>> x.searchsorted(['bread'], side='right') | ||
array([3]) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,9 +86,11 @@ def test_searchsorted(self): | |
# Searching for single item argument, side='left' (default) | ||
res_cat = c1.searchsorted('apple') | ||
assert res_cat == 2 | ||
assert tm.is_scalar(res_cat) | ||
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.
Another way would be to to do |
||
|
||
res_ser = s1.searchsorted('apple') | ||
assert res_ser == 2 | ||
assert tm.is_scalar(res_ser) | ||
|
||
# Searching for single item array, side='left' (default) | ||
res_cat = c1.searchsorted(['bread']) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,8 @@ | |
is_bool, is_categorical_dtype, is_datetime64_dtype, is_datetime64tz_dtype, | ||
is_datetimelike_v_numeric, is_datetimelike_v_object, | ||
is_extension_array_dtype, is_interval_dtype, is_list_like, is_number, | ||
is_period_dtype, is_sequence, is_timedelta64_dtype, needs_i8_conversion) | ||
is_period_dtype, is_scalar, is_sequence, is_timedelta64_dtype, | ||
needs_i8_conversion) # noqa | ||
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. why did you change this? 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. ´´is_scalar`` wasn't in testing, I need it in my tests and think it's helpful to have in this common testing module. I need to to for scalar because 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. Though I do get an flake8 error for unused import. Trying to resolve that... |
||
from pandas.core.dtypes.missing import array_equivalent | ||
|
||
import pandas as pd | ||
|
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.
@datapythonista is this the correct format?
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.
looks good, but can you use
int or array of int
(I'd like to parse at some point the types from these, so I prefer the type nameint
overints
)