Repository: scikit-learn/scikit-learn
File: sklearn/utils/_indexing.py (Python)

Code before:
)


def _array_indexing(array, key, key_dtype, axis):
    """Index an array or scipy.sparse consistently across NumPy version."""
    xp, is_array_api, device_ = get_namespace_and_device(array)
    if is_array_api:
        if hasattr(key, "shape"):
            key = move_to(key, xp=xp, device=device_)
        elif isinstance(key, (int, slice)):
            # Passthrough for valid __getitem__ inputs as noted in the array
            # API spec.
            pass
        else:
            key = xp.asarray(key, device=device_)

        if hasattr(key, "dtype"):
            if xp.isdtype(key.dtype, "integral"):
                return xp.take(array, key, axis=axis)
            elif xp.isdtype(key.dtype, "bool"):
                # Array API does not support boolean indexing for n-dim arrays
                # yet hence the need to turn to equivalent integer indexing.
                indices = xp.arange(array.shape[axis], device=device_)
                return xp.take(array, indices[key], axis=axis)
        else:
            return array[key, ...] if axis == 0 else array[:, key]

    if issparse(array) and key_dtype == "bool":
        key = np.asarray(key)
    if isinstance(key, tuple):
        key = list(key)
    return array[key, ...] if axis == 0 else array[:, key]


def _pandas_indexing(X, key, key_dtype, axis):
    """Index a pandas dataframe or a series."""
    if _is_arraylike_not_scalar(key):
        key = np.asarray(key)

    if key_dtype == "int" and not (isinstance(key, slice) or np.isscalar(key)):
        # using take() instead of iloc[] ensures the return value is a "proper"
        # copy that will not raise SettingWithCopyWarning
        return X.take(key, axis=axis)
    else:
        # check whether we should index with loc or iloc
        indexer = X.iloc if key_dtype == "int" else X.loc
        return indexer[:, key] if axis else indexer[key]


def _list_indexing(X, key, key_dtype):
    """Index a Python list."""

Review comment (suggestion):
It's logically equivalent to remove those lines because array API inputs can never be tuples or sparse datastructures. I originally found the explicit else close more explicit but it's true that it's redundant. Let's remove it.

```suggestion
```

Code after:
)


def _array_indexing(array, key, key_dtype, axis):
    """Index an array or scipy.sparse consistently across NumPy version."""
    xp, is_array_api, device_ = get_namespace_and_device(array)
    if is_array_api:
        if hasattr(key, "shape"):
            key = move_to(key, xp=xp, device=device_)
        elif isinstance(key, (int, slice)):
            # Passthrough for valid __getitem__ inputs as noted in the array
            # API spec.
            pass
        else:
            key = xp.asarray(key, device=device_)

        if hasattr(key, "dtype"):
            if xp.isdtype(key.dtype, "integral"):
                return xp.take(array, key, axis=axis)
            elif xp.isdtype(key.dtype, "bool"):
                # Array API does not support boolean indexing for n-dim arrays
                # yet hence the need to turn to equivalent integer indexing.
                indices = xp.arange(array.shape[axis], device=device_)
                return xp.take(array, indices[key], axis=axis)

    if issparse(array) and key_dtype == "bool":
        key = np.asarray(key)
    if isinstance(key, tuple):
        key = list(key)
    return array[key, ...] if axis == 0 else array[:, key]


def _pandas_indexing(X, key, key_dtype, axis):
    """Index a pandas dataframe or a series."""
    if _is_arraylike_not_scalar(key):
        key = np.asarray(key)

    if key_dtype == "int" and not (isinstance(key, slice) or np.isscalar(key)):
        # using take() instead of iloc[] ensures the return value is a "proper"
        # copy that will not raise SettingWithCopyWarning
        return X.take(key, axis=axis)
    else:
        # check whether we should index with loc or iloc
        indexer = X.iloc if key_dtype == "int" else X.loc
        return indexer[:, key] if axis else indexer[key]


def _list_indexing(X, key, key_dtype):
    """Index a Python list."""
    if np.isscalar(key) or isinstance(key, slice):
        # key is a slice or a scalar