Skip to content

BUG: DatetimeArray+DateOffset result unit #51334

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
Feb 13, 2023
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
2 changes: 1 addition & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 5 additions & 14 deletions pandas/tests/tseries/offsets/test_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
List,
Tuple,
)
import warnings

import numpy as np
import pytest
Expand Down Expand Up @@ -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
Expand All @@ -576,26 +578,15 @@ 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:
# cast to higher reso like we would with Timedelta scalar
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)


Expand Down