Skip to content

Commit a23b3c9

Browse files
committed
Move categorical casting check further up
1 parent 712e871 commit a23b3c9

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

pandas/core/internals/blocks.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,14 @@ def setitem(self, indexer, value):
828828
if lib.is_scalar(value):
829829
value = convert_scalar(values, value)
830830

831+
if (
832+
hasattr(value, "dtype")
833+
and self.is_categorical_astype(value.dtype)
834+
and not is_categorical_dtype(values)
835+
):
836+
values[indexer] = value
837+
return self.make_block(Categorical(self.values, dtype=value.dtype))
838+
831839
else:
832840
# current dtype cannot store value, coerce to common dtype
833841
find_dtype = False
@@ -883,13 +891,6 @@ def setitem(self, indexer, value):
883891
):
884892
values[indexer] = value
885893
try:
886-
# GH25495 may need to convert to categorical block
887-
if self.is_categorical_astype(
888-
arr_value.dtype
889-
) and not is_categorical_dtype(values):
890-
return self.make_block(
891-
Categorical(self.values, dtype=arr_value.dtype)
892-
)
893894

894895
values = values.astype(arr_value.dtype)
895896
except ValueError:

0 commit comments

Comments
 (0)