Skip to content

Commit 12ac43f

Browse files
BUG: Fixed accessor for Categorical[Datetime] (#19469)
* BUG: Fixed accessor for Categorical[Datetime] * Fixup
1 parent fb3b237 commit 12ac43f

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

pandas/core/indexes/accessors.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,12 @@ def _delegate_property_get(self, name):
7272
# blow up if we operate on categories
7373
if self.orig is not None:
7474
result = take_1d(result, self.orig.cat.codes)
75+
index = self.orig.index
76+
else:
77+
index = self.index
7578

7679
# return the result as a Series, which is by definition a copy
77-
result = Series(result, index=self.index, name=self.name)
80+
result = Series(result, index=index, name=self.name)
7881

7982
# setting this object will show a SettingWithCopyWarning/Error
8083
result._is_copy = ("modifications to a property of a datetimelike "

pandas/tests/series/test_datetime_values.py

+8
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,14 @@ def f():
259259

260260
pytest.raises(com.SettingWithCopyError, f)
261261

262+
def test_dt_namespace_accessor_categorical(self):
263+
# GH 19468
264+
dti = DatetimeIndex(['20171111', '20181212']).repeat(2)
265+
s = Series(pd.Categorical(dti), name='foo')
266+
result = s.dt.year
267+
expected = Series([2017, 2017, 2018, 2018], name='foo')
268+
tm.assert_series_equal(result, expected)
269+
262270
def test_dt_accessor_no_new_attributes(self):
263271
# https://github.com/pandas-dev/pandas/issues/10673
264272
s = Series(date_range('20130101', periods=5, freq='D'))

0 commit comments

Comments
 (0)