Skip to content

get_group(...) fails for groupby(...) based on a function #22257

Closed
@Kimonode

Description

@Kimonode

Code Sample, a copy-pastable example if possible

"""
IMPORTANT: The issue happens with pandas==0.21.1 but does NOT happen with pandas==0.19
"""
# Define simple dataframe, from Pandas doc ;)
# http://pandas.pydata.org/pandas-docs/stable/groupby.html#groupby-sorting
df3 = pd.DataFrame({'X' : ['A', 'B', 'A', 'B'], 'Y' : [1, 4, 3, 2]})

# Define a simple function for grouping
def func(df, index):
    return (df['X'].loc[index], df['Y'].loc[index] % 2)

# Make sure the groupby worked, and check the group names
for name, df in df3.groupby(by=lambda index: func(df3, index), sort=False):
    print(name)
    print(df)

# Execute the code that triggers an error
df3.groupby(by=lambda index: func(df3, index), sort=False).get_group(('A', 1))

Problem description

The code raises: NotImplementedError: isna is not defined for MultiIndex
Note: That issue happens with pandas==0.21.1. It does NOT happen with pandas==0.19.

Expected Output

df3.groupby(...).get_group(('A', 1)) should not raise an error but return the group ('A', 1):

('A', 1)
   X  Y
0  A  1
2  A  3

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.6.5.final.0 python-bits: 64 OS: Linux OS-release: 4.15.0-30-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: fr_FR.UTF-8 LOCALE: fr_FR.UTF-8

pandas: 0.21.1
pytest: None
pip: 9.0.1
setuptools: 39.0.1
Cython: None
numpy: 1.13.3
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 6.5.0
sphinx: None
patsy: None
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: 1.1.0
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.999999999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions