Skip to content

BUG: resampling empty dataframe under specific circumstance leads to Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex' #60042

@xytxytxyt

Description

@xytxytxyt

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
subdf = pd.DataFrame({'My Column 1': []}, index=pd.to_datetime([], unit='s', utc=True))
df = pd.DataFrame()
df = df.join(subdf, how='outer')
df = df.resample("H").asfreq()

# output:
'''
<stdin>:1: FutureWarning: 'H' is deprecated and will be removed in a future version, please use 'h' instead.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/xytxytxyt/.pyenv/versions/temp/lib/python3.11/site-packages/pandas/core/generic.py", line 9771, in resample
    return get_resampler(
           ^^^^^^^^^^^^^^
  File "/Users/xytxytxyt/.pyenv/versions/temp/lib/python3.11/site-packages/pandas/core/resample.py", line 2050, in get_resampler
    return tg._get_resampler(obj, kind=kind)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/xytxytxyt/.pyenv/versions/temp/lib/python3.11/site-packages/pandas/core/resample.py", line 2272, in _get_resampler
    raise TypeError(
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex'
'''

Issue Description

see the example

interestingly, this does not happen without the join():

import pandas as pd
df = pd.DataFrame({'My Column 1': []}, index=pd.to_datetime([], unit='s', utc=True))
df = df.resample("H").asfreq()

# output:
# <stdin>:1: FutureWarning: 'H' is deprecated and will be removed in a future version, please use 'h' instead.

Expected Behavior

no error is raised

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.11.8
python-bits : 64
OS : Darwin
OS-release : 22.6.0
Version : Darwin Kernel Version 22.6.0: Mon Apr 22 20:50:39 PDT 2024; root:xnu-8796.141.3.705.2~1/RELEASE_ARM64_T8103
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.3
numpy : 2.1.2
pytz : 2024.2
dateutil : 2.9.0.post0
pip : 24.2
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2024.2
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions