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: Frequency shift on empty DataFrame doesn't shift index #60102

Closed
3 tasks done
colinc719 opened this issue Oct 24, 2024 · 2 comments · Fixed by #60172
Closed
3 tasks done

BUG: Frequency shift on empty DataFrame doesn't shift index #60102

colinc719 opened this issue Oct 24, 2024 · 2 comments · Fixed by #60172
Labels
Bug Datetime Datetime data dtype good first issue Transformations e.g. cumsum, diff, rank

Comments

@colinc719
Copy link

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

dates = pd.date_range('2020-01-01', '2020-01-03')
df = pd.DataFrame(index=dates, columns=[])
day = pd.offsets.Day()
shifted_dates = df.shift(freq=day).index
assert (shifted_dates == dates + day).all()

Issue Description

Running DataFrame.shift with freq on a DataFrame with empty columns does not shift the index.

Expected Behavior

I would expect DataFrame.shift to shift the index similarly to how it would if the DataFrame had columns. Particularly with the above example, I would expect df.reindex(['A'], axis=1).shift(freq=day).reindex([], axis=1) to be equivalent to df.shift(freq=day).

Installed Versions

INSTALLED VERSIONS

commit : 91111fd
python : 3.10.13.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.102.hrtdev
Version : #1 SMP Tue Mar 14 13:23:09 EDT 2023
machine : x86_64
processor :
byteorder : little
LC_ALL :
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.1
numpy : 1.23.2
pytz : 2022.1
dateutil : 2.8.2
setuptools : 62.1.0
pip : 24.2
Cython : 0.29.30
pytest : 8.2.1
hypothesis : 6.108.2
sphinx : 5.0.2
blosc : 1.10.6
feather : None
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : 1.1
pymysql : 1.0.2
psycopg2 : 2.9.3
jinja2 : 3.1.2
IPython : 8.10.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : 1.3.5
brotli : 1.0.9
fastparquet : 0.8.1
fsspec : 2023.10.0
gcsfs : None
matplotlib : 3.5.2+hrt1
numba : 0.59.0
numexpr : 2.8.3
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : 12.2.2.dev14+g99c7fc95e
pyreadstat : None
pyxlsb : 1.0.9
s3fs : None
scipy : 1.9.1
snappy : None
sqlalchemy : 1.4.49
tables : 3.7.0
tabulate : 0.8.10
xarray : 2022.6.0
xlrd : 2.0.1
xlwt : None
zstandard : 0.20.0
tzdata : 2022.1

@colinc719 colinc719 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 24, 2024
@rhshadrach
Copy link
Member

Thanks for the report! Agreed we should be modifying the index of an empty DataFrame here. It appears that modifying this line:

if self.empty:

to be if self.empty and freq is None: will resolve this. Further investigations and PRs to fix are welcome!

@rhshadrach rhshadrach added Transformations e.g. cumsum, diff, rank Datetime Datetime data dtype good first issue and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 2, 2024
@lfffkh
Copy link
Contributor

lfffkh commented Nov 2, 2024

Thanks for the report! Agreed we should be modifying the index of an empty DataFrame here. It appears that modifying this line:

if self.empty:

to be if self.empty and freq is None: will resolve this. Further investigations and PRs to fix are welcome!

Hi, I've tried this before. I am new to the community, thanks for some tips you gave, I would like to continue to delve into this issue, can I take this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype good first issue Transformations e.g. cumsum, diff, rank
Projects
None yet
3 participants