Skip to content

Commit b348eac

Browse files
authored
Avoid warning in DateOffset addition (#56029)
1 parent e75a2a1 commit b348eac

File tree

3 files changed

+13
-41
lines changed

3 files changed

+13
-41
lines changed

pandas/_libs/tslibs/offsets.pyx

+1-7
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,6 @@ cdef bint is_tick_object(object obj):
101101
return isinstance(obj, Tick)
102102

103103

104-
cdef datetime _as_datetime(datetime obj):
105-
if isinstance(obj, _Timestamp):
106-
return obj.to_pydatetime()
107-
return obj
108-
109-
110104
cdef bint _is_normalized(datetime dt):
111105
if dt.hour != 0 or dt.minute != 0 or dt.second != 0 or dt.microsecond != 0:
112106
# Regardless of whether dt is datetime vs Timestamp
@@ -1322,7 +1316,7 @@ cdef class RelativeDeltaOffset(BaseOffset):
13221316
if self._use_relativedelta:
13231317
if isinstance(other, _Timestamp):
13241318
other_nanos = other.nanosecond
1325-
other = _as_datetime(other)
1319+
other = other.to_pydatetime(warn=False)
13261320

13271321
if len(self.kwds) > 0:
13281322
tzinfo = getattr(other, "tzinfo", None)

pandas/core/arrays/datetimes.py

+2-14
Original file line numberDiff line numberDiff line change
@@ -2790,13 +2790,7 @@ def _generate_range(
27902790
break
27912791

27922792
# faster than cur + offset
2793-
with warnings.catch_warnings():
2794-
warnings.filterwarnings(
2795-
"ignore",
2796-
"Discarding nonzero nanoseconds in conversion",
2797-
category=UserWarning,
2798-
)
2799-
next_date = offset._apply(cur)
2793+
next_date = offset._apply(cur)
28002794
next_date = next_date.as_unit(unit)
28012795
if next_date <= cur:
28022796
raise ValueError(f"Offset {offset} did not increment date")
@@ -2811,13 +2805,7 @@ def _generate_range(
28112805
break
28122806

28132807
# faster than cur + offset
2814-
with warnings.catch_warnings():
2815-
warnings.filterwarnings(
2816-
"ignore",
2817-
"Discarding nonzero nanoseconds in conversion",
2818-
category=UserWarning,
2819-
)
2820-
next_date = offset._apply(cur)
2808+
next_date = offset._apply(cur)
28212809
next_date = next_date.as_unit(unit)
28222810
if next_date >= cur:
28232811
raise ValueError(f"Offset {offset} did not decrement date")

pandas/tests/tseries/offsets/test_offsets.py

+10-20
Original file line numberDiff line numberDiff line change
@@ -229,18 +229,9 @@ def _check_offsetfunc_works(self, offset, funcname, dt, expected, normalize=Fals
229229
assert result == expected
230230

231231
# see gh-14101
232-
exp_warning = None
233232
ts = Timestamp(dt) + Nano(5)
234-
235-
if (
236-
type(offset_s).__name__ == "DateOffset"
237-
and (funcname in ["apply", "_apply"] or normalize)
238-
and ts.nanosecond > 0
239-
):
240-
exp_warning = UserWarning
241-
242233
# test nanosecond is preserved
243-
with tm.assert_produces_warning(exp_warning):
234+
with tm.assert_produces_warning(None):
244235
result = func(ts)
245236

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

276267
# see gh-14101
277-
exp_warning = None
278268
ts = Timestamp(dt, tz=tz) + Nano(5)
279-
280-
if (
281-
type(offset_s).__name__ == "DateOffset"
282-
and (funcname in ["apply", "_apply"] or normalize)
283-
and ts.nanosecond > 0
284-
):
285-
exp_warning = UserWarning
286-
287269
# test nanosecond is preserved
288-
with tm.assert_produces_warning(exp_warning):
270+
with tm.assert_produces_warning(None):
289271
result = func(ts)
290272
assert isinstance(result, Timestamp)
291273
if normalize is False:
@@ -1010,6 +992,14 @@ def test_dateoffset_add_sub_timestamp_with_nano():
1010992
result = offset + ts
1011993
assert result == expected
1012994

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

10141004
@pytest.mark.parametrize(
10151005
"attribute",

0 commit comments

Comments
 (0)