Skip to content

BUG(?) Index.__getitem__ with 0D numpy array raises AssertionError #55601

Open
@mroeschke

Description

@mroeschke
In [5]: pd.Index([2, 2, 2])[np.array(0)]
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Cell In[5], line 1
----> 1 pd.Index([2, 2, 2])[np.array(0)]

File ~/pandas/core/indexes/base.py:5370, in Index.__getitem__(self, key)
   5366     disallow_ndim_indexing(result)
   5368 # NB: Using _constructor._simple_new would break if MultiIndex
   5369 #  didn't override __getitem__
-> 5370 return self._constructor._simple_new(result, name=self._name)

File ~/pandas/core/indexes/base.py:641, in Index._simple_new(cls, values, name, refs)
    631 @classmethod
    632 def _simple_new(
    633     cls, values: ArrayLike, name: Hashable | None = None, refs=None
    634 ) -> Self:
    635     """
    636     We require that we have a dtype compat for the values. If we are passed
    637     a non-dtype compat, then coerce using the constructor.
    638 
    639     Must be careful not to recurse.
    640     """
--> 641     assert isinstance(values, cls._data_cls), type(values)
    643     result = object.__new__(cls)
    644     result._data = values

AssertionError: <class 'numpy.int64'>

Not sure if we should allow this case, but I would expect either 2 or a ValueError

Metadata

Metadata

Assignees

No one assigned

    Labels

    IndexRelated to the Index class or subclassesIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions