Skip to content

Commit b90a8fb

Browse files
phoflmroeschke
authored andcommitted
DEP: Deprecate passing fill_value and freq to shift (pandas-dev#54818)
1 parent bee0b68 commit b90a8fb

File tree

5 files changed

+39
-19
lines changed

5 files changed

+39
-19
lines changed

doc/source/whatsnew/v2.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@ Other Deprecations
591591
- Deprecated values ``"pad"``, ``"ffill"``, ``"bfill"``, ``"backfill"`` for :meth:`Series.interpolate` and :meth:`DataFrame.interpolate`, use ``obj.ffill()`` or ``obj.bfill()`` instead (:issue:`53581`)
592592
- Deprecated the behavior of :meth:`Index.argmax`, :meth:`Index.argmin`, :meth:`Series.argmax`, :meth:`Series.argmin` with either all-NAs and ``skipna=True`` or any-NAs and ``skipna=False`` returning -1; in a future version this will raise ``ValueError`` (:issue:`33941`, :issue:`33942`)
593593
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_sql` except ``name`` and ``con`` (:issue:`54229`)
594+
- Deprecated silently ignoring ``fill_value`` when passing both ``freq`` and ``fill_value`` to :meth:`DataFrame.shift`, :meth:`Series.shift` and :meth:`.DataFrameGroupBy.shift`; in a future version this will raise ``ValueError`` (:issue:`53832`)
594595

595596
.. ---------------------------------------------------------------------------
596597
.. _whatsnew_210.performance:
@@ -875,7 +876,6 @@ Other
875876
- Bug in :meth:`.DataFrameGroupBy.first`, :meth:`.DataFrameGroupBy.last`, :meth:`.SeriesGroupBy.first`, and :meth:`.SeriesGroupBy.last` where an empty group would return ``np.nan`` instead of the corresponding :class:`.ExtensionArray` NA value (:issue:`39098`)
876877
- Bug in :meth:`DataFrame.pivot_table` with casting the mean of ints back to an int (:issue:`16676`)
877878
- Bug in :meth:`DataFrame.reindex` with a ``fill_value`` that should be inferred with a :class:`ExtensionDtype` incorrectly inferring ``object`` dtype (:issue:`52586`)
878-
- 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`)
879879
- Bug in :meth:`DataFrame.shift` with ``axis=1`` on a :class:`DataFrame` with a single :class:`ExtensionDtype` column giving incorrect results (:issue:`53832`)
880880
- Bug in :meth:`Index.sort_values` when a ``key`` is passed (:issue:`52764`)
881881
- 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`)

pandas/core/frame.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -5636,10 +5636,14 @@ def shift(
56365636
) -> DataFrame:
56375637
if freq is not None and fill_value is not lib.no_default:
56385638
# GH#53832
5639-
raise ValueError(
5640-
"Cannot pass both 'freq' and 'fill_value' to "
5641-
f"{type(self).__name__}.shift"
5639+
warnings.warn(
5640+
"Passing a 'freq' together with a 'fill_value' silently ignores "
5641+
"the fill_value and is deprecated. This will raise in a future "
5642+
"version.",
5643+
FutureWarning,
5644+
stacklevel=find_stack_level(),
56425645
)
5646+
fill_value = lib.no_default
56435647

56445648
axis = self._get_axis_number(axis)
56455649

pandas/core/generic.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -10822,10 +10822,14 @@ def shift(
1082210822

1082310823
if freq is not None and fill_value is not lib.no_default:
1082410824
# GH#53832
10825-
raise ValueError(
10826-
"Cannot pass both 'freq' and 'fill_value' to "
10827-
f"{type(self).__name__}.shift"
10825+
warnings.warn(
10826+
"Passing a 'freq' together with a 'fill_value' silently ignores "
10827+
"the fill_value and is deprecated. This will raise in a future "
10828+
"version.",
10829+
FutureWarning,
10830+
stacklevel=find_stack_level(),
1082810831
)
10832+
fill_value = lib.no_default
1082910833

1083010834
if periods == 0:
1083110835
return self.copy(deep=None)

pandas/tests/frame/methods/test_shift.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,23 @@ def test_shift_axis1_with_valid_fill_value_one_array(self):
3232
expected2 = DataFrame([12345] * 5, dtype="Float64")
3333
tm.assert_frame_equal(res2, expected2)
3434

35-
def test_shift_disallow_freq_and_fill_value(self, frame_or_series):
35+
def test_shift_deprecate_freq_and_fill_value(self, frame_or_series):
3636
# Can't pass both!
3737
obj = frame_or_series(
3838
np.random.default_rng(2).standard_normal(5),
3939
index=date_range("1/1/2000", periods=5, freq="H"),
4040
)
4141

42-
msg = "Cannot pass both 'freq' and 'fill_value' to (Series|DataFrame).shift"
43-
with pytest.raises(ValueError, match=msg):
42+
msg = (
43+
"Passing a 'freq' together with a 'fill_value' silently ignores the "
44+
"fill_value"
45+
)
46+
with tm.assert_produces_warning(FutureWarning, match=msg):
4447
obj.shift(1, fill_value=1, freq="H")
4548

4649
if frame_or_series is DataFrame:
47-
with pytest.raises(ValueError, match=msg):
50+
obj.columns = date_range("1/1/2000", periods=1, freq="H")
51+
with tm.assert_produces_warning(FutureWarning, match=msg):
4852
obj.shift(1, axis=1, fill_value=1, freq="H")
4953

5054
@pytest.mark.parametrize(
@@ -716,8 +720,11 @@ def test_shift_with_iterable_freq_and_fill_value(self):
716720
df.shift(1, freq="H"),
717721
)
718722

719-
msg = r"Cannot pass both 'freq' and 'fill_value' to.*"
720-
with pytest.raises(ValueError, match=msg):
723+
msg = (
724+
"Passing a 'freq' together with a 'fill_value' silently ignores the "
725+
"fill_value"
726+
)
727+
with tm.assert_produces_warning(FutureWarning, match=msg):
721728
df.shift([1, 2], fill_value=1, freq="H")
722729

723730
def test_shift_with_iterable_check_other_arguments(self):

pandas/tests/groupby/test_groupby_shift_diff.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,14 @@ def test_shift_periods_freq():
166166
tm.assert_frame_equal(result, expected)
167167

168168

169-
def test_shift_disallow_freq_and_fill_value():
169+
def test_shift_deprecate_freq_and_fill_value():
170170
# GH 53832
171171
data = {"a": [1, 2, 3, 4, 5, 6], "b": [0, 0, 0, 1, 1, 1]}
172172
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):
173+
msg = (
174+
"Passing a 'freq' together with a 'fill_value' silently ignores the fill_value"
175+
)
176+
with tm.assert_produces_warning(FutureWarning, match=msg):
175177
df.groupby(df.index).shift(periods=-2, freq="D", fill_value="1")
176178

177179

@@ -238,12 +240,15 @@ def test_group_shift_with_multiple_periods_and_fill_value():
238240
tm.assert_frame_equal(shifted_df, expected_df)
239241

240242

241-
def test_group_shift_with_multiple_periods_and_both_fill_and_freq_fails():
243+
def test_group_shift_with_multiple_periods_and_both_fill_and_freq_deprecated():
242244
# GH#44424
243245
df = DataFrame(
244246
{"a": [1, 2, 3, 4, 5], "b": [True, True, False, False, True]},
245247
index=date_range("1/1/2000", periods=5, freq="H"),
246248
)
247-
msg = r"Cannot pass both 'freq' and 'fill_value' to.*"
248-
with pytest.raises(ValueError, match=msg):
249+
msg = (
250+
"Passing a 'freq' together with a 'fill_value' silently ignores the "
251+
"fill_value"
252+
)
253+
with tm.assert_produces_warning(FutureWarning, match=msg):
249254
df.groupby("b")[["a"]].shift([1, 2], fill_value=1, freq="H")

0 commit comments

Comments
 (0)