9
9
10
10
from pandas ._config import get_option
11
11
12
- from pandas ._libs import NaT , algos as libalgos , hashtable as htable
12
+ from pandas ._libs import NaT , algos as libalgos , hashtable as htable , lib
13
13
from pandas ._typing import ArrayLike , Dtype , Ordered , Scalar
14
14
from pandas .compat .numpy import function as nv
15
15
from pandas .util ._decorators import cache_readonly , deprecate_kwarg , doc
@@ -1868,14 +1868,6 @@ def __repr__(self) -> str:
1868
1868
1869
1869
# ------------------------------------------------------------------
1870
1870
1871
- def _maybe_coerce_indexer (self , indexer ):
1872
- """
1873
- return an indexer coerced to the codes dtype
1874
- """
1875
- if isinstance (indexer , np .ndarray ) and indexer .dtype .kind == "i" :
1876
- indexer = indexer .astype (self ._codes .dtype )
1877
- return indexer
1878
-
1879
1871
def __getitem__ (self , key ):
1880
1872
"""
1881
1873
Return an item.
@@ -1905,6 +1897,11 @@ def __setitem__(self, key, value):
1905
1897
If (one or more) Value is not in categories or if a assigned
1906
1898
`Categorical` does not have the same categories
1907
1899
"""
1900
+ key = self ._validate_setitem_key (key )
1901
+ value = self ._validate_setitem_value (value )
1902
+ self ._ndarray [key ] = value
1903
+
1904
+ def _validate_setitem_value (self , value ):
1908
1905
value = extract_array (value , extract_numpy = True )
1909
1906
1910
1907
# require identical categories set
@@ -1934,12 +1931,19 @@ def __setitem__(self, key, value):
1934
1931
"category, set the categories first"
1935
1932
)
1936
1933
1937
- # set by position
1938
- if isinstance (key , (int , np .integer )):
1934
+ lindexer = self .categories .get_indexer (rvalue )
1935
+ if isinstance (lindexer , np .ndarray ) and lindexer .dtype .kind == "i" :
1936
+ lindexer = lindexer .astype (self ._ndarray .dtype )
1937
+
1938
+ return lindexer
1939
+
1940
+ def _validate_setitem_key (self , key ):
1941
+ if lib .is_integer (key ):
1942
+ # set by position
1939
1943
pass
1940
1944
1941
- # tuple of indexers (dataframe)
1942
1945
elif isinstance (key , tuple ):
1946
+ # tuple of indexers (dataframe)
1943
1947
# only allow 1 dimensional slicing, but can
1944
1948
# in a 2-d case be passed (slice(None),....)
1945
1949
if len (key ) == 2 :
@@ -1951,17 +1955,14 @@ def __setitem__(self, key, value):
1951
1955
else :
1952
1956
raise AssertionError ("invalid slicing for a 1-ndim categorical" )
1953
1957
1954
- # slicing in Series or Categorical
1955
1958
elif isinstance (key , slice ):
1959
+ # slicing in Series or Categorical
1956
1960
pass
1957
1961
1958
1962
# else: array of True/False in Series or Categorical
1959
1963
1960
- lindexer = self .categories .get_indexer (rvalue )
1961
- lindexer = self ._maybe_coerce_indexer (lindexer )
1962
-
1963
1964
key = check_array_indexer (self , key )
1964
- self . _codes [ key ] = lindexer
1965
+ return key
1965
1966
1966
1967
def _reverse_indexer (self ) -> Dict [Hashable , np .ndarray ]:
1967
1968
"""
0 commit comments