Skip to content

BUG: groupby aggregation methods produce empty DataFrame with mixed DataTypes #43209

Closed
@johanneselbs

Description

@johanneselbs
  • [x ] I have checked that this issue has not already been reported.

  • [x ] 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

df = pd.DataFrame([[1, 2, 3, 4, 5, 6]]*3)
df.columns = pd.MultiIndex.from_product([['a', 'b'], ['i', 'j', 'k']])

print('all normal int:')
print(df.groupby(level=1, axis=1).sum())

for col in df.columns:
    if col[-1] == 'j':
        df[col] = df[col].astype('Int64')

print('mixed int:')
print(df.groupby(level=1, axis=1).sum())

for col in df.columns:
    df[col] = df[col].astype('Int64')

print('all nullable int:')
print(df.groupby(level=1, axis=1).sum())

Problem description

If columns have mixed datatypes, in the example partially normal ints and partially nullable ints, several aggregation methods in the example .sum(), of the DataFrameGroupBy objects unexpectedly return empty DataFrames. This behavior was not yet present in version 1.2.5.

Expected Output

Output should be the same for mixed and non mixed

Output of pd.show_versions()

INSTALLED VERSIONS

commit : c7f7443
python : 3.8.6.final.0
python-bits : 64
OS : Linux
OS-release : 3.10.0-1127.19.1.el7.x86_64
Version : #1 SMP Tue Aug 11 19:12:04 EDT 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C
LOCALE : None.None

pandas : 1.3.1
numpy : 1.21.1
pytz : 2021.1
dateutil : 2.8.2
pip : 21.2.3
setuptools : 52.0.0.post20210125
Cython : None
pytest : 6.2.4
hypothesis : None
sphinx : 4.1.2
blosc : None
feather : 0.4.1
xlsxwriter : 1.4.5
lxml.etree : 4.6.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 7.26.0
pandas_datareader: None
bs4 : None
bottleneck : 1.3.2
fsspec : 2021.07.0
fastparquet : None
gcsfs : None
matplotlib : 3.2.0
numexpr : 2.7.3
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : 4.0.0
pyxlsb : None
s3fs : None
scipy : 1.7.1
sqlalchemy : 1.4.22
tables : 3.6.1
tabulate : None
xarray : 0.19.0
xlrd : 2.0.1
xlwt : 1.3.0
numba : 0.53.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    ApplyApply, Aggregate, Transform, MapBugDtype ConversionsUnexpected or buggy dtype conversionsGroupbyMultiIndexReduction Operationssum, mean, min, max, etc.RegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions