Skip to content

Commit

Permalink
BUG: preserve name in set_categories (#17509) (#17517)
Browse files Browse the repository at this point in the history
  • Loading branch information
Giftlin authored and jorisvandenbossche committed Sep 18, 2017
1 parent cbb090f commit 9cc3333
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
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 @@ -571,6 +571,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)

@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

0 comments on commit 9cc3333

Please sign in to comment.