Rename categories with Series by TomAugspurger · Pull Request #17982 · pandas-dev/pandas (original) (raw)
actually, looking at this again.
a Series should be just like a dict. This is a perf issue yes?
do this. (once Index.map
works we could simplify a bit)
In [12]: cat = pd.Categorical(['a', 'b', 'c', 'd'])
...: res = cat.rename_categories(pd.Series({'a': 4, 'b': 3, 'c': 2, 'd': 1}))
...:
...:
In [13]: cat
Out[13]:
[a, b, c, d]
Categories (4, object): [a, b, c, d]
In [14]: res
Out[14]:
[4, 3, 2, 1]
Categories (4, int64): [4, 3, 2, 1]
In [17]: pd.Series(cat.categories).map({'a': 4, 'b': 3, 'c': 2, 'd': 1}).values
Out[17]: array([4, 3, 2, 1])
In [19]: pd.Series(cat.categories).map({'a': 4, 'b': 3, 'c': 2, 'd': 1}).values
Out[19]: array([4, 3, 2, 1])