plydata.cat_tools.cat_rename

plydata.cat_tools.cat_rename(c, mapping=None, **kwargs)[source]

Change/rename categories manually

Parameters
clist-like

Values that will make up the categorical.

mappingdict (optional)

Mapping of the form {old_name: new_name} for how to rename the categories. Setting a value to None removes the category. This arguments is useful if the old names are not valid python parameters. Otherwise, kwargs can be used.

**kwargsdict

Mapping to rename categories. Setting a value to None removes the category.

Examples

>>> c = list('abcd')
>>> cat_rename(c, a='A')
['A', 'b', 'c', 'd']
Categories (4, object): ['A', 'b', 'c', 'd']
>>> c = pd.Categorical(
...     list('abcd'),
...     categories=list('bacd'),
...     ordered=True
... )
>>> cat_rename(c, b='B', d='D')
['a', 'B', 'c', 'D']
Categories (4, object): ['B' < 'a' < 'c' < 'D']

Remove categories by setting them to None.

>>> cat_rename(c, b='B', d=None)
['a', 'B', 'c']
Categories (3, object): ['B' < 'a' < 'c']