Skip to content

Groupby with function returning a tuple fails if index has a name #18696

Closed
@achabotl

Description

@achabotl

Code Sample

import pandas as pd
print(pd.__version__)
idx = pd.date_range('2017-12-08', periods=6, freq='10D')
df = pd.DataFrame(np.arange(6), index=idx)
df.index.name = 'name'
df.groupby(lambda x: (x.year, x.month)).mean()

Problem description

In 0.21.0 (and 0.20.x), calling groupy with a function that returns a tuple on a DataFrame with a named index fails with ValueError: Names should be list-like for a MultiIndex. It seems to be caused by the index having a name.

The sample works in 0.19.0, with the output below. The same code without df.index.name = 'name' works under 0.20 and 0.21.

Expected Output

            0
(2017, 12)  1
(2018, 1)   4

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.6.3.final.0 python-bits: 64 OS: Darwin OS-release: 16.7.0 machine: x86_64 processor: i386 byteorder: little LC_ALL: en_US.UTF-8 LANG: en_US.UTF-8 LOCALE: en_US.UTF-8

pandas: 0.21.0
pytest: None
pip: 9.0.1
setuptools: 38.2.4
Cython: 0.27.3
numpy: 1.13.3
scipy: None
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.1.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions