-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
searchsorted, repeat broken off from #24024 #24461
Changes from 6 commits
e48f9aa
5af400f
1c7b1a4
9a0bb4a
216954e
b793665
11886cb
9873c12
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
from pandas._libs.tslibs.timestamps import ( | ||
RoundTo, maybe_integer_op_deprecated, round_nsint64) | ||
import pandas.compat as compat | ||
from pandas.compat.numpy import function as nv | ||
from pandas.errors import ( | ||
AbstractMethodError, NullFrequencyError, PerformanceWarning) | ||
from pandas.util._decorators import Appender, Substitution, deprecate_kwarg | ||
|
@@ -80,6 +81,80 @@ def _get_attributes_dict(self): | |
""" | ||
return {k: getattr(self, k, None) for k in self._attributes} | ||
|
||
@property | ||
def _scalar_type(self): | ||
# type: () -> Union[type, Tuple[type]] | ||
"""The scalar associated with this datelike | ||
|
||
* PeriodArray : Period | ||
* DatetimeArray : Timestamp | ||
* TimedeltaArray : Timedelta | ||
""" | ||
raise AbstractMethodError(self) | ||
|
||
def _scalar_from_string(self, value): | ||
# type: (str) -> Union[Period, Timestamp, Timedelta, NaTType] | ||
""" | ||
Construct a scalar type from a string. | ||
|
||
Parameters | ||
---------- | ||
value : str | ||
|
||
Returns | ||
------- | ||
Period, Timestamp, or Timedelta, or NaT | ||
Whatever the type of ``self._scalar_type`` is. | ||
|
||
Notes | ||
----- | ||
This should call ``self._check_compatible_with`` before | ||
unboxing the result. | ||
""" | ||
raise AbstractMethodError(self) | ||
|
||
def _unbox_scalar(self, value): | ||
# type: (Union[Period, Timestamp, Timedelta, NaTType]) -> int | ||
""" | ||
Unbox the integer value of a scalar `value`. | ||
|
||
Parameters | ||
---------- | ||
value : Union[Period, Timestamp, Timedelta] | ||
|
||
Returns | ||
------- | ||
int | ||
|
||
Examples | ||
-------- | ||
>>> self._unbox_scalar(Timedelta('10s')) # DOCTEST: +SKIP | ||
10000000000 | ||
""" | ||
raise AbstractMethodError(self) | ||
|
||
def _check_compatible_with(self, other): | ||
# type: (Union[Period, Timestamp, Timedelta, NaTType]) -> None | ||
# TODO: Scalar, array, or both? | ||
""" | ||
Verify that `self` and `other` are compatible. | ||
|
||
* DatetimeArray verifies that the timezones (if any) match | ||
* PeriodArray verifies that the freq matches | ||
* Timedelta has no verification | ||
|
||
In each case, NaT is considered compatible. | ||
|
||
Parameters | ||
---------- | ||
other | ||
|
||
Raises | ||
------ | ||
Exception | ||
""" | ||
raise AbstractMethodError(self) | ||
|
||
|
||
class DatelikeOps(object): | ||
""" | ||
|
@@ -468,6 +543,67 @@ def _values_for_factorize(self): | |
def _from_factorized(cls, values, original): | ||
return cls(values, dtype=original.dtype) | ||
|
||
def _values_for_argsort(self): | ||
TomAugspurger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return self._data | ||
|
||
# ------------------------------------------------------------------ | ||
# Additional array methods | ||
# These are not part of the EA API, but we implement them because | ||
# pandas assumes they're there. | ||
|
||
def searchsorted(self, v, side='left', sorter=None): | ||
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. Just to be clear, the name for the positional argument hasn't been finalized yet. We're figuring out the relative value of matching NumPy vs. matching the rest of pandas 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. well, as I have said multiple times, we are already using 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. Apparently Jeff has decided :) 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. Will revert |
||
""" | ||
Find indices where elements should be inserted to maintain order. | ||
|
||
Find the indices into a sorted array `self` such that, if the | ||
corresponding elements in `value` were inserted before the indices, | ||
the order of `self` would be preserved. | ||
|
||
Parameters | ||
---------- | ||
v : 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 `self` into ascending | ||
order. They are typically the result of ``np.argsort``. | ||
|
||
Returns | ||
------- | ||
indices : array of ints | ||
Array of insertion points with the same shape as `value`. | ||
""" | ||
if isinstance(v, compat.string_types): | ||
v = self._scalar_from_string(v) | ||
|
||
if not (isinstance(v, (self._scalar_type, type(self))) | ||
or isna(v)): | ||
raise ValueError("Unexpected type for 'value': {valtype}" | ||
.format(valtype=type(v))) | ||
|
||
self._check_compatible_with(v) | ||
if isinstance(v, type(self)): | ||
value = v.asi8 | ||
else: | ||
value = self._unbox_scalar(v) | ||
|
||
return self.asi8.searchsorted(value, side=side, sorter=sorter) | ||
|
||
def repeat(self, repeats, *args, **kwargs): | ||
""" | ||
Repeat elements of an array. | ||
|
||
See Also | ||
-------- | ||
numpy.ndarray.repeat | ||
""" | ||
nv.validate_repeat(args, kwargs) | ||
values = self._data.repeat(repeats) | ||
return type(self)(values, dtype=self.dtype) | ||
|
||
# ------------------------------------------------------------------ | ||
# Null Handling | ||
|
||
|
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.
This TODO can be removed I think. We've finalized the type IMO.
But that can be done on merge if we don't have any other commits to push.