Skip to content

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

Closed
@j-bennet

Description

@j-bennet

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDatetimeDatetime data dtypeIndexRelated to the Index class or subclasses

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions