From b05a558d4549d4bdd95cde3b54cebbf09e03a2dd Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 11 Feb 2023 13:07:20 -0800 Subject: [PATCH] BUG: DatetimeArray+DateOffset result unit --- pandas/core/arrays/datetimes.py | 2 +- pandas/tests/tseries/offsets/test_offsets.py | 19 +++++-------------- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 31aff5d4ebb41..ab9e12dc5de81 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -774,7 +774,7 @@ def _add_offset(self, offset) -> DatetimeArray: stacklevel=find_stack_level(), ) result = self.astype("O") + offset - result = type(self)._from_sequence(result) + result = type(self)._from_sequence(result).as_unit(self.unit) if not len(self): # GH#30336 _from_sequence won't be able to infer self.tz return result.tz_localize(self.tz) diff --git a/pandas/tests/tseries/offsets/test_offsets.py b/pandas/tests/tseries/offsets/test_offsets.py index 933723edd6e66..57a783d601a50 100644 --- a/pandas/tests/tseries/offsets/test_offsets.py +++ b/pandas/tests/tseries/offsets/test_offsets.py @@ -12,7 +12,6 @@ List, Tuple, ) -import warnings import numpy as np import pytest @@ -566,6 +565,9 @@ def test_offsets_hashable(self, offset_types): off = _create_offset(offset_types) assert hash(off) is not None + @pytest.mark.filterwarnings( + "ignore:Non-vectorized DateOffset being applied to Series or DatetimeIndex" + ) @pytest.mark.parametrize("unit", ["s", "ms", "us"]) def test_add_dt64_ndarray_non_nano(self, offset_types, unit, request): # check that the result with non-nano matches nano @@ -576,9 +578,8 @@ def test_add_dt64_ndarray_non_nano(self, offset_types, unit, request): arr = dti._data._ndarray.astype(f"M8[{unit}]") dta = type(dti._data)._simple_new(arr, dtype=arr.dtype) - with warnings.catch_warnings(record=True) as w: - expected = dti._data + off - result = dta + off + expected = dti._data + off + result = dta + off exp_unit = unit if isinstance(off, Tick) and off._creso > dta._creso: @@ -586,16 +587,6 @@ def test_add_dt64_ndarray_non_nano(self, offset_types, unit, request): exp_unit = Timedelta(off).unit expected = expected.as_unit(exp_unit) - if len(w): - # PerformanceWarning was issued bc _apply_array raised, so we - # fell back to object dtype, for which the code path does - # not yet cast back to the original resolution - mark = pytest.mark.xfail( - reason="Goes through object dtype in DatetimeArray._add_offset, " - "doesn't restore reso in result" - ) - request.node.add_marker(mark) - tm.assert_numpy_array_equal(result._ndarray, expected._ndarray)