-
-
Notifications
You must be signed in to change notification settings - Fork 18.9k
BUG: Preserve day freq on DatetimeIndex subtraction #62096
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -1089,6 +1089,18 @@ def _get_arithmetic_result_freq(self, other) -> BaseOffset | None: | |
# e.g. TestTimedelta64ArithmeticUnsorted::test_timedelta | ||
# Day is unambiguously 24h | ||
return self.freq | ||
elif ( | ||
lib.is_np_dtype(self.dtype, "M") | ||
and isinstance(self.freq, Day) | ||
and isinstance(other, Timestamp) | ||
): | ||
self = cast("DatetimeArray", self) | ||
if (self.tz is None or timezones.is_utc(self.tz)) and ( | ||
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. self.tz is None is implied by |
||
other.tz is None or timezones.is_utc(other.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. other.tz will always be None here |
||
): | ||
# e.g. issue gh-62094: subtracting a Timestamp from a DTI | ||
# with Day freq retains that freq | ||
return self.freq | ||
|
||
return None | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,3 +74,10 @@ def test_shift_no_freq(self): | |
tdi = TimedeltaIndex(["1 days 01:00:00", "2 days 01:00:00"], freq=None) | ||
with pytest.raises(NullFrequencyError, match="Cannot shift with no freq"): | ||
tdi.shift(2) | ||
|
||
def test_shift_after_dti_sub_timestamp(self): | ||
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. instead of testing this, test the subtraction op directly in tests.indexes.datetimes.test_arithmetic |
||
dti = pd.date_range("1/1/2021", "1/5/2021") | ||
tdi = dti - pd.Timestamp("1/3/2019") | ||
result = tdi.shift(1) | ||
expected = tdi + pd.Timedelta(days=1) | ||
tm.assert_index_equal(result, expected) |
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.
the note isn't necessary bc the bug is not present in a released version