-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Fixed ignoring of nanoseconds when adding to series #47856 #48008
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 1 commit
bab1995
c62dde3
ee8752f
ea1db06
f7e7dce
ca4bb65
656a00d
f9ff01d
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 |
---|---|---|
|
@@ -297,8 +297,8 @@ _relativedelta_kwds = {"years", "months", "weeks", "days", "year", "month", | |
|
||
cdef _determine_offset(kwds): | ||
# timedelta is used for sub-daily plural offsets and all singular | ||
# offsets relativedelta is used for plural offsets of daily length or | ||
# more nanosecond(s) are handled by apply_wraps | ||
# offsets, relativedelta is used for plural offsets of daily length or | ||
# more, nanosecond(s) are handled by apply_wraps | ||
kwds_no_nanos = dict( | ||
(k, v) for k, v in kwds.items() | ||
if k not in ('nanosecond', 'nanoseconds') | ||
|
@@ -1157,7 +1157,9 @@ cdef class RelativeDeltaOffset(BaseOffset): | |
return dt64other | ||
elif not self._use_relativedelta and hasattr(self, "_offset"): | ||
# timedelta | ||
delta = Timedelta(self._offset * self.n) | ||
num_nano = getattr(self, "nanoseconds", 0) | ||
rem_nano = Timedelta(nanoseconds=num_nano) | ||
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 think for performance reasons, it would be good to only construct the Timedelta and add to the offset if |
||
delta = Timedelta((self._offset + rem_nano) * self.n) | ||
td = (<_Timedelta>delta)._as_reso(reso) | ||
return dt64other + td | ||
else: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ | |
|
||
from pandas import ( | ||
DatetimeIndex, | ||
Series, | ||
date_range, | ||
) | ||
import pandas._testing as tm | ||
|
@@ -987,7 +988,7 @@ def test_dateoffset_add_sub(offset_kwargs, expected_arg): | |
assert result == expected | ||
|
||
|
||
def test_dataoffset_add_sub_timestamp_with_nano(): | ||
def test_dateoffset_add_sub_timestamp_with_nano(): | ||
offset = DateOffset(minutes=2, nanoseconds=9) | ||
ts = Timestamp(4) | ||
result = ts + offset | ||
|
@@ -1032,3 +1033,26 @@ def test_construct_int_arg_no_kwargs_assumed_days(n): | |
result = Timestamp(2022, 1, 2) + offset | ||
expected = Timestamp(2022, 1, 2 + n) | ||
assert result == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"offset, expected", | ||
[ | ||
( | ||
DateOffset(minutes=7, nanoseconds=18), | ||
Timestamp("2022-01-01 00:07:00.000000018"), | ||
), | ||
(DateOffset(nanoseconds=3), Timestamp("2022-01-01 00:00:00.000000003")), | ||
], | ||
) | ||
def test_dateoffset_add_sub_timestamp_series_with_nano(offset, expected): | ||
# GH 47856 | ||
t = Timestamp("2022-01-01") | ||
teststamp = t | ||
s = Series([t]) | ||
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. Nit: Could you avoid one letter names? |
||
s = s + offset | ||
assert s[0] == expected | ||
s -= offset | ||
assert s[0] == teststamp | ||
s = offset + s | ||
assert s[0] == expected |
Uh oh!
There was an error while loading. Please reload this page.