@@ -864,7 +864,11 @@ def setitem(self, indexer, value):
864
864
865
865
# length checking
866
866
check_setitem_lengths (indexer , value , values )
867
-
867
+ exact_match = (
868
+ len (arr_value .shape )
869
+ and arr_value .shape [0 ] == values .shape [0 ]
870
+ and arr_value .size == values .size
871
+ )
868
872
if is_empty_indexer (indexer , arr_value ):
869
873
# GH#8669 empty indexers
870
874
pass
@@ -874,21 +878,20 @@ def setitem(self, indexer, value):
874
878
# be e.g. a list; see GH#6043
875
879
values [indexer ] = value
876
880
877
- # if we are an exact match (ex-broadcasting),
878
- # then use the resultant dtype
879
881
elif (
880
- len (arr_value .shape )
881
- and arr_value . shape [ 0 ] == values . shape [ 0 ]
882
- and arr_value . size == values . size
882
+ self . is_categorical_astype (arr_value .dtype )
883
+ and not is_categorical_dtype ( values )
884
+ and exact_match
883
885
):
886
+ # GH25495 - If the current dtype is not categorical,
887
+ # we need to create a new categorical block
884
888
values [indexer ] = value
889
+ return self .make_block (Categorical (self .values , dtype = arr_value .dtype ))
885
890
886
- if self .is_categorical_astype (arr_value .dtype ) and not is_categorical_dtype (
887
- values
888
- ):
889
- # GH25495 - If the current dtype is not categorical,
890
- # we need to create a new categorical block
891
- return self .make_block (Categorical (self .values , dtype = arr_value .dtype ))
891
+ # if we are an exact match (ex-broadcasting),
892
+ # then use the resultant dtype
893
+ elif exact_match :
894
+ values [indexer ] = value
892
895
893
896
try :
894
897
values = values .astype (arr_value .dtype )
0 commit comments