5
5
6
6
import numpy as np
7
7
8
- from pandas ._typing import AnyArrayLike
8
+ from pandas ._typing import Any , AnyArrayLike
9
9
10
10
from pandas .core .dtypes .common import (
11
11
is_array_like ,
@@ -274,14 +274,18 @@ def deprecate_ndim_indexing(result):
274
274
# Public indexer validation
275
275
276
276
277
- def check_array_indexer (array : AnyArrayLike , indexer ) -> np . ndarray :
277
+ def check_array_indexer (array : AnyArrayLike , indexer : Any ) -> Any :
278
278
"""
279
279
Check if `indexer` is a valid array indexer for `array`.
280
280
281
281
For a boolean mask, `array` and `indexer` are checked to have the same
282
282
length. The dtype is validated, and if it is an integer or boolean
283
283
ExtensionArray, it is checked if there are missing values present, and
284
- it is converted to the appropriate numpy array.
284
+ it is converted to the appropriate numpy array. Other dtypes will raise
285
+ an error.
286
+
287
+ Non-array indexers (integer, slice, Ellipsis, tuples, ..) are passed
288
+ through as is.
285
289
286
290
.. versionadded:: 1.0.0
287
291
@@ -290,9 +294,9 @@ def check_array_indexer(array: AnyArrayLike, indexer) -> np.ndarray:
290
294
array : array-like
291
295
The array that is being indexed (only used for the length).
292
296
indexer : array-like or list-like
293
- The array-like that's used to index. The function assumes this is an
294
- array-like, and input that is not yet an numpy array or an ExtensionArray
295
- is converted to one.
297
+ The array-like that's used to index. List-like input that is not yet
298
+ a numpy array or an ExtensionArray is converted to one. Other input
299
+ types are passed through as is
296
300
297
301
Returns
298
302
-------
@@ -369,11 +373,23 @@ def check_array_indexer(array: AnyArrayLike, indexer) -> np.ndarray:
369
373
"""
370
374
from pandas .core .construction import array as pd_array
371
375
376
+ # whathever is not an array-like is returned as-is (possible valid array
377
+ # indexers that are not array-like: integer, slice, Ellipsis, None)
378
+ # In this context, tuples are not considered as array-like, as they have
379
+ # a specific meaning in indexing (multi-dimensional indexing)
380
+ if is_list_like (indexer ):
381
+ if isinstance (indexer , tuple ):
382
+ return indexer
383
+ else :
384
+ return indexer
385
+
386
+ # convert list-likes to array
372
387
if not is_array_like (indexer ):
373
388
indexer = pd_array (indexer )
374
389
if len (indexer ) == 0 :
375
390
# empty list is converted to float array by pd.array
376
391
indexer = np .array ([], dtype = np .intp )
392
+
377
393
dtype = indexer .dtype
378
394
if is_bool_dtype (dtype ):
379
395
try :
0 commit comments