75
75
from pandas .core .indexers import (
76
76
check_setitem_lengths ,
77
77
is_empty_indexer ,
78
+ is_exact_shape_match ,
78
79
is_scalar_indexer ,
79
80
)
80
81
import pandas .core .missing as missing
@@ -903,15 +904,7 @@ def setitem(self, indexer, value):
903
904
904
905
# coerce if block dtype can store value
905
906
values = self .values
906
- if self ._can_hold_element (value ):
907
- # We only get here for non-Extension Blocks, so _try_coerce_args
908
- # is only relevant for DatetimeBlock and TimedeltaBlock
909
- if self .dtype .kind in ["m" , "M" ]:
910
- arr = self .array_values ().T
911
- arr [indexer ] = value
912
- return self
913
-
914
- else :
907
+ if not self ._can_hold_element (value ):
915
908
# current dtype cannot store value, coerce to common dtype
916
909
# TODO: can we just use coerce_to_target_dtype for all this
917
910
if hasattr (value , "dtype" ):
@@ -932,6 +925,11 @@ def setitem(self, indexer, value):
932
925
933
926
return self .astype (dtype ).setitem (indexer , value )
934
927
928
+ if self .dtype .kind in ["m" , "M" ]:
929
+ arr = self .array_values ().T
930
+ arr [indexer ] = value
931
+ return self
932
+
935
933
# value must be storable at this moment
936
934
if is_extension_array_dtype (getattr (value , "dtype" , None )):
937
935
# We need to be careful not to allow through strings that
@@ -947,11 +945,7 @@ def setitem(self, indexer, value):
947
945
948
946
# length checking
949
947
check_setitem_lengths (indexer , value , values )
950
- exact_match = (
951
- len (arr_value .shape )
952
- and arr_value .shape [0 ] == values .shape [0 ]
953
- and arr_value .size == values .size
954
- )
948
+ exact_match = is_exact_shape_match (values , arr_value )
955
949
if is_empty_indexer (indexer , arr_value ):
956
950
# GH#8669 empty indexers
957
951
pass
0 commit comments