Skip to content

BUG: groupby.resample have inconsistent behavior when calling dataframe that's empty vs have entries #47705

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
3 tasks done
ahmedibrhm opened this issue Jul 13, 2022 · 5 comments · Fixed by #47672
Closed
3 tasks done
Labels

Comments

@ahmedibrhm
Copy link
Contributor

Pandas version checks

  • 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 main branch of pandas.

Reproducible Example

import pandas as pd
import datetime as dt

#constructing df2 that's empty

df = pd.DataFrame([(dt.date.today(), 13, 12)], columns=["date", "b", "count"])
df["date"] = pd.to_datetime(df["date"])
df = df[df["count"] == 1]
df2 = df.set_index('date').groupby(['b']).resample('M').sum()

#constructing df4 that is the same as df2 but has one entry

df3 = pd.DataFrame([(dt.date.today(), 13, 12)], columns=["date", "b", "count"])
df3["date"] = pd.to_datetime(df3["date"])
df4 = df3.set_index('date').groupby(['b']).resample('M').sum()

Issue Description

There's inconsistency when calling .groupby.resample on an empty dataframe versus dataframe that have entry. The frames of df2 and df4 are different as df4 has the index dates but it was dropped from df2.

df2:

    b    count
b                  
13 13       12

df4:

                b  count
b  date                 
13 2022-07-31  13     12

Expected Behavior

The two dataframes should have the date as an index

df2 should be as follows:

         b  count
b  date                 

df4 should be as follows:

                b  count
b  date                 
13 2022-07-31  13     12

Installed Versions

INSTALLED VERSIONS

commit : a4fca56
python : 3.9.6.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.22000
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 1.5.0.dev0+1164.ga4fca5637f
numpy : 1.22.4
pytz : 2022.1
dateutil : 2.8.2
setuptools : 56.0.0
pip : 21.1.3
Cython : 0.29.30
pytest : 7.1.2
hypothesis : 6.50.1
sphinx : 4.5.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.4.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : 2022.5.0
gcsfs : None
matplotlib : 3.5.2
numba : None
numexpr : 2.8.3
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 8.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

@mattB1989
Copy link
Contributor

I see the exact same behaviour for groupby().fillna - your PR seems specific to resample though. Happy to have a look if needed but I assume the change would be very similar. Is there a list of those aggregation that should be tested together (resample, fillna, etc.) ?

@ahmedibrhm
Copy link
Contributor Author

@mattB1989 I guess it's the wrap function in the operations that are applied to groupby

@ahmedibrhm
Copy link
Contributor Author

@mattB1989 What do you think?

@mattB1989
Copy link
Contributor

mattB1989 commented Jul 19, 2022

I'll see if I can submit a PR. The code path for mine seems different than the resample one you fixed. I've raise a separate issue in #47787

@mattB1989
Copy link
Contributor

Done here: #47840
Turns out I generalized to all "transforms" but resample is not part of that list, so your PR is still needed

@mroeschke mroeschke added Resample resample method Groupby and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 15, 2022
mroeschke pushed a commit that referenced this issue Oct 3, 2022
…lling it over empty df #47705 (#47672)

* DOC #45443 edited the documentation of where/mask functions

* DOC #45443 edited the documentation of where/mask functions

* Update generic.py

* Bug 43767 fix

* fixing lines doc

* visual indent

* visual indent

* indentation

* grouby.resample bug

* groubby.sample

* syntax

* syntax

* syntax

* syntax

* what's new

* flake8 error

* pytest

* blank line

* editting resample

* editting resample

* syntax

* syntax

* syntax

* space

* space

* space

* inplace

* spelling

* test

* test resampler

* tests

* tests

* Update resample.py

* Update resample.py

* Update resample.py

* Update v1.6.0.rst
noatamir pushed a commit to noatamir/pandas that referenced this issue Nov 9, 2022
…lling it over empty df pandas-dev#47705 (pandas-dev#47672)

* DOC pandas-dev#45443 edited the documentation of where/mask functions

* DOC pandas-dev#45443 edited the documentation of where/mask functions

* Update generic.py

* Bug 43767 fix

* fixing lines doc

* visual indent

* visual indent

* indentation

* grouby.resample bug

* groubby.sample

* syntax

* syntax

* syntax

* syntax

* what's new

* flake8 error

* pytest

* blank line

* editting resample

* editting resample

* syntax

* syntax

* syntax

* space

* space

* space

* inplace

* spelling

* test

* test resampler

* tests

* tests

* Update resample.py

* Update resample.py

* Update resample.py

* Update v1.6.0.rst
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants