>> pd...">

Categorical.from_codes does not allow categories to be a CategoricalIndex · Issue #17884 · pandas-dev/pandas (original) (raw)

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.