99
99
is_float_dtype ,
100
100
is_hashable ,
101
101
is_integer ,
102
- is_integer_dtype ,
103
102
is_iterator ,
104
103
is_list_like ,
105
104
is_numeric_dtype ,
161
160
extract_array ,
162
161
sanitize_array ,
163
162
)
164
- from pandas .core .indexers import disallow_ndim_indexing
163
+ from pandas .core .indexers import (
164
+ disallow_ndim_indexing ,
165
+ is_valid_positional_slice ,
166
+ )
165
167
from pandas .core .indexes .frozen import FrozenList
166
168
from pandas .core .missing import clean_reindex_fill_method
167
169
from pandas .core .ops import get_op_result_name
@@ -4071,7 +4073,7 @@ def _validate_positional_slice(self, key: slice) -> None:
4071
4073
self ._validate_indexer ("positional" , key .stop , "iloc" )
4072
4074
self ._validate_indexer ("positional" , key .step , "iloc" )
4073
4075
4074
- def _convert_slice_indexer (self , key : slice , kind : str_t ):
4076
+ def _convert_slice_indexer (self , key : slice , kind : Literal [ "loc" , "getitem" ] ):
4075
4077
"""
4076
4078
Convert a slice indexer.
4077
4079
@@ -4083,7 +4085,6 @@ def _convert_slice_indexer(self, key: slice, kind: str_t):
4083
4085
key : label of the slice bound
4084
4086
kind : {'loc', 'getitem'}
4085
4087
"""
4086
- assert kind in ["loc" , "getitem" ], kind
4087
4088
4088
4089
# potentially cast the bounds to integers
4089
4090
start , stop , step = key .start , key .stop , key .step
@@ -4096,22 +4097,14 @@ def _convert_slice_indexer(self, key: slice, kind: str_t):
4096
4097
return self .slice_indexer (start , stop , step )
4097
4098
4098
4099
# figure out if this is a positional indexer
4099
- def is_int (v ):
4100
- return v is None or is_integer (v )
4101
-
4102
- is_index_slice = is_int (start ) and is_int (stop ) and is_int (step )
4103
-
4104
- # special case for interval_dtype bc we do not do partial-indexing
4105
- # on integer Intervals when slicing
4106
- # TODO: write this in terms of e.g. should_partial_index?
4107
- ints_are_positional = self ._should_fallback_to_positional or isinstance (
4108
- self .dtype , IntervalDtype
4109
- )
4110
- is_positional = is_index_slice and ints_are_positional
4100
+ is_index_slice = is_valid_positional_slice (key )
4111
4101
4112
4102
if kind == "getitem" :
4113
4103
# called from the getitem slicers, validate that we are in fact integers
4114
- if is_index_slice or is_integer_dtype (self .dtype ):
4104
+ if is_index_slice :
4105
+ # In this case the _validate_indexer checks below are redundant
4106
+ return key
4107
+ elif self .dtype .kind in "iu" :
4115
4108
# Note: these checks are redundant if we know is_index_slice
4116
4109
self ._validate_indexer ("slice" , key .start , "getitem" )
4117
4110
self ._validate_indexer ("slice" , key .stop , "getitem" )
@@ -4120,6 +4113,14 @@ def is_int(v):
4120
4113
4121
4114
# convert the slice to an indexer here
4122
4115
4116
+ # special case for interval_dtype bc we do not do partial-indexing
4117
+ # on integer Intervals when slicing
4118
+ # TODO: write this in terms of e.g. should_partial_index?
4119
+ ints_are_positional = self ._should_fallback_to_positional or isinstance (
4120
+ self .dtype , IntervalDtype
4121
+ )
4122
+ is_positional = is_index_slice and ints_are_positional
4123
+
4123
4124
# if we are mixed and have integers
4124
4125
if is_positional :
4125
4126
try :
@@ -4151,7 +4152,7 @@ def is_int(v):
4151
4152
@final
4152
4153
def _raise_invalid_indexer (
4153
4154
self ,
4154
- form : str_t ,
4155
+ form : Literal [ "slice" , "positional" ] ,
4155
4156
key ,
4156
4157
reraise : lib .NoDefault | None | Exception = lib .no_default ,
4157
4158
) -> None :
@@ -6384,14 +6385,17 @@ def _maybe_cast_listlike_indexer(self, target) -> Index:
6384
6385
return ensure_index (target )
6385
6386
6386
6387
@final
6387
- def _validate_indexer (self , form : str_t , key , kind : str_t ) -> None :
6388
+ def _validate_indexer (
6389
+ self ,
6390
+ form : Literal ["positional" , "slice" ],
6391
+ key ,
6392
+ kind : Literal ["getitem" , "iloc" ],
6393
+ ) -> None :
6388
6394
"""
6389
6395
If we are positional indexer, validate that we have appropriate
6390
6396
typed bounds must be an integer.
6391
6397
"""
6392
- assert kind in ["getitem" , "iloc" ]
6393
-
6394
- if key is not None and not is_integer (key ):
6398
+ if not lib .is_int_or_none (key ):
6395
6399
self ._raise_invalid_indexer (form , key )
6396
6400
6397
6401
def _maybe_cast_slice_bound (self , label , side : str_t ):
0 commit comments