Skip to content

Commit 25a0578

Browse files
authored
BUG: DatetimeArray+DateOffset result unit (#51334)
1 parent e31f8bb commit 25a0578

File tree

2 files changed

+6
-15
lines changed

2 files changed

+6
-15
lines changed

pandas/core/arrays/datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ def _add_offset(self, offset) -> DatetimeArray:
774774
stacklevel=find_stack_level(),
775775
)
776776
result = self.astype("O") + offset
777-
result = type(self)._from_sequence(result)
777+
result = type(self)._from_sequence(result).as_unit(self.unit)
778778
if not len(self):
779779
# GH#30336 _from_sequence won't be able to infer self.tz
780780
return result.tz_localize(self.tz)

pandas/tests/tseries/offsets/test_offsets.py

+5-14
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
List,
1313
Tuple,
1414
)
15-
import warnings
1615

1716
import numpy as np
1817
import pytest
@@ -566,6 +565,9 @@ def test_offsets_hashable(self, offset_types):
566565
off = _create_offset(offset_types)
567566
assert hash(off) is not None
568567

568+
@pytest.mark.filterwarnings(
569+
"ignore:Non-vectorized DateOffset being applied to Series or DatetimeIndex"
570+
)
569571
@pytest.mark.parametrize("unit", ["s", "ms", "us"])
570572
def test_add_dt64_ndarray_non_nano(self, offset_types, unit, request):
571573
# check that the result with non-nano matches nano
@@ -576,26 +578,15 @@ def test_add_dt64_ndarray_non_nano(self, offset_types, unit, request):
576578
arr = dti._data._ndarray.astype(f"M8[{unit}]")
577579
dta = type(dti._data)._simple_new(arr, dtype=arr.dtype)
578580

579-
with warnings.catch_warnings(record=True) as w:
580-
expected = dti._data + off
581-
result = dta + off
581+
expected = dti._data + off
582+
result = dta + off
582583

583584
exp_unit = unit
584585
if isinstance(off, Tick) and off._creso > dta._creso:
585586
# cast to higher reso like we would with Timedelta scalar
586587
exp_unit = Timedelta(off).unit
587588
expected = expected.as_unit(exp_unit)
588589

589-
if len(w):
590-
# PerformanceWarning was issued bc _apply_array raised, so we
591-
# fell back to object dtype, for which the code path does
592-
# not yet cast back to the original resolution
593-
mark = pytest.mark.xfail(
594-
reason="Goes through object dtype in DatetimeArray._add_offset, "
595-
"doesn't restore reso in result"
596-
)
597-
request.node.add_marker(mark)
598-
599590
tm.assert_numpy_array_equal(result._ndarray, expected._ndarray)
600591

601592

0 commit comments

Comments
 (0)