Skip to content

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

Closed
@soilstack

Description

@soilstack
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDuplicate ReportDuplicate issue or pull requestGroupbyIndexingRelated 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

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions