Skip to content

Commit 28da588

Browse files
BUG: preserve DTA/TDA+timedeltalike scalar with mismatched resos (pandas-dev#48923)
* BUG: Timedelta.__new__ * remove assertion * GH refs * API: Timedelta(td64_obj) retain resolution * API: Timedelta constructor pytimedelta, Tick preserve reso * STYLE: fix pylint style checks for inherit-non-class. (pandas-dev#48897) * fix pylint for non-class-inherit and add pre-commit changes * move ExtensionArraySupportsAnyAll to below ExtensionArray * move if TYPE_CHECKING back up to line 94 * remove erroneous space * BUG: preserve DTA/TDA+timedeltalike scalar with mismatched resos * merge main * remove debugging variable Co-authored-by: Kostya Farber <[email protected]>
1 parent bada58a commit 28da588

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

pandas/tests/arrays/test_datetimes.py

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
"""
22
Tests for DatetimeArray
33
"""
4+
from datetime import timedelta
45
import operator
56

67
import numpy as np
78
import pytest
89

910
from pandas._libs.tslibs import tz_compare
10-
from pandas._libs.tslibs.dtypes import NpyDatetimeUnit
11+
from pandas._libs.tslibs.dtypes import (
12+
NpyDatetimeUnit,
13+
npy_unit_to_abbrev,
14+
)
1115

1216
from pandas.core.dtypes.dtypes import DatetimeTZDtype
1317

@@ -221,6 +225,35 @@ def test_add_mismatched_reso_doesnt_downcast(self):
221225
# (so we _could_ downcast to unit="s"), we do not.
222226
assert res._unit == "us"
223227

228+
@pytest.mark.parametrize(
229+
"scalar",
230+
[
231+
timedelta(hours=2),
232+
pd.Timedelta(hours=2),
233+
np.timedelta64(2, "h"),
234+
np.timedelta64(2 * 3600 * 1000, "ms"),
235+
pd.offsets.Minute(120),
236+
pd.offsets.Hour(2),
237+
],
238+
)
239+
def test_add_timedeltalike_scalar_mismatched_reso(self, dta_dti, scalar):
240+
dta, dti = dta_dti
241+
242+
td = pd.Timedelta(scalar)
243+
exp_reso = max(dta._reso, td._reso)
244+
exp_unit = npy_unit_to_abbrev(exp_reso)
245+
246+
expected = (dti + td)._data._as_unit(exp_unit)
247+
result = dta + scalar
248+
tm.assert_extension_array_equal(result, expected)
249+
250+
result = scalar + dta
251+
tm.assert_extension_array_equal(result, expected)
252+
253+
expected = (dti - td)._data._as_unit(exp_unit)
254+
result = dta - scalar
255+
tm.assert_extension_array_equal(result, expected)
256+
224257
def test_sub_datetimelike_scalar_mismatch(self):
225258
dti = pd.date_range("2016-01-01", periods=3)
226259
dta = dti._data._as_unit("us")

0 commit comments

Comments
 (0)