Skip to content

BUG:Tuple Series name causes InvalidIndexError in Pandas 1.2.4 but not in 0.25.3 #42966

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
2 of 3 tasks
soilstack opened this issue Aug 10, 2021 · 3 comments
Closed
2 of 3 tasks
Labels
Bug Duplicate Report Duplicate issue or pull request Groupby Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version

Comments

@soilstack
Copy link
Contributor

  • 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
print(f"pandas: {pd.__version__}")
!python --version   

qf = pd.Series(index = pd.to_datetime(['12Dec2020', '13Dec2020','14Dec2020', '15Dec2020','16Dec2020']),
                    data= [1,1,-1,-1,-1], 
          name=('coz1', 'this_bar'),
         )
qf.index.name="Date"
qf = qf.groupby(
    (qf != qf.shift())
    .cumsum()
).cumsum().apply(int)
qf.head(10)

Problem description

I pass the groupby function a by parameter which is series whose name is a Tuple. In older pandas (0.25.3), this worked. In modern (1.2.4) pandas this raises an InvalidIndexError.

You might say, "well then don't name your series with Tuple, name it with a string." How my situation happens in real-life is that I'm taking a .loc slice of a DataFrame with multilevel columns. This returns a Series, and its name is automatically a tuple of the column levels. I can do a gross temporary-rename of the series, but feels like a hack.

Expected Output

pandas: 0.25.3
Python 3.7.11
Date
2020-12-12    1
2020-12-13    2
2020-12-14   -1
2020-12-15   -2
2020-12-16   -3
Name: (coz1, this_bar), dtype: int64

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 2cb9652
python : 3.9.4.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19041
machine : AMD64
processor : AMD64 Family 23 Model 113 Stepping 0, AuthenticAMD
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_Singapore.1252

pandas : 1.2.4
numpy : 1.20.1
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 52.0.0.post20210125
Cython : 0.29.23
pytest : 6.2.3
hypothesis : None
sphinx : 4.0.1
blosc : None
feather : None
xlsxwriter : 1.3.8
lxml.etree : 4.6.3
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.22.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : 1.3.2
fsspec : 0.9.0
fastparquet : None
gcsfs : None
matplotlib : 3.3.4
numexpr : 2.7.3
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.2
sqlalchemy : 1.4.7
tables : 3.6.1
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : 1.3.0
numba : 0.53.1

@soilstack soilstack added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 10, 2021
@MarcoGorelli
Copy link
Member

MarcoGorelli commented Aug 10, 2021

Thanks @soilstack for the report

I'm getting the following from git bisect:

20003347f81d76b3cbaa8259b577e19f76603120 is the first bad commit
commit 20003347f81d76b3cbaa8259b577e19f76603120
Author: MeeseeksMachine <[email protected]>
Date:   Sun Sep 13 06:27:21 2020 -0700

    Backport PR #36147: REGR: Series access with Index of tuples/frozenset (#36332)
    
    Co-authored-by: Richard Shadrach <[email protected]>

cc @rhshadrach

#36332

@rhshadrach
Copy link
Member

Thanks @soilstack for the report! This is a duplicate of #42731, which already has a PR that is just about ready to fix. I applied the patch from there and got the result

Date
2020-12-12    1
2020-12-13    2
2020-12-14   -1
2020-12-15   -2
2020-12-16   -3
Name: (coz1, this_bar), dtype: int64

here, which agrees with the expected output. With any luck, we'll be able to get this in for 1.3.2.

Closing as a duplicate.

@rhshadrach rhshadrach added Duplicate Report Duplicate issue or pull request Groupby Regression Functionality that used to work in a prior pandas version Indexing Related to indexing on series/frames, not to indexes themselves and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 12, 2021
@soilstack
Copy link
Contributor Author

Thanks @soilstack for the report! This is a duplicate of #42731, which already has a PR that is just about ready to fix. I applied the patch from there and got the result

Date
2020-12-12    1
2020-12-13    2
2020-12-14   -1
2020-12-15   -2
2020-12-16   -3
Name: (coz1, this_bar), dtype: int64

here, which agrees with the expected output. With any luck, we'll be able to get this in for 1.3.2.

Closing as a duplicate.

Thanks for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Duplicate Report Duplicate issue or pull request Groupby 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

No branches or pull requests

3 participants