Skip to content

BUG: preserve DTA/TDA+timedeltalike scalar with mismatched resos #48923

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 17 commits into from
Oct 11, 2022
Merged
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
35 changes: 34 additions & 1 deletion pandas/tests/arrays/test_datetimes.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
"""
Tests for DatetimeArray
"""
from datetime import timedelta
import operator

import numpy as np
import pytest

from pandas._libs.tslibs import tz_compare
from pandas._libs.tslibs.dtypes import NpyDatetimeUnit
from pandas._libs.tslibs.dtypes import (
NpyDatetimeUnit,
npy_unit_to_abbrev,
)

from pandas.core.dtypes.dtypes import DatetimeTZDtype

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

@pytest.mark.parametrize(
"scalar",
[
timedelta(hours=2),
pd.Timedelta(hours=2),
np.timedelta64(2, "h"),
np.timedelta64(2 * 3600 * 1000, "ms"),
pd.offsets.Minute(120),
pd.offsets.Hour(2),
],
)
def test_add_timedeltalike_scalar_mismatched_reso(self, dta_dti, scalar):
dta, dti = dta_dti

td = pd.Timedelta(scalar)
exp_reso = max(dta._reso, td._reso)
exp_unit = npy_unit_to_abbrev(exp_reso)

expected = (dti + td)._data._as_unit(exp_unit)
result = dta + scalar
tm.assert_extension_array_equal(result, expected)

result = scalar + dta
tm.assert_extension_array_equal(result, expected)

expected = (dti - td)._data._as_unit(exp_unit)
result = dta - scalar
tm.assert_extension_array_equal(result, expected)

def test_sub_datetimelike_scalar_mismatch(self):
dti = pd.date_range("2016-01-01", periods=3)
dta = dti._data._as_unit("us")
Expand Down