Skip to content

AssertionError in multidimensional indexing of a Series #35527

Closed
@TomAugspurger

Description

@TomAugspurger

In pandas 1.0.5, indexing a Series with a multidimensional key raised an IndexError. In 1.1.0 it raises a TypeError:

In [1]: import pandas as pd

In [2]: s = pd.Series(pd.date_range("2000", periods=4, tz="CET"))

In [3]: s[:, None]
# 1.0.5
In [3]: s[:, None]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-3-89522aec9762> in <module>
----> 1 s[:, None]

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
# 1.1.0
In [3]: s[:, None]
/Users/taugspurger/sandbox/pandas/pandas/core/indexes/range.py:716: FutureWarning: Support for multi-dimensional indexing (e.g. `obj[:, None]`) is deprecated and will be removed in a future version.  Convert to a numpy array before indexing instead.
  return super().__getitem__(key)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-3-89522aec9762> in <module>
----> 1 s[:, None]

~/sandbox/pandas/pandas/core/series.py in __getitem__(self, key)
    906             return self._get_values(key)
    907
--> 908         return self._get_with(key)
    909
    910     def _get_with(self, key):

~/sandbox/pandas/pandas/core/series.py in _get_with(self, key)
    921             )
    922         elif isinstance(key, tuple):
--> 923             return self._get_values_tuple(key)
    924
    925         elif not is_list_like(key):

~/sandbox/pandas/pandas/core/series.py in _get_values_tuple(self, key)
    951         # mpl hackaround
    952         if com.any_none(*key):
--> 953             result = self._get_values(key)
    954             deprecate_ndim_indexing(result, stacklevel=5)
    955             return result

~/sandbox/pandas/pandas/core/series.py in _get_values(self, indexer)
    966     def _get_values(self, indexer):
    967         try:
--> 968             return self._constructor(self._mgr.get_slice(indexer)).__finalize__(self,)
    969         except ValueError:
    970             # mpl compat if we look up e.g. ser[:, np.newaxis];

~/sandbox/pandas/pandas/core/internals/managers.py in get_slice(self, slobj, axis)
   1561         array = blk._slice(slobj)
   1562         block = blk.make_block_same_class(array, placement=slice(0, len(array)))
-> 1563         return type(self)(block, self.index[slobj])
   1564
   1565     @property

~/sandbox/pandas/pandas/core/internals/managers.py in __init__(self, block, axis, do_integrity_check, fastpath)
   1505     ):
   1506         assert isinstance(block, Block), type(block)
-> 1507         assert isinstance(axis, Index), type(axis)
   1508
   1509         if fastpath is not lib.no_default:

AssertionError: <class 'numpy.ndarray'>

We should continue to raise an IndexError here.

Reported in matplotlib/matplotlib#18158.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselvesRegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions