def __setitem__(self, key, value):
from pandas.core.internals.arrays import extract_array<EOL>value = extract_array(value, extract_numpy=True)<EOL>if isinstance(value, Categorical):<EOL><INDENT>if not is_dtype_equal(self, value):<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if not self.categories.equals(value.categories):<EOL><INDENT>new_codes = _recode_for_categories(<EOL>value.codes, value.categories, self.categories<EOL>)<EOL>value = Categorical.from_codes(new_codes, dtype=self.dtype)<EOL><DEDENT><DEDENT>rvalue = value if is_list_like(value) else [value]<EOL>from pandas import Index<EOL>to_add = Index(rvalue).difference(self.categories)<EOL>if len(to_add) and not isna(to_add).all():<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if isinstance(key, (int, np.integer)):<EOL><INDENT>pass<EOL><DEDENT>elif isinstance(key, tuple):<EOL><INDENT>if len(key) == <NUM_LIT:2>:<EOL><INDENT>if not com.is_null_slice(key[<NUM_LIT:0>]):<EOL><INDENT>raise AssertionError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>key = key[<NUM_LIT:1>]<EOL><DEDENT>elif len(key) == <NUM_LIT:1>:<EOL><INDENT>key = key[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>raise AssertionError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT><DEDENT>elif isinstance(key, slice):<EOL><INDENT>pass<EOL><DEDENT>lindexer = self.categories.get_indexer(rvalue)<EOL>lindexer = self._maybe_coerce_indexer(lindexer)<EOL>self._codes[key] = lindexer<EOL>
Item assignment.


Raises
------
ValueError
    If (one or more) Value is not in categories or if a assigned
    `Categorical` does not have the same categories