Skip to content

Commit

Permalink
Avoid warning in DateOffset addition
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel committed Nov 17, 2023
1 parent fd55cbe commit c31c432
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 41 deletions.
8 changes: 1 addition & 7 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,6 @@ cdef bint is_tick_object(object obj):
return isinstance(obj, Tick)


cdef datetime _as_datetime(datetime obj):
if isinstance(obj, _Timestamp):
return obj.to_pydatetime()
return obj


cdef bint _is_normalized(datetime dt):
if dt.hour != 0 or dt.minute != 0 or dt.second != 0 or dt.microsecond != 0:
# Regardless of whether dt is datetime vs Timestamp
Expand Down Expand Up @@ -1322,7 +1316,7 @@ cdef class RelativeDeltaOffset(BaseOffset):
if self._use_relativedelta:
if isinstance(other, _Timestamp):
other_nanos = other.nanosecond
other = _as_datetime(other)
other = other.to_pydatetime(warn=False)

if len(self.kwds) > 0:
tzinfo = getattr(other, "tzinfo", None)
Expand Down
16 changes: 2 additions & 14 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2790,13 +2790,7 @@ def _generate_range(
break

# faster than cur + offset
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
"Discarding nonzero nanoseconds in conversion",
category=UserWarning,
)
next_date = offset._apply(cur)
next_date = offset._apply(cur)
next_date = next_date.as_unit(unit)
if next_date <= cur:
raise ValueError(f"Offset {offset} did not increment date")
Expand All @@ -2811,13 +2805,7 @@ def _generate_range(
break

# faster than cur + offset
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
"Discarding nonzero nanoseconds in conversion",
category=UserWarning,
)
next_date = offset._apply(cur)
next_date = offset._apply(cur)
next_date = next_date.as_unit(unit)
if next_date >= cur:
raise ValueError(f"Offset {offset} did not decrement date")
Expand Down
30 changes: 10 additions & 20 deletions pandas/tests/tseries/offsets/test_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,9 @@ def _check_offsetfunc_works(self, offset, funcname, dt, expected, normalize=Fals
assert result == expected

# see gh-14101
exp_warning = None
ts = Timestamp(dt) + Nano(5)

if (
type(offset_s).__name__ == "DateOffset"
and (funcname in ["apply", "_apply"] or normalize)
and ts.nanosecond > 0
):
exp_warning = UserWarning

# test nanosecond is preserved
with tm.assert_produces_warning(exp_warning):
with tm.assert_produces_warning(None):
result = func(ts)

assert isinstance(result, Timestamp)
Expand Down Expand Up @@ -274,18 +265,9 @@ def _check_offsetfunc_works(self, offset, funcname, dt, expected, normalize=Fals
assert result == expected_localize

# see gh-14101
exp_warning = None
ts = Timestamp(dt, tz=tz) + Nano(5)

if (
type(offset_s).__name__ == "DateOffset"
and (funcname in ["apply", "_apply"] or normalize)
and ts.nanosecond > 0
):
exp_warning = UserWarning

# test nanosecond is preserved
with tm.assert_produces_warning(exp_warning):
with tm.assert_produces_warning(None):
result = func(ts)
assert isinstance(result, Timestamp)
if normalize is False:
Expand Down Expand Up @@ -1010,6 +992,14 @@ def test_dateoffset_add_sub_timestamp_with_nano():
result = offset + ts
assert result == expected

offset2 = DateOffset(minutes=2, nanoseconds=9, hour=1)
assert offset2._use_relativedelta
with tm.assert_produces_warning(None):
# no warning about Discarding nonzero nanoseconds
result2 = ts + offset2
expected2 = Timestamp("1970-01-01 01:02:00.000000013")
assert result2 == expected2


@pytest.mark.parametrize(
"attribute",
Expand Down

0 comments on commit c31c432

Please sign in to comment.