7575from pandas .core .indexers import (
7676 check_setitem_lengths ,
7777 is_empty_indexer ,
78+ is_exact_shape_match ,
7879 is_scalar_indexer ,
7980)
8081import pandas .core .missing as missing
@@ -903,15 +904,7 @@ def setitem(self, indexer, value):
903904
904905 # coerce if block dtype can store value
905906 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 ):
915908 # current dtype cannot store value, coerce to common dtype
916909 # TODO: can we just use coerce_to_target_dtype for all this
917910 if hasattr (value , "dtype" ):
@@ -932,6 +925,11 @@ def setitem(self, indexer, value):
932925
933926 return self .astype (dtype ).setitem (indexer , value )
934927
928+ if self .dtype .kind in ["m" , "M" ]:
929+ arr = self .array_values ().T
930+ arr [indexer ] = value
931+ return self
932+
935933 # value must be storable at this moment
936934 if is_extension_array_dtype (getattr (value , "dtype" , None )):
937935 # We need to be careful not to allow through strings that
@@ -947,11 +945,7 @@ def setitem(self, indexer, value):
947945
948946 # length checking
949947 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 )
955949 if is_empty_indexer (indexer , arr_value ):
956950 # GH#8669 empty indexers
957951 pass
0 commit comments