Skip to content

API/BUG: awkward syntax to add categories to a Categorical #9927

Closed
@jreback

Description

@jreback

from SO

  • the .add_categories should be able to take an Index/ndarray, ATM it must be converted to a list
  • should be a keyword for something like: add any additional categories that I am passing to you (even if the current ones are duplicates, just add these to the end). e.g.
In [147]: s = pd.Categorical(list('aabbcd'))

In [148]: s2 = list('aabbcdefg')

In [149]: s
Out[149]: 
[a, a, b, b, c, d]
Categories (4, object): [a, b, c, d]

In [150]: s.add_categories(s.categories.sym_diff(Index(s2)).tolist())
Out[150]: 
[a, a, b, b, c, d]
Categories (7, object): [a, b, c, d, e, f, g]

I would ideally just like to say:

s.add_categories(s2, take_new=True)

(maybe not the best keyword, but something like this)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions