Skip to content
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

BUG: Cannot select values in series using tuples when indexing with tuples in pandas 1.1.0 #35534

Closed
2 of 3 tasks
lfiedler opened this issue Aug 4, 2020 · 5 comments · Fixed by #36147
Closed
2 of 3 tasks
Labels
Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@lfiedler
Copy link

lfiedler commented Aug 4, 2020

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import pandas as pd

if __name__ == "__main__":
    series = pd.Series([1, 2, 3, 4], index=[("a", ), ("b", ), ("c", ), ("d", )])
    print(series[("a", )])

Problem description

On pandas 1.0.5 selecting values of a series which is indexed by tuples was possible via __getitem__ using the desired tuple (see above code). In pandas 1.1.0 this suddenly raises a ValueError:

ValueError: Can only tuple-index with a MultiIndex

As this used to work in 1.0.5 and earlier versions (0.25 as far as I can recall) I expect this to be an unwanted side effect rather than a wanted change in behavior (in which case it would certainly be breaking).
Apart from this, I expected Series objects to behave to some degree like python dictionaries. In particular using tuples of strings as keys and selecting values by such tuples I think should be possible.

Expected Output

1

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit           : d9fff2792bf16178d4e450fe7384244e50635733
python           : 3.8.3.final.0
python-bits      : 64
OS               : Linux
OS-release       : 5.4.0-42-generic
Version          : #46-Ubuntu SMP Fri Jul 10 00:24:02 UTC 2020
machine          : x86_64
processor        : x86_64
byteorder        : little
LC_ALL           : None
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 1.1.0
numpy            : 1.19.1
pytz             : 2020.1
dateutil         : 2.8.1
pip              : 20.1.1
setuptools       : 47.3.1.post20200622
Cython           : None
pytest           : None
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : None
IPython          : None
pandas_datareader: None
bs4              : None
bottleneck       : None
fsspec           : None
fastparquet      : None
gcsfs            : None
matplotlib       : None
numexpr          : None
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : None
pytables         : None
pyxlsb           : None
s3fs             : None
scipy            : None
sqlalchemy       : None
tables           : None
tabulate         : None
xarray           : None
xlrd             : None
xlwt             : None
numba            : None
None

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

Thanks @lfiedler for the report

This was changed with a bugfix #31112. @jbrockmendel I'm not sure what the bugfix was, no release note or added tests.

f19035d is the first bad commit
commit f19035d
Author: jbrockmendel [email protected]
Date: Sat Jan 18 07:49:03 2020 -0800

REF: fix calls to Index.get_value (#31112)

@simonjayhawkins
Copy link
Member

It appears that the call to result = self.index.get_value(self, key) inside a try block in Series.__getitem__ raised InvalidIndexError after the changes in #31112 instead of returning the result.

However, due to further changes in #31399, get_value is no longer called for a tuple indexer before the isinstance MultiIndex check. and the 'Can only tuple-index with a MultiIndex' message raised.

@jbrockmendel
Copy link
Member

This is pretty hairy, and likely to remain fragile even once fixed (e.g series.loc[("a",)] also raises because it treats the 1-tuple as a listlike indexer). I'd advise users to avoid this pattern wherever possible.

@simonjayhawkins
Copy link
Member

I'd advise users to avoid this pattern wherever possible.

discussion in #24688.

As there is not consensus there on deprecating tuples in index, we should restore the 1.0.5 behaviour for now.

@jbrockmendel
Copy link
Member

As there is not consensus there on deprecating tuples in index, we should restore the 1.0.5 behaviour for now.

Agreed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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
Development

Successfully merging a pull request may close this issue.

5 participants