pandas-dev/pandas · error · ValueError

Cannot slice with Ellipsis

Error message

Cannot slice with Ellipsis

What it means

Raised in SparseArray.__getitem__ when the key is a tuple that, after unpack_tuple_and_ellipses, reduces to a bare Ellipsis. SparseArray's optimized __getitem__ does not implement the Ellipsis-as-full-slice path for tuple keys, so it rejects it explicitly rather than falling through.

Source

Thrown at pandas/core/arrays/sparse/array.py:1044

            index = Index(keys, copy=False)
        else:
            index = keys
        return Series(counts, index=index, copy=False)

    # --------
    # Indexing
    # --------
    @overload
    def __getitem__(self, key: ScalarIndexer) -> Any: ...

    @overload
    def __getitem__(self, key: SequenceIndexer) -> Self: ...

    def __getitem__(self, key: PositionalIndexer) -> Self | Any:
        if isinstance(key, tuple):
            key = unpack_tuple_and_ellipses(key)
            if key is ...:
                raise ValueError("Cannot slice with Ellipsis")

        if is_integer(key):
            return self._get_val_at(key)
        elif isinstance(key, tuple):
            data_slice = self.to_dense()[key]
        elif isinstance(key, slice):
            if key == slice(None):
                # to ensure arr[:] (used by view()) does not make a copy
                result = type(self)._simple_new(
                    self.sp_values, self.sp_index, self.dtype
                )
                result._readonly = self._readonly
                return result
            # Avoid densifying when handling contiguous slices
            if key.step is None or key.step == 1:
                start = 0 if key.start is None else key.start
                if start < 0:
                    start += len(self)

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Use a full slice instead: sparse_arr[:] returns the whole array via the slice(None) fast path.
  2. Strip Ellipsis from programmatic keys before indexing: key = slice(None) if key is ... else key.
  3. Avoid tuple keys for 1-D SparseArray; pass a scalar, slice, or array directly.

Example fix

// before
sparse_arr[(Ellipsis,)]
// after
sparse_arr[:]
Defensive patterns

Strategy: validation

Validate before calling

import pandas as pd

def getitem_sparse(arr, key):
    if isinstance(key, tuple):
        from pandas.core.indexers import unpack_tuple_and_ellipses
        key = unpack_tuple_and_ellipses(key)
    if key is ...:
        key = slice(None)
    return arr[key]

Type guard

def is_safe_sparse_key(key) -> bool:
    return key is not Ellipsis and not (isinstance(key, tuple) and Ellipsis in key)

Try / catch

try:
    return arr[key]
except ValueError as e:
    if 'Ellipsis' in str(e):
        return arr[slice(None)]
    raise

Prevention

When it happens

Trigger: sparse_arr[(Ellipsis,)]; sparse_arr[..., ...] collapsing to ...; code that programmatically builds tuple keys including Ellipsis for n-D compatibility.

Common situations: Generic n-D indexing helpers that always insert Ellipsis; forwarding matrix-style keys to a 1-D array; copy-paste from DataFrame indexing.

Related errors


AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07). Data as JSON: /api/errors/6327581ce48d01cb. Report an issue: GitHub.