Skip to content

BUG: DateOffset addition preserve non-nano #47334

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 1 commit into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ from pandas._libs.tslibs.np_datetime cimport (

from .dtypes cimport PeriodDtypeCode
from .timedeltas cimport (
_Timedelta,
delta_to_nanoseconds,
is_any_td_scalar,
)
Expand Down Expand Up @@ -1140,7 +1141,9 @@ cdef class RelativeDeltaOffset(BaseOffset):

weeks = kwds.get("weeks", 0) * self.n
if weeks:
dt64other = dt64other + Timedelta(days=7 * weeks)
delta = Timedelta(days=7 * weeks)
td = (<_Timedelta>delta)._as_reso(reso)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future this cast isn't needed once _as_reso is public?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needed as long as it is cdef instead of cpdef

dt64other = dt64other + td

timedelta_kwds = {
k: v
Expand All @@ -1149,13 +1152,14 @@ cdef class RelativeDeltaOffset(BaseOffset):
}
if timedelta_kwds:
delta = Timedelta(**timedelta_kwds)
dt64other = dt64other + (self.n * delta)
# FIXME: fails to preserve non-nano
td = (<_Timedelta>delta)._as_reso(reso)
dt64other = dt64other + (self.n * td)
return dt64other
elif not self._use_relativedelta and hasattr(self, "_offset"):
# timedelta
# FIXME: fails to preserve non-nano
return dt64other + Timedelta(self._offset * self.n)
delta = Timedelta(self._offset * self.n)
td = (<_Timedelta>delta)._as_reso(reso)
return dt64other + td
else:
# relativedelta with other keywords
kwd = set(kwds) - relativedelta_fast
Expand Down
4 changes: 0 additions & 4 deletions pandas/tests/tseries/offsets/test_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,10 +556,6 @@ def test_add_dt64_ndarray_non_nano(self, offset_types, unit, request):
# check that the result with non-nano matches nano
off = self._get_offset(offset_types)

if type(off) is DateOffset:
mark = pytest.mark.xfail(reason="non-nano not implemented")
request.node.add_marker(mark)

dti = date_range("2016-01-01", periods=35, freq="D")

arr = dti._data._ndarray.astype(f"M8[{unit}]")
Expand Down