-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Fix name setting in DTI/TDI __add__ and __sub__ #19744
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 all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6bb94fe
Delay name setting to ensure we do it consistently
jbrockmendel 191b2fb
Whatsnew note
jbrockmendel 21a5864
update GH reference
jbrockmendel 0b51e00
docstrings, rename get_series_op_result_name-->get_op_result_name
jbrockmendel 205b09c
Merge branch 'master' of https://github.com/pandas-dev/pandas into dt…
jbrockmendel 27ed3bb
move maybe_match_name from com to ops
jbrockmendel d67d8e5
fix missing copy/paste
jbrockmendel 9bb2f9a
fix rename inplace
jbrockmendel a10a9fb
Merge branch 'master' of https://github.com/pandas-dev/pandas into dt…
jbrockmendel 34b2514
replace uses of _maybe_match_name with get_op_result_name
jbrockmendel e78d361
Merge branch 'master' of https://github.com/pandas-dev/pandas into dt…
jbrockmendel 249b7ba
dont use result.rename
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
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 |
---|---|---|
|
@@ -886,7 +886,7 @@ def _sub_datelike(self, other): | |
else: | ||
raise TypeError("cannot subtract DatetimeIndex and {typ}" | ||
.format(typ=type(other).__name__)) | ||
return TimedeltaIndex(result, name=self.name, copy=False) | ||
return TimedeltaIndex(result) | ||
|
||
def _sub_datelike_dti(self, other): | ||
"""subtraction of two DatetimeIndexes""" | ||
|
@@ -910,28 +910,39 @@ def _maybe_update_attributes(self, attrs): | |
return attrs | ||
|
||
def _add_delta(self, delta): | ||
if isinstance(delta, ABCSeries): | ||
return NotImplemented | ||
""" | ||
Add a timedelta-like, DateOffset, or TimedeltaIndex-like object | ||
to self. | ||
|
||
Parameters | ||
---------- | ||
delta : {timedelta, np.timedelta64, DateOffset, | ||
TimedelaIndex, ndarray[timedelta64]} | ||
|
||
Returns | ||
------- | ||
result : DatetimeIndex | ||
|
||
Notes | ||
----- | ||
The result's name is set outside of _add_delta by the calling | ||
method (__add__ or __sub__) | ||
""" | ||
from pandas import TimedeltaIndex | ||
name = self.name | ||
|
||
if isinstance(delta, (Tick, timedelta, np.timedelta64)): | ||
new_values = self._add_delta_td(delta) | ||
elif is_timedelta64_dtype(delta): | ||
if not isinstance(delta, TimedeltaIndex): | ||
delta = TimedeltaIndex(delta) | ||
else: | ||
# update name when delta is Index | ||
name = com._maybe_match_name(self, delta) | ||
new_values = self._add_delta_tdi(delta) | ||
elif isinstance(delta, DateOffset): | ||
new_values = self._add_offset(delta).asi8 | ||
else: | ||
new_values = self.astype('O') + delta | ||
|
||
tz = 'UTC' if self.tz is not None else None | ||
result = DatetimeIndex(new_values, tz=tz, name=name, freq='infer') | ||
result = DatetimeIndex(new_values, tz=tz, freq='infer') | ||
if self.tz is not None and self.tz is not utc: | ||
result = result.tz_convert(self.tz) | ||
return result | ||
|
@@ -954,22 +965,19 @@ def _add_offset(self, offset): | |
|
||
def _add_offset_array(self, other): | ||
# Array/Index of DateOffset objects | ||
if isinstance(other, ABCSeries): | ||
return NotImplemented | ||
elif len(other) == 1: | ||
if len(other) == 1: | ||
return self + other[0] | ||
else: | ||
warnings.warn("Adding/subtracting array of DateOffsets to " | ||
"{} not vectorized".format(type(self)), | ||
PerformanceWarning) | ||
return self.astype('O') + np.array(other) | ||
# TODO: pass freq='infer' like we do in _sub_offset_array? | ||
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. what is the purpose of these comments (esp here, after the return)? 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. a) to make sure it gets seen during review, b) as a note to myself or the next pass |
||
# TODO: This works for __add__ but loses dtype in __sub__ | ||
|
||
def _sub_offset_array(self, other): | ||
# Array/Index of DateOffset objects | ||
if isinstance(other, ABCSeries): | ||
return NotImplemented | ||
elif len(other) == 1: | ||
if len(other) == 1: | ||
return self - other[0] | ||
else: | ||
warnings.warn("Adding/subtracting array of DateOffsets to " | ||
|
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
Oops, something went wrong.
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.
let's move _maybe_match_name to ops and put next to get_op_result_name (and should remove it in favor of get_op_result_name, but that might be slightly tricky).
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.
OK. While I'm at it I'm going to move get_op_result_name with the utility functions near the top of the file instead of in the arithmetic-specific spot it is now.