@@ -740,7 +740,7 @@ def _validate_fill_value(self, fill_value):
740
740
return fill_value
741
741
742
742
def _validate_shift_value (self , fill_value ):
743
- # TODO(2.0): once this deprecation is enforced, used _validate_fill_value
743
+ # TODO(2.0): once this deprecation is enforced, use _validate_fill_value
744
744
if is_valid_nat_for_dtype (fill_value , self .dtype ):
745
745
fill_value = NaT
746
746
elif isinstance (fill_value , self ._recognized_scalars ):
@@ -782,6 +782,9 @@ def _validate_searchsorted_value(self, value):
782
782
elif isinstance (value , self ._recognized_scalars ):
783
783
value = self ._scalar_type (value )
784
784
785
+ elif isinstance (value , type (self )):
786
+ pass
787
+
785
788
elif is_list_like (value ) and not isinstance (value , type (self )):
786
789
value = array (value )
787
790
@@ -791,7 +794,7 @@ def _validate_searchsorted_value(self, value):
791
794
f"not { type (value ).__name__ } "
792
795
)
793
796
794
- if not ( isinstance ( value , ( self . _scalar_type , type ( self ))) or ( value is NaT )) :
797
+ else :
795
798
raise TypeError (f"Unexpected type for 'value': { type (value )} " )
796
799
797
800
if isinstance (value , type (self )):
@@ -803,25 +806,41 @@ def _validate_searchsorted_value(self, value):
803
806
return value
804
807
805
808
def _validate_setitem_value (self , value ):
806
- if lib .is_scalar (value ) and not isna (value ):
807
- value = com .maybe_box_datetimelike (value )
808
809
809
810
if is_list_like (value ):
810
- value = type (self )._from_sequence (value , dtype = self .dtype )
811
- self ._check_compatible_with (value , setitem = True )
812
- value = value .asi8
813
- elif isinstance (value , self ._scalar_type ):
814
- self ._check_compatible_with (value , setitem = True )
815
- value = self ._unbox_scalar (value )
811
+ value = array (value )
812
+ if is_dtype_equal (value .dtype , "string" ):
813
+ # We got a StringArray
814
+ try :
815
+ # TODO: Could use from_sequence_of_strings if implemented
816
+ # Note: passing dtype is necessary for PeriodArray tests
817
+ value = type (self )._from_sequence (value , dtype = self .dtype )
818
+ except ValueError :
819
+ pass
820
+
821
+ if not type (self )._is_recognized_dtype (value ):
822
+ raise TypeError (
823
+ "setitem requires compatible dtype or scalar, "
824
+ f"not { type (value ).__name__ } "
825
+ )
826
+
827
+ elif isinstance (value , self ._recognized_scalars ):
828
+ value = self ._scalar_type (value )
816
829
elif is_valid_nat_for_dtype (value , self .dtype ):
817
- value = iNaT
830
+ value = NaT
818
831
else :
819
832
msg = (
820
833
f"'value' should be a '{ self ._scalar_type .__name__ } ', 'NaT', "
821
834
f"or array of those. Got '{ type (value ).__name__ } ' instead."
822
835
)
823
836
raise TypeError (msg )
824
837
838
+ self ._check_compatible_with (value , setitem = True )
839
+ if isinstance (value , type (self )):
840
+ value = value .asi8
841
+ else :
842
+ value = self ._unbox_scalar (value )
843
+
825
844
return value
826
845
827
846
def _validate_insert_value (self , value ):
0 commit comments