Skip to content

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

Merged
merged 36 commits into from
Sep 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
42b1c7d
FIX for set_categories issue #17509
Giftlin Sep 13, 2017
3843746
FIX for set_categories issue #17509
Giftlin Sep 13, 2017
4f731cf
FIX for set_categories issue #17509
Giftlin Sep 13, 2017
a4ba634
Update categorical.py
Giftlin Sep 13, 2017
b817366
Update categorical.py
Giftlin Sep 13, 2017
aa782dd
Test for the changes
Giftlin Sep 15, 2017
70089a4
Merge pull request #2 from Giftlin/patch-7
Giftlin Sep 15, 2017
4c30c96
Space removed
Giftlin Sep 15, 2017
42afe67
- Bug in preserving name in set_categories. (:issue:`17509`)
Giftlin Sep 15, 2017
73a6713
Merge pull request #3 from Giftlin/patch-10
Giftlin Sep 15, 2017
8f888ef
Update v0.21.0.txt
Giftlin Sep 17, 2017
f09c9d5
Update test_categorical.py
Giftlin Sep 17, 2017
8781d64
Update v0.21.0.txt
Giftlin Sep 17, 2017
2ed3740
Update test_categorical.py
Giftlin Sep 17, 2017
8192515
Update test_categorical.py
Giftlin Sep 17, 2017
9007df3
Update v0.21.0.txt
Giftlin Sep 17, 2017
72c9ebe
Update v0.21.0.txt
Giftlin Sep 17, 2017
90ff725
Update test_categorical.py
Giftlin Sep 17, 2017
9e737c8
Merge pull request #4 from Giftlin/Giftlin-param_test_categorical.py
Giftlin Sep 17, 2017
f44a09a
Update v0.21.0.txt
Giftlin Sep 17, 2017
3b40433
Lint error fix
Giftlin Sep 17, 2017
b9ab8d3
Merge pull request #5 from Giftlin/Giftlin-lint-error-patch
Giftlin Sep 17, 2017
e21fe08
Removed space
Giftlin Sep 17, 2017
9730757
Lint error
Giftlin Sep 17, 2017
1749a1c
Merge pull request #6 from Giftlin/Giftlinlint
Giftlin Sep 17, 2017
5ae2453
Update test_categorical.py
Giftlin Sep 18, 2017
9f25ad9
Update test_categorical.py
Giftlin Sep 18, 2017
6b4f0f2
Update test_categorical.py
Giftlin Sep 18, 2017
68a2d86
Update v0.21.0.txt
Giftlin Sep 18, 2017
a94d52a
Update v0.21.0.txt
Giftlin Sep 18, 2017
73f5241
Update categorical.rst
Giftlin Sep 18, 2017
f6e8aa9
Merge pull request #9 from Giftlin/Giftlin-patch-1-1
Giftlin Sep 18, 2017
d4bbe27
Merge pull request #7 from Giftlin/Giftlin-whatsnew
Giftlin Sep 18, 2017
7bb3378
Update test_categorical.py
Giftlin Sep 18, 2017
7f5ec9b
Merge pull request #10 from Giftlin/Giftlin-test_categorical.py-1
Giftlin Sep 18, 2017
ce78e6c
small edits
jorisvandenbossche Sep 18, 2017
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
2 changes: 2 additions & 0 deletions doc/source/categorical.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ Using ``.describe()`` on categorical data will produce similar output to a `Seri
df.describe()
df["cat"].describe()

.. _categorical.cat:

Working with categories
-----------------------

Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ Categorical
- Bug in the categorical constructor with empty values and categories causing
the ``.categories`` to be an empty ``Float64Index`` rather than an empty
``Index`` with object dtype (:issue:`17248`)
- Bug in categorical operations with :ref:`Series.cat <categorical.cat>' not preserving the original Series' name (:issue:`17509`)

PyPy
^^^^
Expand Down
8 changes: 5 additions & 3 deletions pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I misread the earlier. self is the instance of CategoricalAccessor, not the Series like I expected. So this approach won't work.

Copy link
Contributor

@TomAugspurger TomAugspurger Sep 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if you modify

return CategoricalAccessor(data.values, data.index)
to get 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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so the signature should follow like we do in pandas.core.indexes.accessors, e.g. (data, index, name=None)


@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,
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,25 @@ def test_getitem_listlike(self):
expected = c[np.array([100000]).astype(np.int64)].codes
tm.assert_numpy_array_equal(result, expected)

@pytest.mark.parametrize(
"method",
[
lambda x: x.cat.set_categories([1, 2, 3]),
lambda x: x.cat.reorder_categories([2, 3, 1], ordered=True),
lambda x: x.cat.rename_categories([1, 2, 3]),
lambda x: x.cat.remove_unused_categories(),
lambda x: x.cat.remove_categories([2]),
lambda x: x.cat.add_categories([4]),
lambda x: x.cat.as_ordered(),
lambda x: x.cat.as_unordered(),
])
def test_getname_categorical_accessor(self, method):
# GH 17509
s = pd.Series([1, 2, 3], name='A').astype('category')
expected = 'A'
result = method(s).name
assert result == expected

def test_getitem_category_type(self):
# GH 14580
# test iloc() on Series with Categorical data
Expand Down