Skip to content

Commit 2dfa594

Browse files
committed
Move checks to seperate elif
1 parent 5d47754 commit 2dfa594

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

pandas/core/internals/blocks.py

+15-12
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,11 @@ def setitem(self, indexer, value):
864864

865865
# length checking
866866
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+
)
868872
if is_empty_indexer(indexer, arr_value):
869873
# GH#8669 empty indexers
870874
pass
@@ -874,21 +878,20 @@ def setitem(self, indexer, value):
874878
# be e.g. a list; see GH#6043
875879
values[indexer] = value
876880

877-
# if we are an exact match (ex-broadcasting),
878-
# then use the resultant dtype
879881
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
883885
):
886+
# GH25495 - If the current dtype is not categorical,
887+
# we need to create a new categorical block
884888
values[indexer] = value
889+
return self.make_block(Categorical(self.values, dtype=arr_value.dtype))
885890

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
892895

893896
try:
894897
values = values.astype(arr_value.dtype)

0 commit comments

Comments
 (0)