ENH: Make categories setitem error more readable
State: closed

- [X] closes #46646


Labels:
name: Error Reporting
description: Incorrect or improved errors from pandas
name: Categorical
description: Categorical Data Type

Comments:
Thanks for the pull request. Could you show the different in traceback after this PR?
### Before this PR
```python
>>> import pandas as pd
>>> ps = pd.Series([1, 2, 3], dtype="category")

>>> ps[0] = 5
Traceback (most recent call last):
  File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/core/series.py", line 1085, in __setitem__
    self._set_with_engine(key, value)
  File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/core/series.py", line 1149, in _set_with_engine
    self._mgr.setitem_inplace(loc, value)
  File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/core/internals/base.py", line 190, in setitem_inplace
    arr[indexer] = value
  File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/core/arrays/_mixins.py", line 249, in __setitem__
    value = self._validate_setitem_value(value)
  File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/core/arrays/categorical.py", line 1457, in _validate_setitem_value
    return self._validate_scalar(value)
  File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/core/arrays/categorical.py", line 1484, in _validate_scalar
    raise TypeError(
TypeError: Cannot setitem on a Categorical with a new category (5), set the categories first

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/core/series.py", line 1140, in __setitem__
    self._set_with(key, value)
  File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/core/series.py", line 1167, in _set_with
    self._set_labels(key, value)
  File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/core/series.py", line 1179, in _set_labels
    self._set_values(indexer, value)
  File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/core/series.py", line 1185, in _set_values
    self._mgr = self._mgr.setitem(indexer=key, value=value)
  File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/core/internals/managers.py", line 337, in setitem
    return self.apply("setitem", indexer=indexer, value=value)
  File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/core/internals/managers.py", line 304, in apply
    applied = getattr(b, f)(**kwargs)
  File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/core/internals/blocks.py", line 1604, in setitem
    self.values[indexer] = value
  File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/core/arrays/_mixins.py", line 249, in __setitem__
    value = self._validate_setitem_value(value)
  File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/core/arrays/categorical.py", line 1457, in _validate_setitem_value
    return self._validate_scalar(value)
  File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/core/arrays/categorical.py", line 1484, in _validate_scalar
    raise TypeError(
TypeError: Cannot setitem on a Categorical with a new category (5), set the categories first
```
### After this PR
```python
>>> ps[0] = 5
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/core/series.py", line 1140, in __setitem__
    self._set_with(key, value)
  File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/core/series.py", line 1167, in _set_with
    self._set_labels(key, value)
  File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/core/series.py", line 1179, in _set_labels
    self._set_values(indexer, value)
  File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/core/series.py", line 1185, in _set_values
    self._mgr = self._mgr.setitem(indexer=key, value=value)
  File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/core/internals/managers.py", line 337, in setitem
    return self.apply("setitem", indexer=indexer, value=value)
  File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/core/internals/managers.py", line 304, in apply
    applied = getattr(b, f)(**kwargs)
  File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/core/internals/blocks.py", line 1604, in setitem
    self.values[indexer] = value
  File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/core/arrays/_mixins.py", line 249, in __setitem__
    value = self._validate_setitem_value(value)
  File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/core/arrays/categorical.py", line 1457, in _validate_setitem_value
    return self._validate_scalar(value)
  File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/core/arrays/categorical.py", line 1484, in _validate_scalar
    raise TypeError(
TypeError: Cannot setitem on a Categorical with a new category (5), set the categories first
```
Thanks @daspartho