20
20
is_datetime64_any_dtype ,
21
21
is_float_dtype ,
22
22
is_integer_dtype ,
23
- is_interval ,
24
23
is_interval_dtype ,
25
24
is_list_like ,
26
25
is_object_dtype ,
@@ -813,7 +812,9 @@ def take(self, indices, allow_fill=False, fill_value=None, axis=None, **kwargs):
813
812
814
813
fill_left = fill_right = fill_value
815
814
if allow_fill :
816
- fill_left , fill_right = self ._validate_fill_value (fill_value )
815
+ if (np .asarray (indices ) == - 1 ).any ():
816
+ # We have excel tests that pass fill_value=True, xref GH#36466
817
+ fill_left , fill_right = self ._validate_fill_value (fill_value )
817
818
818
819
left_take = take (
819
820
self .left , indices , allow_fill = allow_fill , fill_value = fill_left
@@ -824,20 +825,33 @@ def take(self, indices, allow_fill=False, fill_value=None, axis=None, **kwargs):
824
825
825
826
return self ._shallow_copy (left_take , right_take )
826
827
827
- def _validate_fill_value (self , value ):
828
- if is_interval (value ):
829
- self ._check_closed_matches (value , name = "fill_value" )
830
- fill_left , fill_right = value .left , value .right
831
- elif not is_scalar (value ) and notna (value ):
832
- msg = (
833
- "'IntervalArray.fillna' only supports filling with a "
834
- "'scalar pandas.Interval or NA'. "
835
- f"Got a '{ type (value ).__name__ } ' instead."
836
- )
837
- raise ValueError (msg )
828
+ def _validate_listlike (self , value ):
829
+ # list-like of intervals
830
+ try :
831
+ array = IntervalArray (value )
832
+ # TODO: self._check_closed_matches(array, name="value")
833
+ value_left , value_right = array .left , array .right
834
+ except TypeError as err :
835
+ # wrong type: not interval or NA
836
+ msg = f"'value' should be an interval type, got { type (value )} instead."
837
+ raise TypeError (msg ) from err
838
+ return value_left , value_right
839
+
840
+ def _validate_scalar (self , value ):
841
+ if isinstance (value , Interval ):
842
+ self ._check_closed_matches (value , name = "value" )
843
+ left , right = value .left , value .right
844
+ elif is_valid_nat_for_dtype (value , self .left .dtype ):
845
+ # GH#18295
846
+ left = right = value
838
847
else :
839
- fill_left = fill_right = self .left ._na_value
840
- return fill_left , fill_right
848
+ raise ValueError (
849
+ "can only insert Interval objects and NA into an IntervalArray"
850
+ )
851
+ return left , right
852
+
853
+ def _validate_fill_value (self , value ):
854
+ return self ._validate_scalar (value )
841
855
842
856
def _validate_fillna_value (self , value ):
843
857
if not isinstance (value , Interval ):
@@ -851,26 +865,12 @@ def _validate_fillna_value(self, value):
851
865
return value .left , value .right
852
866
853
867
def _validate_insert_value (self , value ):
854
- if isinstance (value , Interval ):
855
- if value .closed != self .closed :
856
- raise ValueError (
857
- "inserted item must be closed on the same side as the index"
858
- )
859
- left_insert = value .left
860
- right_insert = value .right
861
- elif is_valid_nat_for_dtype (value , self .left .dtype ):
862
- # GH#18295
863
- left_insert = right_insert = value
864
- else :
865
- raise ValueError (
866
- "can only insert Interval objects and NA into an IntervalIndex"
867
- )
868
- return left_insert , right_insert
868
+ return self ._validate_scalar (value )
869
869
870
870
def _validate_setitem_value (self , value ):
871
871
needs_float_conversion = False
872
872
873
- if is_scalar (value ) and isna ( value ):
873
+ if is_valid_nat_for_dtype (value , self . left . dtype ):
874
874
# na value: need special casing to set directly on numpy arrays
875
875
if is_integer_dtype (self .dtype .subtype ):
876
876
# can't set NaN on a numpy integer array
0 commit comments