-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: preserve name in set_categories (#17509) #17517
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
Changes from 12 commits
42b1c7d
3843746
4f731cf
a4ba634
b817366
aa782dd
70089a4
4c30c96
42afe67
73a6713
8f888ef
f09c9d5
8781d64
2ed3740
8192515
9007df3
72c9ebe
90ff725
9e737c8
f44a09a
3b40433
b9ab8d3
e21fe08
9730757
1749a1c
5ae2453
9f25ad9
6b4f0f2
68a2d86
a94d52a
73f5241
f6e8aa9
d4bbe27
7bb3378
7f5ec9b
ce78e6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -2054,9 +2054,10 @@ class CategoricalAccessor(PandasDelegate, NoNewAttributesMixin): | |||
|
||||
""" | ||||
|
||||
def __init__(self, values, index): | ||||
def __init__(self, values, index, name): | ||||
self.categorical = values | ||||
self.index = index | ||||
self.name = name | ||||
self._freeze() | ||||
|
||||
def _delegate_property_get(self, name): | ||||
|
@@ -2075,14 +2076,15 @@ def _delegate_method(self, name, *args, **kwargs): | |||
method = getattr(self.categorical, name) | ||||
res = method(*args, **kwargs) | ||||
if res is not None: | ||||
return Series(res, index=self.index) | ||||
return Series(res, index=self.index, name=self.name) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I misread the earlier. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think if you modify pandas/pandas/core/categorical.py Line 2078 in f11bbf2
data.name . and then modify CategoricalAccessor.__init__ to accept a name. data may not have a name, so maybe getattr(data, 'name', None) . Make sense?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so the signature should follow like we do in |
||||
|
||||
@classmethod | ||||
def _make_accessor(cls, data): | ||||
if not is_categorical_dtype(data.dtype): | ||||
raise AttributeError("Can only use .cat accessor with a " | ||||
"'category' dtype") | ||||
return CategoricalAccessor(data.values, data.index) | ||||
return CategoricalAccessor(data.values, data.index, | ||||
getattr(data, 'name', None),) | ||||
|
||||
|
||||
CategoricalAccessor._add_delegate_accessors(delegate=Categorical, | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,13 @@ def test_getitem_listlike(self): | |
expected = c[np.array([100000]).astype(np.int64)].codes | ||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
def test_getname_category(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. parametrize with all accessor methods There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can use a lambda expression
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current test function is also fine only right? What is the difference? Performance? |
||
expected = 'A' | ||
s = pd.Series([1, 2, 3], name='A').astype('category') | ||
s = s.cat.set_categories([1, 2, 3]) | ||
result = s.astype('category').name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be |
||
tm.assert_almost_equal(result, expected) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add additional tests for the other delegated methods that this should apply to? |
||
def test_getitem_category_type(self): | ||
# GH 14580 | ||
# test iloc() on Series with Categorical data | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, it should be double backticks, not single