Skip to content

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

Closed
@ahmedibrhm

Description

@ahmedibrhm

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions