Skip to content

BUG: groupby / resample / aggregate produces wrong results #35173

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

Closed
2 of 3 tasks
roy2006 opened this issue Jul 8, 2020 · 2 comments
Closed
2 of 3 tasks

BUG: groupby / resample / aggregate produces wrong results #35173

roy2006 opened this issue Jul 8, 2020 · 2 comments
Labels
Bug Duplicate Report Duplicate issue or pull request Groupby

Comments

@roy2006
Copy link

roy2006 commented Jul 8, 2020

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

  • 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

data = """        date  revenue name
0 2020-02-24        9  n_1
1 2020-05-12        8  n_2
2 2020-03-28        9  n_2
3 2020-01-14        2  n_0"""

df = pd.read_csv(StringIO(data), sep = "\s+", engine = "python")
df.date = pd.to_datetime(df.date)     

gb = df.groupby("name").resample("M", on="date")

res1 = gb.sum()[['revenue']]

# ==>
#                  revenue
# name date               
# n_0  2020-01-31        2
# n_1  2020-02-29        9
# n_2  2020-03-31        9
#      2020-04-30        0
#      2020-05-31        8

res2 = gb.aggregate({'revenue':'sum'})

# ==>
#                  revenue
# name date               
# n_0  2020-05-31        2
# n_1  2020-01-31        9
# n_2  2020-02-29        8
#      2020-03-31        9

Problem description

As the code sample above shows, there are two issues:

  • agg(sum) produces different results from just sum.
  • The results of agg(sum) are plain wrong. The name "n_2" has no data in Feb, and yet - the aggregation shows that it does.

Expected Output

The correct output is the one produced by sum.

Output of pd.show_versions()

INSTALLED VERSIONS

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

pandas : 1.0.5
numpy : 1.17.4
pytz : 2019.3
dateutil : 2.8.0
pip : 19.3.1
setuptools : 41.6.0
Cython : None
pytest : 5.4.3
hypothesis : 5.18.0
sphinx : None
blosc : None
feather : None
xlsxwriter : 1.2.9
lxml.etree : 4.5.0
html5lib : 1.0.1
pymysql : None
psycopg2 : 2.8.4 (dt dec pq3 ext lo64)
jinja2 : 2.10.3
IPython : 7.10.1
pandas_datareader: 0.8.1
bs4 : 4.9.0
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.5.0
matplotlib : 3.2.1
numexpr : None
odfpy : None
openpyxl : 3.0.3
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 5.4.3
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.16
tables : None
tabulate : None
xarray : 0.15.1
xlrd : 1.2.0
xlwt : None
xlsxwriter : 1.2.9
numba : None

@roy2006 roy2006 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 8, 2020
@MarcoGorelli MarcoGorelli added Groupby and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 9, 2020
@MarcoGorelli
Copy link
Member

Thanks @roy2006 for the excellent bug report!

@simonjayhawkins
Copy link
Member

Thanks @roy2006 this looks like a duplicate of #33548 so closing. lmk if i'm overlooking something.

@simonjayhawkins simonjayhawkins added the Duplicate Report Duplicate issue or pull request label Jul 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Duplicate Report Duplicate issue or pull request Groupby
Projects
None yet
Development

No branches or pull requests

3 participants