Skip to content

Categorical.__setitem__ doesn't handle setting tuples #20439

Closed
@TomAugspurger

Description

@TomAugspurger

We allow tuples in the categories, since they're hashable. But Categorical.__setitem__ has an incorrect check before setting.

In [3]: c = pd.Categorical([(0, 1), (1, 2)])

In [5]: c[0] = (1, 2)

Raises

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-4d3f2e8a3cfd> in <module>()
----> 1 c[0] = (1, 2)

/Users/taugspurger/miniconda3/envs/pandas-0.19.2/lib/python3.5/site-packages/pandas/core/categorical.py in __setitem__(self, key, value)
   1652         # something to np.nan
   1653         if len(to_add) and not isnull(to_add).all():
-> 1654             raise ValueError("Cannot setitem on a Categorical with a new "
   1655                              "category, set the categories first")
   1656

ValueError: Cannot setitem on a Categorical with a new category, set the categories first

If it's easy, this is worth supporting. If it's complicated, then maybe not.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugCategoricalCategorical Data TypeEnhancementIndexingRelated to indexing on series/frames, not to indexes themselvesNested DataData where the values are collections (lists, sets, dicts, objects, etc.).

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions