Skip to content

Commit f38c15b

Browse files
author
Marco Gorelli
committed
make tz_localize operate on values rather than categories
1 parent 0bde7ce commit f38c15b

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

pandas/core/indexes/accessors.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,9 @@ def __new__(cls, data):
324324

325325
orig = data if is_categorical_dtype(data) else None
326326
if orig is not None:
327-
data = Series(orig.values.categories, name=orig.name, copy=False)
327+
data = Series(
328+
orig.values.astype("datetime64[ns]"), name=orig.name, copy=False
329+
)
328330

329331
try:
330332
if is_datetime64_dtype(data.dtype):

pandas/tests/series/test_timezones.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,17 @@ def test_tz_localize_convert_copy_inplace_mutate(self, copy, method, tz):
373373
)
374374
tm.assert_series_equal(result, expected)
375375

376+
def test_tz_localize_categorical(self, tz_aware_fixture):
377+
# GH 27952
378+
tz = tz_aware_fixture
379+
datetimes = Series(
380+
["2019-01-01", "2019-01-01", "2019-01-02"], dtype="datetime64[ns]"
381+
)
382+
categorical = datetimes.astype("category")
383+
result = categorical.dt.tz_localize(tz)
384+
expected = datetimes.dt.tz_localize(tz)
385+
tm.assert_series_equal(result, expected)
386+
376387
def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture):
377388
# GH 25843
378389
tz = tz_aware_fixture

0 commit comments

Comments
 (0)