Skip to content

BUG: groupby shift fill_value, freq followup #54133

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

Merged
merged 6 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ Other
- Bug in :func:`assert_almost_equal` now throwing assertion error for two unequal sets (:issue:`51727`)
- Bug in :func:`assert_frame_equal` checks category dtypes even when asked not to check index type (:issue:`52126`)
- Bug in :meth:`DataFrame.reindex` with a ``fill_value`` that should be inferred with a :class:`ExtensionDtype` incorrectly inferring ``object`` dtype (:issue:`52586`)
- Bug in :meth:`DataFrame.shift` and :meth:`Series.shift` when passing both "freq" and "fill_value" silently ignoring "fill_value" instead of raising ``ValueError`` (:issue:`53832`)
- Bug in :meth:`DataFrame.shift` and :meth:`Series.shift` and :meth:`DataFrameGroupBy.shift` when passing both "freq" and "fill_value" silently ignoring "fill_value" instead of raising ``ValueError`` (:issue:`53832`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this restriction be added to the docstrings?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Added

- Bug in :meth:`DataFrame.shift` with ``axis=1`` on a :class:`DataFrame` with a single :class:`ExtensionDtype` column giving incorrect results (:issue:`53832`)
- Bug in :meth:`Series.align`, :meth:`DataFrame.align`, :meth:`Series.reindex`, :meth:`DataFrame.reindex`, :meth:`Series.interpolate`, :meth:`DataFrame.interpolate`, incorrectly failing to raise with method="asfreq" (:issue:`53620`)
- Bug in :meth:`Series.map` when giving a callable to an empty series, the returned series had ``object`` dtype. It now keeps the original dtype (:issue:`52384`)
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -4911,7 +4911,7 @@ def shift(
periods: int = 1,
freq=None,
axis: Axis | lib.NoDefault = lib.no_default,
fill_value=None,
fill_value=lib.no_default,
):
"""
Shift each group by periods observations.
Expand All @@ -4934,6 +4934,9 @@ def shift(
fill_value : optional
The scalar value to use for newly introduced missing values.

.. versionchanged:: 2.1.0
Will raise a ``ValueError`` if ``freq`` is provided too.

Returns
-------
Series or DataFrame
Expand Down Expand Up @@ -4991,6 +4994,8 @@ def shift(
f = lambda x: x.shift(periods, freq, axis, fill_value)
return self._python_apply_general(f, self._selected_obj, is_transform=True)

if fill_value is lib.no_default:
fill_value = None
ids, _, ngroups = self.grouper.group_info
res_indexer = np.zeros(len(ids), dtype=np.int64)

Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/groupby/test_groupby_shift_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Series,
Timedelta,
Timestamp,
date_range,
)
import pandas._testing as tm

Expand Down Expand Up @@ -154,3 +155,21 @@ def test_multindex_empty_shift_with_fill():
shifted_with_fill = df.groupby(["a", "b"]).shift(1, fill_value=0)
tm.assert_frame_equal(shifted, shifted_with_fill)
tm.assert_index_equal(shifted.index, shifted_with_fill.index)


def test_shift_periods_freq():
# GH 54093
data = {"a": [1, 2, 3, 4, 5, 6], "b": [0, 0, 0, 1, 1, 1]}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GH ref

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks added

df = DataFrame(data, index=date_range(start="20100101", periods=6))
result = df.groupby(df.index).shift(periods=-2, freq="D")
expected = DataFrame(data, index=date_range(start="2009-12-30", periods=6))
tm.assert_frame_equal(result, expected)


def test_shift_disallow_freq_and_fill_value():
# GH 53832
data = {"a": [1, 2, 3, 4, 5, 6], "b": [0, 0, 0, 1, 1, 1]}
df = DataFrame(data, index=date_range(start="20100101", periods=6))
msg = "Cannot pass both 'freq' and 'fill_value' to (Series|DataFrame).shift"
with pytest.raises(ValueError, match=msg):
df.groupby(df.index).shift(periods=-2, freq="D", fill_value="1")