-
-
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
ENH: tolerance now takes list-like argument for reindex and get_indexer. #17367
Changes from all commits
bbe248c
44b08f2
e190435
700b20a
2b549b1
7e7051a
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 |
---|---|---|
|
@@ -641,12 +641,17 @@ def to_timestamp(self, freq=None, how='start'): | |
return DatetimeIndex(new_data, freq='infer', name=self.name) | ||
|
||
def _maybe_convert_timedelta(self, other): | ||
if isinstance(other, (timedelta, np.timedelta64, offsets.Tick)): | ||
if isinstance( | ||
other, (timedelta, np.timedelta64, offsets.Tick, np.ndarray)): | ||
offset = frequencies.to_offset(self.freq.rule_code) | ||
if isinstance(offset, offsets.Tick): | ||
nanos = tslib._delta_to_nanoseconds(other) | ||
if isinstance(other, np.ndarray): | ||
nanos = np.vectorize(tslib._delta_to_nanoseconds)(other) | ||
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. the array version of this function is almost trivial 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. actually ignore the above this is ok here |
||
else: | ||
nanos = tslib._delta_to_nanoseconds(other) | ||
offset_nanos = tslib._delta_to_nanoseconds(offset) | ||
if nanos % offset_nanos == 0: | ||
check = np.all(nanos % offset_nanos == 0) | ||
if check: | ||
return nanos // offset_nanos | ||
elif isinstance(other, offsets.DateOffset): | ||
freqstr = other.rule_code | ||
|
@@ -782,7 +787,7 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None): | |
target = target.asi8 | ||
|
||
if tolerance is not None: | ||
tolerance = self._convert_tolerance(tolerance) | ||
tolerance = self._convert_tolerance(tolerance, target) | ||
return Index.get_indexer(self._int64index, target, method, | ||
limit, tolerance) | ||
|
||
|
@@ -825,7 +830,8 @@ def get_loc(self, key, method=None, tolerance=None): | |
try: | ||
ordinal = tslib.iNaT if key is tslib.NaT else key.ordinal | ||
if tolerance is not None: | ||
tolerance = self._convert_tolerance(tolerance) | ||
tolerance = self._convert_tolerance(tolerance, | ||
np.asarray(key)) | ||
return self._int64index.get_loc(ordinal, method, tolerance) | ||
|
||
except KeyError: | ||
|
@@ -908,8 +914,12 @@ def _get_string_slice(self, key): | |
return slice(self.searchsorted(t1.ordinal, side='left'), | ||
self.searchsorted(t2.ordinal, side='right')) | ||
|
||
def _convert_tolerance(self, tolerance): | ||
tolerance = DatetimeIndexOpsMixin._convert_tolerance(self, tolerance) | ||
def _convert_tolerance(self, tolerance, target): | ||
tolerance = DatetimeIndexOpsMixin._convert_tolerance(self, tolerance, | ||
target) | ||
if target.size != tolerance.size and tolerance.size > 1: | ||
raise ValueError('list-like tolerance size must match ' | ||
'target index size') | ||
return self._maybe_convert_timedelta(tolerance) | ||
|
||
def insert(self, loc, item): | ||
|
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