55
66import numpy as np
77
8- from pandas ._typing import AnyArrayLike
8+ from pandas ._typing import Any , AnyArrayLike
99
1010from pandas .core .dtypes .common import (
1111 is_array_like ,
@@ -274,14 +274,18 @@ def deprecate_ndim_indexing(result):
274274# Public indexer validation
275275
276276
277- def check_array_indexer (array : AnyArrayLike , indexer ) -> np . ndarray :
277+ def check_array_indexer (array : AnyArrayLike , indexer : Any ) -> Any :
278278 """
279279 Check if `indexer` is a valid array indexer for `array`.
280280
281281 For a boolean mask, `array` and `indexer` are checked to have the same
282282 length. The dtype is validated, and if it is an integer or boolean
283283 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.
285289
286290 .. versionadded:: 1.0.0
287291
@@ -290,9 +294,9 @@ def check_array_indexer(array: AnyArrayLike, indexer) -> np.ndarray:
290294 array : array-like
291295 The array that is being indexed (only used for the length).
292296 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
296300
297301 Returns
298302 -------
@@ -369,11 +373,23 @@ def check_array_indexer(array: AnyArrayLike, indexer) -> np.ndarray:
369373 """
370374 from pandas .core .construction import array as pd_array
371375
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
372387 if not is_array_like (indexer ):
373388 indexer = pd_array (indexer )
374389 if len (indexer ) == 0 :
375390 # empty list is converted to float array by pd.array
376391 indexer = np .array ([], dtype = np .intp )
392+
377393 dtype = indexer .dtype
378394 if is_bool_dtype (dtype ):
379395 try :
0 commit comments