-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Fix PeriodIndex +/- TimedeltaIndex #23031
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
341212f
Fix PeriodIndex +- TimedeltaIndex
jbrockmendel 0704e53
better comment
jbrockmendel 97f6798
typo fixup
jbrockmendel a4d2da2
Allow _add_delta_tdi to handle ndarray[timedelta64] gracefully
jbrockmendel 01d3af6
Implement specialized versions of _add_delta, _add_delta_tdi
jbrockmendel 9393f5d
Add tests for adding offset scalar
jbrockmendel 0573f3f
add tests for subtracting offset scalar
jbrockmendel e1ebe09
Merge branch 'master' of https://github.com/pandas-dev/pandas into pi…
jbrockmendel 30d9f6b
remove superfluous comment
jbrockmendel 9bac746
whatsnew
jbrockmendel 59748cd
Revert incorrect docstring change
jbrockmendel eb252bf
docstring
jbrockmendel 8f5fd55
comment
jbrockmendel ed50b9f
comment
jbrockmendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ | |
from pandas.util._decorators import cache_readonly | ||
|
||
from pandas.core.dtypes.common import ( | ||
is_integer_dtype, is_float_dtype, is_period_dtype) | ||
is_integer_dtype, is_float_dtype, is_period_dtype, is_timedelta64_dtype) | ||
from pandas.core.dtypes.dtypes import PeriodDtype | ||
from pandas.core.dtypes.generic import ABCSeries | ||
|
||
|
@@ -316,8 +316,7 @@ def _add_delta_td(self, other): | |
freqstr=self.freqstr)) | ||
|
||
def _add_delta(self, other): | ||
ordinal_delta = self._maybe_convert_timedelta(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. Getting the _maybe_convert_timedelta call out of the arithmetic ops makes everything simpler |
||
return self._time_shift(ordinal_delta) | ||
return self._time_shift(other) | ||
|
||
def shift(self, n): | ||
""" | ||
|
@@ -335,7 +334,8 @@ def shift(self, n): | |
return self._time_shift(n) | ||
|
||
def _time_shift(self, n): | ||
values = self._ndarray_values + n * self.freq.n | ||
n = self._maybe_convert_timedelta(n) | ||
values = self._ndarray_values + n | ||
if self.hasnans: | ||
values[self._isnan] = iNaT | ||
return self._shallow_copy(values=values) | ||
|
@@ -346,7 +346,8 @@ def _maybe_convert_timedelta(self, other): | |
|
||
Parameters | ||
---------- | ||
other : timedelta, np.timedelta64, DateOffset, int, np.ndarray | ||
other : timedelta, np.timedelta64, DateOffset, int, | ||
np.ndarray[timedelta64], TimedeltaIndex | ||
|
||
Returns | ||
------- | ||
|
@@ -357,30 +358,42 @@ def _maybe_convert_timedelta(self, other): | |
IncompatibleFrequency : if the input cannot be written as a multiple | ||
of self.freq. Note IncompatibleFrequency subclasses ValueError. | ||
""" | ||
if isinstance( | ||
other, (timedelta, np.timedelta64, Tick, np.ndarray)): | ||
offset = frequencies.to_offset(self.freq.rule_code) | ||
if isinstance(offset, Tick): | ||
if isinstance(other, (timedelta, np.timedelta64, Tick)): | ||
base_offset = frequencies.to_offset(self.freq.rule_code) | ||
if isinstance(base_offset, Tick): | ||
other_nanos = delta_to_nanoseconds(other) | ||
base_nanos = delta_to_nanoseconds(base_offset) | ||
check = np.all(other_nanos % base_nanos == 0) | ||
if check: | ||
return other_nanos // base_nanos | ||
elif (isinstance(other, (np.ndarray, DatetimeLikeArrayMixin)) and | ||
is_timedelta64_dtype(other)): | ||
# i.e. TimedeltaArray/TimedeltaIndex | ||
if isinstance(self.freq, Tick): | ||
base_offset = frequencies.to_offset(self.freq.rule_code) | ||
base_nanos = base_offset.nanos | ||
if isinstance(other, np.ndarray): | ||
nanos = np.vectorize(delta_to_nanoseconds)(other) | ||
other_nanos = other.astype('m8[ns]').view('i8') | ||
else: | ||
nanos = delta_to_nanoseconds(other) | ||
offset_nanos = delta_to_nanoseconds(offset) | ||
check = np.all(nanos % offset_nanos == 0) | ||
other_nanos = other.asi8 | ||
check = np.all(other_nanos % base_nanos == 0) | ||
if check: | ||
return nanos // offset_nanos | ||
return other_nanos // base_nanos | ||
elif isinstance(other, DateOffset): | ||
freqstr = other.rule_code | ||
base = frequencies.get_base_alias(freqstr) | ||
if base == self.freq.rule_code: | ||
return other.n | ||
return other.n * self.freq.n | ||
msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr) | ||
raise IncompatibleFrequency(msg) | ||
elif lib.is_integer(other): | ||
# integer is passed to .shift via | ||
# _add_datetimelike_methods basically | ||
# but ufunc may pass integer to _add_delta | ||
return other | ||
return other * self.freq.n | ||
elif is_integer_dtype(other) and isinstance(other, np.ndarray): | ||
# TODO: Also need to get Index | ||
return other * self.freq.n | ||
|
||
# raise when input doesn't have freq | ||
msg = "Input has different freq from {cls}(freq={freqstr})" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -805,6 +805,7 @@ def assert_index_equal(left, right, exact='equiv', check_names=True, | |
Specify object name being compared, internally used to show appropriate | ||
assertion message | ||
""" | ||
__tracebackhide__ = True | ||
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. See #22643 |
||
|
||
def _check_types(l, r, obj='Index'): | ||
if exact: | ||
|
@@ -1048,6 +1049,8 @@ def assert_interval_array_equal(left, right, exact='equiv', | |
|
||
|
||
def raise_assert_detail(obj, message, left, right, diff=None): | ||
__tracebackhide__ = True | ||
|
||
if isinstance(left, np.ndarray): | ||
left = pprint_thing(left) | ||
elif is_categorical_dtype(left): | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
can you clarify?