Skip to content

AssertionError in multidimensional indexing of a Series #35527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
TomAugspurger opened this issue Aug 3, 2020 · 6 comments · Fixed by #38099
Closed

AssertionError in multidimensional indexing of a Series #35527

TomAugspurger opened this issue Aug 3, 2020 · 6 comments · Fixed by #38099
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@TomAugspurger
Copy link
Contributor

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.

@TomAugspurger TomAugspurger added Bug Needs Triage Issue that has not been reviewed by a pandas team member Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 3, 2020
@TomAugspurger TomAugspurger added this to the 1.1.1 milestone Aug 3, 2020
@simonjayhawkins simonjayhawkins modified the milestones: 1.1.1, 1.1.2 Aug 20, 2020
@dsaxton
Copy link
Member

dsaxton commented Aug 29, 2020

f1fe031 is the first bad commit
commit f1fe031
Author: jbrockmendel [email protected]
Date: Fri Apr 24 14:41:43 2020 -0700

CLN: simplify DTA.__getitem__ (#33714)

pandas/core/arrays/datetimelike.py | 31 +++++++++++--------------------
1 file changed, 11 insertions(+), 20 deletions(-)

cc @jbrockmendel

@jbrockmendel
Copy link
Member

AssertionError is clearly wrong, but it isnt obvious to me that IndexingError is correct. The code path this goes through includes

    def _get_values_tuple(self, key):
        # mpl hackaround
        if com.any_none(*key):
            result = self._get_values(key)
            deprecate_ndim_indexing(result, stacklevel=5)
            return result

which suggests that we should be treating the obj[:, None] like obj[:].values[:, None]

@simonjayhawkins simonjayhawkins modified the milestones: 1.1.2, 1.1.3 Sep 7, 2020
@simonjayhawkins
Copy link
Member

moved off 1.1.2 milestone (scheduled for this week) as no PRs to fix in the pipeline

@simonjayhawkins
Copy link
Member

f1fe031 is the first bad commit

#33714

@simonjayhawkins simonjayhawkins modified the milestones: 1.1.3, 1.1.4 Oct 5, 2020
@simonjayhawkins
Copy link
Member

moved off 1.1.3 milestone (overdue) as no PRs to fix in the pipeline

@simonjayhawkins simonjayhawkins modified the milestones: 1.1.4, 1.1.5 Oct 29, 2020
@simonjayhawkins
Copy link
Member

moved off 1.1.4 milestone (scheduled for release tomorrow) as no PRs to fix in the pipeline

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version
Projects
None yet
6 participants