Skip to content

Commit 2e3cb76

Browse files
authored
BUG: DatetimeIndex.union returning object dtype for indexes with the same tz but different units (#55259)
* add DatetimeTZDtype._get_common_type * mypy
1 parent ce814c2 commit 2e3cb76

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

doc/source/whatsnew/v2.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ Categorical
259259

260260
Datetimelike
261261
^^^^^^^^^^^^
262-
-
262+
- Bug in :meth:`DatetimeIndex.union` returning object dtype for tz-aware indexes with the same timezone but different units (:issue:`55238`)
263263
-
264264

265265
Timedelta

pandas/core/dtypes/dtypes.py

+7
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,13 @@ def __setstate__(self, state) -> None:
928928
self._tz = state["tz"]
929929
self._unit = state["unit"]
930930

931+
def _get_common_dtype(self, dtypes: list[DtypeObj]) -> DtypeObj | None:
932+
if all(isinstance(t, DatetimeTZDtype) and t.tz == self.tz for t in dtypes):
933+
np_dtype = np.max([cast(DatetimeTZDtype, t).base for t in [self, *dtypes]])
934+
unit = np.datetime_data(np_dtype)[0]
935+
return type(self)(unit=unit, tz=self.tz)
936+
return super()._get_common_dtype(dtypes)
937+
931938
@cache_readonly
932939
def index_class(self) -> type_t[DatetimeIndex]:
933940
from pandas import DatetimeIndex

pandas/tests/indexes/datetimes/test_setops.py

+8
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,14 @@ def test_union_with_DatetimeIndex(self, sort):
189189
# Fails with "AttributeError: can't set attribute"
190190
i2.union(i1, sort=sort)
191191

192+
def test_union_same_timezone_different_units(self):
193+
# GH 55238
194+
idx1 = date_range("2000-01-01", periods=3, tz="UTC").as_unit("ms")
195+
idx2 = date_range("2000-01-01", periods=3, tz="UTC").as_unit("us")
196+
result = idx1.union(idx2)
197+
expected = date_range("2000-01-01", periods=3, tz="UTC").as_unit("us")
198+
tm.assert_index_equal(result, expected)
199+
192200
# TODO: moved from test_datetimelike; de-duplicate with version below
193201
def test_intersection2(self):
194202
first = tm.makeDateIndex(10)

0 commit comments

Comments
 (0)