-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
API: Tick arithmetic respects calendar time #22195
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 12 commits
c4d4d10
eae612f
6a2eaff
08fb37e
3fc886a
4d2a2b6
867dd89
d6fe7cc
469f5d5
84eb679
8676f63
c16980c
d64ce4f
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 |
---|---|---|
|
@@ -466,22 +466,19 @@ def _sub_datelike_dti(self, other): | |
return new_values.view('timedelta64[ns]') | ||
|
||
def _add_offset(self, offset): | ||
assert not isinstance(offset, Tick) | ||
try: | ||
if self.tz is not None: | ||
values = self.tz_localize(None) | ||
else: | ||
values = self | ||
result = offset.apply_index(values) | ||
if self.tz is not None: | ||
result = result.tz_localize(self.tz) | ||
|
||
except NotImplementedError: | ||
warnings.warn("Non-vectorized DateOffset being applied to Series " | ||
"or DatetimeIndex", PerformanceWarning) | ||
result = self.astype('O') + offset | ||
|
||
return type(self)(result, freq='infer') | ||
return type(self)(result, freq='infer', tz=self.tz) | ||
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. This was changed because if the code goes down the |
||
|
||
def _sub_datelike(self, other): | ||
# subtract a datetime from myself, yielding a ndarray[timedelta64[ns]] | ||
|
@@ -536,8 +533,12 @@ def _add_delta(self, delta): | |
method (__add__ or __sub__) | ||
""" | ||
from pandas.core.arrays.timedeltas import TimedeltaArrayMixin | ||
|
||
if isinstance(delta, (Tick, timedelta, np.timedelta64)): | ||
if isinstance(delta, Tick): | ||
# GH 20633: Ticks behave like offsets now, but cannot change | ||
# this directly in the mixin because it affects Periods and | ||
# Timedeltas | ||
return self._add_offset(delta) | ||
elif isinstance(delta, (timedelta, np.timedelta64)): | ||
new_values = self._add_delta_td(delta) | ||
elif is_timedelta64_dtype(delta): | ||
if not isinstance(delta, TimedeltaArrayMixin): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2204,7 +2204,6 @@ def delta(self): | |
def nanos(self): | ||
return delta_to_nanoseconds(self.delta) | ||
|
||
# TODO: Should Tick have its own apply_index? | ||
def apply(self, other): | ||
# Timestamp can handle tz and nano sec, thus no need to use apply_wraps | ||
if isinstance(other, Timestamp): | ||
|
@@ -2229,6 +2228,24 @@ def apply(self, other): | |
raise ApplyTypeError('Unhandled type: {type_str}' | ||
.format(type_str=type(other).__name__)) | ||
|
||
def apply_index(self, idx): | ||
""" | ||
Vectorized apply (addition) of Tick to DatetimeIndex | ||
|
||
Parameters | ||
---------- | ||
idx : DatetimeIndex | ||
|
||
Returns | ||
------- | ||
DatetimeIndex | ||
""" | ||
# TODO: Add a vectorized DatetimeIndex.dst() method | ||
ambiguous = np.array([bool(ts.dst()) if ts is not tslibs.NaT else False | ||
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. I could delay this ambiguous calculation until an AmbiguousTimeError is raised? |
||
for ts in idx]) | ||
result = idx.tz_localize(None) + self.delta | ||
return result.tz_localize(idx.tz, ambiguous=ambiguous) | ||
|
||
def isAnchored(self): | ||
return False | ||
|
||
|
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.
You might be able to import
tslibs.offsets._Tick
here and make this an isinstance check (thought that might cause a circular import, not sure off the top)