File tree 3 files changed +16
-1
lines changed
3 files changed +16
-1
lines changed Original file line number Diff line number Diff line change @@ -259,7 +259,7 @@ Categorical
259
259
260
260
Datetimelike
261
261
^^^^^^^^^^^^
262
- -
262
+ - Bug in :meth: ` DatetimeIndex.union ` returning object dtype for tz-aware indexes with the same timezone but different units ( :issue: ` 55238 `)
263
263
-
264
264
265
265
Timedelta
Original file line number Diff line number Diff line change @@ -928,6 +928,13 @@ def __setstate__(self, state) -> None:
928
928
self ._tz = state ["tz" ]
929
929
self ._unit = state ["unit" ]
930
930
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
+
931
938
@cache_readonly
932
939
def index_class (self ) -> type_t [DatetimeIndex ]:
933
940
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):
189
189
# Fails with "AttributeError: can't set attribute"
190
190
i2 .union (i1 , sort = sort )
191
191
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
+
192
200
# TODO: moved from test_datetimelike; de-duplicate with version below
193
201
def test_intersection2 (self ):
194
202
first = tm .makeDateIndex (10 )
You can’t perform that action at this time.
0 commit comments