Skip to content

Categorical.from_codes does not allow categories to be a CategoricalIndex #17884

Closed
@jnothman

Description

@jnothman

Code Sample, a copy-pastable example if possible

Categorical.from_codes requires categories to be specified, with documented type "index-like" but passing a CategoricalIndex raises an error.

>>> pd.Categorical.from_codes([1, 2], pd.CategoricalIndex(['Hello', 'world', '!', '**'])
/Users/joel/anaconda3/envs/scipy3k/lib/python3.5/site-packages/pandas/core/categorical.py in get_values(self)
   1276         if is_datetimelike(self.categories):
   1277             return self.categories.take(self._codes, fill_value=np.nan)
-> 1278         return np.array(self)
   1279
   1280     def check_for_ordered(self, op):

ValueError: object __array__ method not producing an array

Problem description

It seems that being able to construct a categorical column with an existing CategoricalIndex would be a useful way to share CategoricalIndex int-string mappings. This would seem to be what from_codes aims to facilitate, but requires reconstructing the CategoricalIndex, which seems unnecessary to me.

The documentation also says index-like which seems underspecified if a CategoricalIndex is not accepted. At least, the error is not sufficiently informative.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions