File tree Expand file tree Collapse file tree 3 files changed +16
-1
lines changed Expand file tree Collapse file tree 3 files changed +16
-1
lines changed Original file line number Diff line number Diff line change @@ -259,7 +259,7 @@ Categorical
259259
260260Datetimelike
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
265265Timedelta
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 )
You can’t perform that action at this time.
0 commit comments