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.sum removed columns in case sequential calls of several groupby.sum #44132

Closed
3 tasks done
prutskov opened this issue Oct 21, 2021 · 2 comments
Closed
3 tasks done
Labels
Bug Closing Candidate May be closeable, needs more eyeballs Groupby Reduction Operations sum, mean, min, max, etc.

Comments

@prutskov
Copy link

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the master branch of pandas.

Reproducible Example

import pandas as pd
from pandas import util

df = util.testing.makeMixedDataFrame()
df = df.to_numpy()
df = pd.DataFrame(df)
df.columns = ["A", "B", "C", "D"]
df = df.set_index("B")

df1, df2 = df.iloc[:2], df.iloc[2:]

groupby1 = df1.groupby("B").sum()
groupby2 = df2.groupby("B").sum()

df_conc = pd.concat([groupby1, groupby2], axis=0)
print(f'\nConcatenated dataframe from partial results:\n{df_conc}')
print(f'\nOriginal dataframe:\n{df}')

conc_sum = df_conc.groupby(level=0).sum()
orig_sum = df.groupby(level=0).sum()

print(f'\nConcatenated df groupby.sum: \n{conc_sum}')
print(f'\nOriginal df groupby.sum: \n{orig_sum}')

Issue Description

Script output

Concatenated dataframe from partial results:
       A         C
B
0.0  0.0      foo1
1.0  1.0      foo2
0.0  6.0  foo3foo5
1.0  3.0      foo4

Original dataframe:
       A     C          D
B
0.0  0.0  foo1 2009-01-01
1.0  1.0  foo2 2009-01-02
0.0  2.0  foo3 2009-01-05
1.0  3.0  foo4 2009-01-06
0.0  4.0  foo5 2009-01-07

Concatenated df groupby.sum:
       A    # Column `C` is missed
B
0.0  6.0
1.0  4.0

Original df groupby.sum:
       A             C
B
0.0  6.0  foo1foo3foo5
1.0  4.0      foo2foo4

groupby.sum misses columns in case of splitting DataFrame on several parts and applying groupby.sum first for each part then for concatenation result of processed parts.

One observation: groupby.sum changed dtypes of partitions. If align dtypes of partitions with df.dtypes we can get expected behavior.

Expected Behavior

Expected results:

Concatenated df groupby.sum:
       A             C
B
0.0  6.0  foo1foo3foo5
1.0  4.0      foo2foo4

Original df groupby.sum:
       A             C
B
0.0  6.0  foo1foo3foo5
1.0  4.0      foo2foo4

Installed Versions

INSTALLED VERSIONS

commit : 73c6825
python : 3.8.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-65-generic
Version : #73-Ubuntu SMP Mon Jan 18 17:25:17 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US
LOCALE : en_US.ISO8859-1

pandas : 1.3.3
numpy : 1.21.0
pytz : 2021.1
dateutil : 2.8.1
pip : 21.1.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 : None
lxml.etree : 4.6.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 7.25.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : 2021.06.1
fastparquet : None
gcsfs : None
matplotlib : 3.2.2
numexpr : 2.7.3
odfpy : None
openpyxl : 3.0.7
pandas_gbq : 0.15.0
pyarrow : 4.0.1
pyxlsb : None
s3fs : 2021.06.1
scipy : 1.7.0
sqlalchemy : 1.4.20
tables : 3.6.1
tabulate : None
xarray : 0.18.2
xlrd : 2.0.1
xlwt : None
numba : None

@prutskov prutskov added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 21, 2021
@prutskov prutskov changed the title BUG: BUG: groupby.sum removed columns in case sequential calls of several groupby.sum Oct 21, 2021
@mroeschke mroeschke added Groupby Reduction Operations sum, mean, min, max, etc. and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 30, 2021
@jreback jreback added this to the Contributions Welcome milestone Jan 16, 2022
@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
@jbrockmendel
Copy link
Member

This works on main. Maybe could use a test (or closeable)

@jbrockmendel jbrockmendel added the Closing Candidate May be closeable, needs more eyeballs label Feb 2, 2023
@rhshadrach
Copy link
Member

This is tested as part of #46560

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Closing Candidate May be closeable, needs more eyeballs Groupby Reduction Operations sum, mean, min, max, etc.
Projects
None yet
5 participants