@@ -158,9 +158,7 @@ def _get_label(self, label, axis=None):
158
158
159
159
return self .obj ._xs (label , axis = axis )
160
160
161
- def _get_loc (self , key , axis = None ):
162
- if axis is None :
163
- axis = self .axis
161
+ def _get_loc (self , key , axis : int ):
164
162
return self .obj ._ixs (key , axis = axis )
165
163
166
164
def _slice (self , obj , axis = None , kind = None ):
@@ -172,11 +170,11 @@ def _get_setitem_indexer(self, key):
172
170
if self .axis is not None :
173
171
return self ._convert_tuple (key , is_setter = True )
174
172
175
- axis = self .obj ._get_axis (0 )
173
+ ax = self .obj ._get_axis (0 )
176
174
177
- if isinstance (axis , MultiIndex ) and self .name != "iloc" :
175
+ if isinstance (ax , MultiIndex ) and self .name != "iloc" :
178
176
try :
179
- return axis .get_loc (key )
177
+ return ax .get_loc (key )
180
178
except Exception :
181
179
pass
182
180
@@ -189,8 +187,9 @@ def _get_setitem_indexer(self, key):
189
187
if isinstance (key , range ):
190
188
return self ._convert_range (key , is_setter = True )
191
189
190
+ axis = self .axis or 0
192
191
try :
193
- return self ._convert_to_indexer (key , is_setter = True )
192
+ return self ._convert_to_indexer (key , axis = axis , is_setter = True )
194
193
except TypeError as e :
195
194
196
195
# invalid indexer type vs 'other' indexing errors
@@ -206,30 +205,27 @@ def __setitem__(self, key, value):
206
205
indexer = self ._get_setitem_indexer (key )
207
206
self ._setitem_with_indexer (indexer , value )
208
207
209
- def _validate_key (self , key , axis ):
208
+ def _validate_key (self , key , axis : int ):
210
209
"""
211
210
Ensure that key is valid for current indexer.
212
211
213
212
Parameters
214
213
----------
215
214
key : scalar, slice or list-like
216
215
The key requested
217
-
218
216
axis : int
219
217
Dimension on which the indexing is being made
220
218
221
219
Raises
222
220
------
223
221
TypeError
224
222
If the key (or some element of it) has wrong type
225
-
226
223
IndexError
227
224
If the key (or some element of it) is out of bounds
228
-
229
225
KeyError
230
226
If the key was not found
231
227
"""
232
- raise AbstractMethodError ()
228
+ raise AbstractMethodError (self )
233
229
234
230
def _has_valid_tuple (self , key ):
235
231
""" check the key for valid keys across my indexer """
@@ -249,7 +245,7 @@ def _is_nested_tuple_indexer(self, tup):
249
245
return any (is_nested_tuple (tup , ax ) for ax in self .obj .axes )
250
246
return False
251
247
252
- def _convert_tuple (self , key , is_setter = False ):
248
+ def _convert_tuple (self , key , is_setter : bool = False ):
253
249
keyidx = []
254
250
if self .axis is not None :
255
251
axis = self .obj ._get_axis_number (self .axis )
@@ -268,19 +264,17 @@ def _convert_tuple(self, key, is_setter=False):
268
264
keyidx .append (idx )
269
265
return tuple (keyidx )
270
266
271
- def _convert_range (self , key , is_setter = False ):
267
+ def _convert_range (self , key , is_setter : bool = False ):
272
268
""" convert a range argument """
273
269
return list (key )
274
270
275
- def _convert_scalar_indexer (self , key , axis ):
271
+ def _convert_scalar_indexer (self , key , axis : int ):
276
272
# if we are accessing via lowered dim, use the last dim
277
- if axis is None :
278
- axis = 0
279
273
ax = self .obj ._get_axis (min (axis , self .ndim - 1 ))
280
274
# a scalar
281
275
return ax ._convert_scalar_indexer (key , kind = self .name )
282
276
283
- def _convert_slice_indexer (self , key , axis ):
277
+ def _convert_slice_indexer (self , key , axis : int ):
284
278
# if we are accessing via lowered dim, use the last dim
285
279
ax = self .obj ._get_axis (min (axis , self .ndim - 1 ))
286
280
return ax ._convert_slice_indexer (key , kind = self .name )
@@ -883,7 +877,7 @@ def _multi_take(self, tup):
883
877
}
884
878
return o ._reindex_with_indexers (d , copy = True , allow_dups = True )
885
879
886
- def _convert_for_reindex (self , key , axis = None ):
880
+ def _convert_for_reindex (self , key , axis : int ):
887
881
return key
888
882
889
883
def _handle_lowerdim_multi_index_axis0 (self , tup ):
@@ -1055,7 +1049,7 @@ def _getitem_axis(self, key, axis=None):
1055
1049
1056
1050
return self ._get_label (key , axis = axis )
1057
1051
1058
- def _get_listlike_indexer (self , key , axis , raise_missing = False ):
1052
+ def _get_listlike_indexer (self , key , axis : int , raise_missing : bool = False ):
1059
1053
"""
1060
1054
Transform a list-like of keys into a new index and an indexer.
1061
1055
@@ -1151,7 +1145,9 @@ def _getitem_iterable(self, key, axis: int):
1151
1145
{axis : [keyarr , indexer ]}, copy = True , allow_dups = True
1152
1146
)
1153
1147
1154
- def _validate_read_indexer (self , key , indexer , axis , raise_missing = False ):
1148
+ def _validate_read_indexer (
1149
+ self , key , indexer , axis : int , raise_missing : bool = False
1150
+ ):
1155
1151
"""
1156
1152
Check that indexer can be used to return a result (e.g. at least one
1157
1153
element was found, unless the list of keys was actually empty).
@@ -1216,7 +1212,9 @@ def _validate_read_indexer(self, key, indexer, axis, raise_missing=False):
1216
1212
if not (ax .is_categorical () or ax .is_interval ()):
1217
1213
warnings .warn (_missing_key_warning , FutureWarning , stacklevel = 6 )
1218
1214
1219
- def _convert_to_indexer (self , obj , axis = None , is_setter = False , raise_missing = False ):
1215
+ def _convert_to_indexer (
1216
+ self , obj , axis : int , is_setter : bool = False , raise_missing : bool = False
1217
+ ):
1220
1218
"""
1221
1219
Convert indexing key into something we can use to do actual fancy
1222
1220
indexing on an ndarray
@@ -1231,9 +1229,6 @@ def _convert_to_indexer(self, obj, axis=None, is_setter=False, raise_missing=Fal
1231
1229
raise AmbiguousIndexError with integer labels?
1232
1230
- No, prefer label-based indexing
1233
1231
"""
1234
- if axis is None :
1235
- axis = self .axis or 0
1236
-
1237
1232
labels = self .obj ._get_axis (axis )
1238
1233
1239
1234
if isinstance (obj , slice ):
@@ -1362,7 +1357,7 @@ def __init__(self, name, obj):
1362
1357
super ().__init__ (name , obj )
1363
1358
1364
1359
@Appender (_NDFrameIndexer ._validate_key .__doc__ )
1365
- def _validate_key (self , key , axis ):
1360
+ def _validate_key (self , key , axis : int ):
1366
1361
if isinstance (key , slice ):
1367
1362
return True
1368
1363
@@ -1378,7 +1373,7 @@ def _validate_key(self, key, axis):
1378
1373
1379
1374
return True
1380
1375
1381
- def _convert_for_reindex (self , key , axis = None ):
1376
+ def _convert_for_reindex (self , key , axis : int ):
1382
1377
"""
1383
1378
Transform a list of keys into a new array ready to be used as axis of
1384
1379
the object we return (e.g. including NaNs).
@@ -1394,9 +1389,6 @@ def _convert_for_reindex(self, key, axis=None):
1394
1389
-------
1395
1390
list-like of labels
1396
1391
"""
1397
-
1398
- if axis is None :
1399
- axis = self .axis or 0
1400
1392
labels = self .obj ._get_axis (axis )
1401
1393
1402
1394
if com .is_bool_indexer (key ):
@@ -1726,7 +1718,7 @@ class _LocIndexer(_LocationIndexer):
1726
1718
_exception = KeyError
1727
1719
1728
1720
@Appender (_NDFrameIndexer ._validate_key .__doc__ )
1729
- def _validate_key (self , key , axis ):
1721
+ def _validate_key (self , key , axis : int ):
1730
1722
1731
1723
# valid for a collection of labels (we check their presence later)
1732
1724
# slice of labels (where start-end in labels)
@@ -2006,7 +1998,7 @@ class _iLocIndexer(_LocationIndexer):
2006
1998
_exception = IndexError
2007
1999
_get_slice_axis = _NDFrameIndexer ._get_slice_axis
2008
2000
2009
- def _validate_key (self , key , axis ):
2001
+ def _validate_key (self , key , axis : int ):
2010
2002
if com .is_bool_indexer (key ):
2011
2003
if hasattr (key , "index" ) and isinstance (key .index , Index ):
2012
2004
if key .index .inferred_type == "integer" :
@@ -2132,7 +2124,7 @@ def _getitem_tuple(self, tup):
2132
2124
2133
2125
return retval
2134
2126
2135
- def _get_list_axis (self , key , axis = None ):
2127
+ def _get_list_axis (self , key , axis : int ):
2136
2128
"""
2137
2129
Return Series values by list or array of integers
2138
2130
@@ -2145,8 +2137,6 @@ def _get_list_axis(self, key, axis=None):
2145
2137
-------
2146
2138
Series object
2147
2139
"""
2148
- if axis is None :
2149
- axis = self .axis or 0
2150
2140
try :
2151
2141
return self .obj ._take (key , axis = axis )
2152
2142
except IndexError :
@@ -2184,10 +2174,11 @@ def _getitem_axis(self, key, axis=None):
2184
2174
2185
2175
return self ._get_loc (key , axis = axis )
2186
2176
2187
- def _convert_to_indexer (self , obj , axis = None , is_setter = False ):
2177
+ # raise_missing is included for compat with the parent class signature
2178
+ def _convert_to_indexer (
2179
+ self , obj , axis : int , is_setter : bool = False , raise_missing : bool = False
2180
+ ):
2188
2181
""" much simpler as we only have to deal with our valid types """
2189
- if axis is None :
2190
- axis = self .axis or 0
2191
2182
2192
2183
# make need to convert a float key
2193
2184
if isinstance (obj , slice ):
@@ -2209,7 +2200,7 @@ def _convert_to_indexer(self, obj, axis=None, is_setter=False):
2209
2200
class _ScalarAccessIndexer (_NDFrameIndexer ):
2210
2201
""" access scalars quickly """
2211
2202
2212
- def _convert_key (self , key , is_setter = False ):
2203
+ def _convert_key (self , key , is_setter : bool = False ):
2213
2204
return list (key )
2214
2205
2215
2206
def __getitem__ (self , key ):
@@ -2289,7 +2280,7 @@ class _AtIndexer(_ScalarAccessIndexer):
2289
2280
2290
2281
_takeable = False
2291
2282
2292
- def _convert_key (self , key , is_setter = False ):
2283
+ def _convert_key (self , key , is_setter : bool = False ):
2293
2284
""" require they keys to be the same type as the index (so we don't
2294
2285
fallback)
2295
2286
"""
@@ -2366,7 +2357,7 @@ class _iAtIndexer(_ScalarAccessIndexer):
2366
2357
def _has_valid_setitem_indexer (self , indexer ):
2367
2358
self ._has_valid_positional_setitem_indexer (indexer )
2368
2359
2369
- def _convert_key (self , key , is_setter = False ):
2360
+ def _convert_key (self , key , is_setter : bool = False ):
2370
2361
""" require integer args (and convert to label arguments) """
2371
2362
for a , i in zip (self .obj .axes , key ):
2372
2363
if not is_integer (i ):
0 commit comments