Skip to content

Commit c92b370

Browse files
authored
BUG: groupby shift fill_value, freq followup (#54133)
1 parent 6e7025d commit c92b370

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

doc/source/whatsnew/v2.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ Other
618618
- Bug in :func:`assert_almost_equal` now throwing assertion error for two unequal sets (:issue:`51727`)
619619
- Bug in :func:`assert_frame_equal` checks category dtypes even when asked not to check index type (:issue:`52126`)
620620
- Bug in :meth:`DataFrame.reindex` with a ``fill_value`` that should be inferred with a :class:`ExtensionDtype` incorrectly inferring ``object`` dtype (:issue:`52586`)
621-
- 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`)
621+
- 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`)
622622
- Bug in :meth:`DataFrame.shift` with ``axis=1`` on a :class:`DataFrame` with a single :class:`ExtensionDtype` column giving incorrect results (:issue:`53832`)
623623
- 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`)
624624
- 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`)

pandas/core/groupby/groupby.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -4911,7 +4911,7 @@ def shift(
49114911
periods: int = 1,
49124912
freq=None,
49134913
axis: Axis | lib.NoDefault = lib.no_default,
4914-
fill_value=None,
4914+
fill_value=lib.no_default,
49154915
):
49164916
"""
49174917
Shift each group by periods observations.
@@ -4934,6 +4934,9 @@ def shift(
49344934
fill_value : optional
49354935
The scalar value to use for newly introduced missing values.
49364936
4937+
.. versionchanged:: 2.1.0
4938+
Will raise a ``ValueError`` if ``freq`` is provided too.
4939+
49374940
Returns
49384941
-------
49394942
Series or DataFrame
@@ -4991,6 +4994,8 @@ def shift(
49914994
f = lambda x: x.shift(periods, freq, axis, fill_value)
49924995
return self._python_apply_general(f, self._selected_obj, is_transform=True)
49934996

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

pandas/tests/groupby/test_groupby_shift_diff.py

+19
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Series,
88
Timedelta,
99
Timestamp,
10+
date_range,
1011
)
1112
import pandas._testing as tm
1213

@@ -154,3 +155,21 @@ def test_multindex_empty_shift_with_fill():
154155
shifted_with_fill = df.groupby(["a", "b"]).shift(1, fill_value=0)
155156
tm.assert_frame_equal(shifted, shifted_with_fill)
156157
tm.assert_index_equal(shifted.index, shifted_with_fill.index)
158+
159+
160+
def test_shift_periods_freq():
161+
# GH 54093
162+
data = {"a": [1, 2, 3, 4, 5, 6], "b": [0, 0, 0, 1, 1, 1]}
163+
df = DataFrame(data, index=date_range(start="20100101", periods=6))
164+
result = df.groupby(df.index).shift(periods=-2, freq="D")
165+
expected = DataFrame(data, index=date_range(start="2009-12-30", periods=6))
166+
tm.assert_frame_equal(result, expected)
167+
168+
169+
def test_shift_disallow_freq_and_fill_value():
170+
# GH 53832
171+
data = {"a": [1, 2, 3, 4, 5, 6], "b": [0, 0, 0, 1, 1, 1]}
172+
df = DataFrame(data, index=date_range(start="20100101", periods=6))
173+
msg = "Cannot pass both 'freq' and 'fill_value' to (Series|DataFrame).shift"
174+
with pytest.raises(ValueError, match=msg):
175+
df.groupby(df.index).shift(periods=-2, freq="D", fill_value="1")

0 commit comments

Comments
 (0)