-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
API: Make Series.searchsorted return a scalar, when supplied a scalar #23801
Conversation
Hello @topper-123! Thanks for updating the PR.
Comment last updated on November 20, 2018 at 00:41 Hours UTC |
9323f92
to
ef5c259
Compare
pandas/util/testing.py
Outdated
@@ -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 comment
The 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 comment
The 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 assert np.array([2]) == 2
passes without an AssertionError...
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.
Though I do get an flake8 error for unused import. Trying to resolve that...
doc/source/whatsnew/v0.24.0.rst
Outdated
@@ -1008,6 +1008,7 @@ Other API Changes | |||
- Slicing a single row of a DataFrame with multiple ExtensionArrays of the same type now preserves the dtype, rather than coercing to object (:issue:`22784`) | |||
- :class:`DateOffset` attribute `_cacheable` and method `_should_cache` have been removed (:issue:`23118`) | |||
- Comparing :class:`Timedelta` to be less or greater than unknown types now raises a ``TypeError`` instead of returning ``False`` (:issue:`20829`) | |||
- :meth:`Series.searchsorted`, when supplied a scalar value to search for, now returns a scalar instead of an array (:issue:`xxxxx`). |
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.
use this PR number
pandas/core/base.py
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
- "Ìf" -- > "If"
- missing a space between "`value`" and "is"
c9b3c91
to
e998d7c
Compare
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
tm.is_scalar
guards against res_cat being an array, as assert np.array(...) == 2
would always pass the assertion test, as long as the array is non-empty.
Another way would be to to do assert (res_cat == 2) is True
.
TheTravis failure is unrelated (hypothesis related)
|
@@ -182,22 +183,28 @@ def test_searchsorted_monotonic(indices): | |||
# test searchsorted only for increasing | |||
if indices.is_monotonic_increasing: |
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.
pls don't import is_scalar from testing, import it from the canonical location
pandas.api.types.is_scalar.
pandas/util/testing.py
Outdated
@@ -28,6 +28,7 @@ | |||
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) | |||
from pandas.core.dtypes.common import is_scalar # noqa: F401 |
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.
don't add this
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.
these are internally used imports. this is not exporting these functions.
@topper-123 happy to have this if it can be done in a reasonbale way. pls update or close. |
e998d7c
to
9d74e8c
Compare
Codecov Report
@@ Coverage Diff @@
## master #23801 +/- ##
==========================================
+ Coverage 92.21% 92.28% +0.07%
==========================================
Files 162 161 -1
Lines 51723 51459 -264
==========================================
- Hits 47695 47491 -204
+ Misses 4028 3968 -60
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #23801 +/- ##
==========================================
+ Coverage 92.29% 92.29% +<.01%
==========================================
Files 162 162
Lines 51845 51846 +1
==========================================
+ Hits 47852 47853 +1
Misses 3993 3993
Continue to review full report at Codecov.
|
9d74e8c
to
49bcca5
Compare
I've updated the PR. |
Is thia ok? This would make the searchsorted method for all of the pandas objects return scalars for scalar inputs and make the API consistent. |
this PR looked ok, merge master. |
49bcca5
to
4b31c78
Compare
Rebased. |
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`. |
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 name int
over 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`. |
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 name int
over ints
)
pandas/core/base.py
Outdated
value : array_like | ||
Values to insert into `self`. | ||
value : scalar or array_like | ||
Value(s) to insert into `self`.t |
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.
Value(s) to insert into `self`.t | |
Value(s) to insert into ``self.t``. |
I think render the whole self.t
as code makes more sense (also, we end parameter descriptions with a period).
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.
I think this is actually better: Value(s) to search for.
As we're nor actually inserting, but searching?
b7e502e
to
27af08e
Compare
2545f4a
to
8f9c917
Compare
8f9c917
to
c09a411
Compare
git diff upstream/master -u -- "*.py" | flake8 --diff
Series.searchsorted
returns the wrong shape for scalar input. Numpy arrays and all other array types return a scalar if the input is a scalar, butSeries
does not.For example: