diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index 699b8749e7580..69156ca561508 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -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 @@ -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) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 9079e33e08dc7..0aa2078f6a076 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -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") @@ -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") diff --git a/pandas/tests/tseries/offsets/test_offsets.py b/pandas/tests/tseries/offsets/test_offsets.py index f360d2d239384..bc20e840b7c61 100644 --- a/pandas/tests/tseries/offsets/test_offsets.py +++ b/pandas/tests/tseries/offsets/test_offsets.py @@ -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) @@ -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: @@ -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",