Skip to content

BUG: Fixed accessor for Categorical[Datetime] #19469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pandas/core/indexes/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,12 @@ def _delegate_property_get(self, name):
# blow up if we operate on categories
if self.orig is not None:
result = take_1d(result, self.orig.cat.codes)
index = self.orig.index
else:
index = self.index

# return the result as a Series, which is by definition a copy
result = Series(result, index=self.index, name=self.name)
result = Series(result, index=index, name=self.name)

# setting this object will show a SettingWithCopyWarning/Error
result._is_copy = ("modifications to a property of a datetimelike "
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/series/test_datetime_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,14 @@ def f():

pytest.raises(com.SettingWithCopyError, f)

def test_dt_namespace_accessor_categorical(self):
# GH 19468
dti = DatetimeIndex(['20171111', '20181212']).repeat(2)
s = Series(pd.Categorical(dti), name='foo')
result = s.dt.year
expected = Series([2017, 2017, 2018, 2018], name='foo')
tm.assert_series_equal(result, expected)

def test_dt_accessor_no_new_attributes(self):
# https://github.com/pandas-dev/pandas/issues/10673
s = Series(date_range('20130101', periods=5, freq='D'))
Expand Down