Skip to content

Commit 049a6fa

Browse files
committed
DEP: Deprecate passing fill_value and freq to shift
1 parent 0a23624 commit 049a6fa

File tree

5 files changed

+26
-14
lines changed

5 files changed

+26
-14
lines changed

doc/source/whatsnew/v2.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ Other Deprecations
587587
- Deprecated values ``"pad"``, ``"ffill"``, ``"bfill"``, ``"backfill"`` for :meth:`Series.interpolate` and :meth:`DataFrame.interpolate`, use ``obj.ffill()`` or ``obj.bfill()`` instead (:issue:`53581`)
588588
- 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`)
589589
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_sql` except ``name`` and ``con`` (:issue:`54229`)
590+
- Deprecated silently ignoring ``fill_value`` when passing both ``freq`` and ``fill_value`` to :meth:`DataFrame.shift` and :meth:`Series.shift` and :meth:`.DataFrameGroupBy.shift`; in a future version this will raise ``ValueError`` (:issue:`53832`)
590591

591592
.. ---------------------------------------------------------------------------
592593
.. _whatsnew_210.performance:
@@ -871,7 +872,6 @@ Other
871872
- 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`)
872873
- Bug in :meth:`DataFrame.pivot_table` with casting the mean of ints back to an int (:issue:`16676`)
873874
- Bug in :meth:`DataFrame.reindex` with a ``fill_value`` that should be inferred with a :class:`ExtensionDtype` incorrectly inferring ``object`` dtype (:issue:`52586`)
874-
- 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`)
875875
- Bug in :meth:`DataFrame.shift` with ``axis=1`` on a :class:`DataFrame` with a single :class:`ExtensionDtype` column giving incorrect results (:issue:`53832`)
876876
- Bug in :meth:`Index.sort_values` when a ``key`` is passed (:issue:`52764`)
877877
- 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

+6-3
Original file line numberDiff line numberDiff line change
@@ -5636,10 +5636,13 @@ 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. This will raise in a future version.",
5642+
FutureWarning,
5643+
stacklevel=find_stack_level(),
56425644
)
5645+
fill_value = lib.no_default
56435646

56445647
axis = self._get_axis_number(axis)
56455648

pandas/core/generic.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -10822,10 +10822,13 @@ 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. This will raise in a future version.",
10828+
FutureWarning,
10829+
stacklevel=find_stack_level(),
1082810830
)
10831+
fill_value = lib.no_default
1082910832

1083010833
if periods == 0:
1083110834
return self.copy(deep=None)

pandas/tests/frame/methods/test_shift.py

+8-4
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+
frame_or_series.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(

pandas/tests/groupby/test_groupby_shift_diff.py

+5-3
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

0 commit comments

Comments
 (0)