Skip to content
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

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

Closed
1 task
johanneselbs opened this issue Aug 25, 2021 · 2 comments · Fixed by #43213
Closed
1 task

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

johanneselbs opened this issue Aug 25, 2021 · 2 comments · Fixed by #43213
Labels
Apply Apply, Aggregate, Transform, Map Bug Dtype Conversions Unexpected or buggy dtype conversions Groupby MultiIndex Reduction Operations sum, mean, min, max, etc. Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@johanneselbs
Copy link

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

@johanneselbs johanneselbs added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 25, 2021
@simonjayhawkins
Copy link
Member

Thanks @johanneselbs for the report.

This behavior was not yet present in version 1.2.5.

the result on 1.2.5 was not completely correct. the dtype of the result seems iffy.

mixed int:
   i    j  k
0  5  7.0  9
1  5  7.0  9
2  5  7.0  9

dtypes

i      int64
j    float64
k      int64
dtype: object

will label as a regression for now pending further investigation.

@simonjayhawkins simonjayhawkins added Regression Functionality that used to work in a prior pandas version and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 25, 2021
@simonjayhawkins simonjayhawkins added this to the 1.3.3 milestone Aug 25, 2021
@simonjayhawkins simonjayhawkins added Apply Apply, Aggregate, Transform, Map Dtype Conversions Unexpected or buggy dtype conversions Groupby MultiIndex Reduction Operations sum, mean, min, max, etc. labels Aug 25, 2021
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Aug 25, 2021
@simonjayhawkins
Copy link
Member

first bad commit: [6b94e24] BUG: DataFrameGroupBy with numeric_only and empty non-numeric data (#41706)

This is the same commit that caused the regression in #42395

cc @jbrockmendel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform, Map Bug Dtype Conversions Unexpected or buggy dtype conversions Groupby MultiIndex Reduction Operations sum, mean, min, max, etc. Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants