Skip to content

BUG: Cannot pass both 'freq' and 'fill_value' to DataFrame.shift #54093

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
j-bennet opened this issue Jul 12, 2023 · 4 comments · Fixed by #54133
Closed
3 tasks done

BUG: Cannot pass both 'freq' and 'fill_value' to DataFrame.shift #54093

j-bennet opened this issue Jul 12, 2023 · 4 comments · Fixed by #54133
Labels
Bug Datetime Datetime data dtype Index Related to the Index class or subclasses
Milestone

Comments

@j-bennet
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

import pandas as pd
df = pd.DataFrame(dict(a=[1, 2, 3, 4, 5, 6], b=[0, 0, 0, 1, 1, 1]), index=pd.date_range(start="20100101", periods=6))
res = df.groupby(pdf.index).shift(periods=-2, freq="D")

Issue Description

Produces an error:

ValueError                                Traceback (most recent call last)
<ipython-input-3-a9b8cc2ef441> in ?()
----> 1 pdf.groupby(pdf.index).shift(periods=-2, freq="D")

~/mambaforge/envs/dask-ci/lib/python3.11/site-packages/pandas/core/groupby/groupby.py in ?(self, periods, freq, axis, fill_value)
   4942             axis = 0
   4943
   4944         if freq is not None or axis != 0:
   4945             f = lambda x: x.shift(periods, freq, axis, fill_value)
-> 4946             return self._python_apply_general(f, self._selected_obj, is_transform=True)
   4947
   4948         ids, _, ngroups = self.grouper.group_info
   4949         res_indexer = np.zeros(len(ids), dtype=np.int64)

~/mambaforge/envs/dask-ci/lib/python3.11/site-packages/pandas/core/groupby/groupby.py in ?(self, f, data, not_indexed_same, is_transform, is_agg)
   1762         -------
   1763         Series or DataFrame
   1764             data after applying f
   1765         """
-> 1766         values, mutated = self.grouper.apply_groupwise(f, data, self.axis)
   1767         if not_indexed_same is None:
   1768             not_indexed_same = mutated
   1769

~/mambaforge/envs/dask-ci/lib/python3.11/site-packages/pandas/core/groupby/ops.py in ?(self, f, data, axis)
    901             object.__setattr__(group, "name", key)
    902
    903             # group might be modified
    904             group_axes = group.axes
--> 905             res = f(group)
    906             if not mutated and not _is_indexed_like(res, group_axes, axis):
    907                 mutated = True
    908             result_values.append(res)

~/mambaforge/envs/dask-ci/lib/python3.11/site-packages/pandas/core/groupby/groupby.py in ?(x)
-> 4945             f = lambda x: x.shift(periods, freq, axis, fill_value)

~/mambaforge/envs/dask-ci/lib/python3.11/site-packages/pandas/core/frame.py in ?(self, periods, freq, axis, fill_value)
   5499         axis = self._get_axis_number(axis)
   5500
   5501         if freq is not None and fill_value is not lib.no_default:
   5502             # GH#53832
-> 5503             raise ValueError(
   5504                 "Cannot pass both 'freq' and 'fill_value' to "
   5505                 f"{type(self).__name__}.shift"
   5506             )

ValueError: Cannot pass both 'freq' and 'fill_value' to DataFrame.shift

Expected Behavior

Should not raise an error.

Installed Versions

INSTALLED VERSIONS

commit : b4929f8
python : 3.11.4.final.0
python-bits : 64
OS : Darwin
OS-release : 22.5.0
Version : Darwin Kernel Version 22.5.0: Mon Apr 24 20:53:19 PDT 2023; root:xnu-8796.121.2~5/RELEASE_ARM64_T6020
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.1.0.dev0+1173.gb4929f8536
numpy : 1.24.4
pytz : 2023.3
dateutil : 2.8.2
setuptools : 67.7.2
pip : 23.1.2
Cython : 0.29.33
pytest : 7.4.0
hypothesis : 6.78.1
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.14.0
pandas_datareader: None
bs4 : 4.12.2
bottleneck : None
brotli :
fastparquet : 2023.7.0
fsspec : 2023.6.0
gcsfs : None
matplotlib : None
numba : 0.57.1
numexpr : 2.8.4
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 12.0.1
pyreadstat : None
pyxlsb : None
s3fs : 0.4.2
scipy : 1.11.1
snappy :
sqlalchemy : 2.0.18
tables : 3.8.0
tabulate : None
xarray : 2023.6.0
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

@j-bennet j-bennet added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 12, 2023
@lithomas1
Copy link
Member

#53832 is the relevant PR.

cc @jbrockmendel

@lithomas1 lithomas1 added Datetime Datetime data dtype Index Related to the Index class or subclasses labels Jul 12, 2023
@jbrockmendel
Copy link
Member

Before #53832, passing both freq and fill_value would cause one to be silently ignored (off the top of my head I dont remember which, guessing fill_value). now it raises instead of ignoring. just pass one of them.

@mroeschke
Copy link
Member

@jbrockmendel in #53832, looks like the fill_value arg for groupby shift needed to be changed too

@j-bennet
Copy link
Author

Before #53832, passing both freq and fill_value would cause one to be silently ignored (off the top of my head I dont remember which, guessing fill_value). now it raises instead of ignoring. just pass one of them.

I'm only passing freq, not fill_value - see the code snippet.

@lithomas1 lithomas1 removed the Needs Triage Issue that has not been reviewed by a pandas team member label Jul 17, 2023
@rhshadrach rhshadrach added this to the 2.1 milestone Jul 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype Index Related to the Index class or subclasses
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants