Skip to content

Commit 5b88827

Browse files
authored
DEPR: invalid fill_value in .shift (#62300)
1 parent 734f519 commit 5b88827

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,7 @@ Other Deprecations
661661
- Deprecated the ``arg`` parameter of ``Series.map``; pass the added ``func`` argument instead. (:issue:`61260`)
662662
- Deprecated using ``epoch`` date format in :meth:`DataFrame.to_json` and :meth:`Series.to_json`, use ``iso`` instead. (:issue:`57063`)
663663
- Deprecated allowing ``fill_value`` that cannot be held in the original dtype (excepting NA values for integer and bool dtypes) in :meth:`Series.unstack` and :meth:`DataFrame.unstack` (:issue:`12189`, :issue:`53868`)
664+
- Deprecated allowing ``fill_value`` that cannot be held in the original dtype (excepting NA values for integer and bool dtypes) in :meth:`Series.shift` and :meth:`DataFrame.shift` (:issue:`53802`)
664665

665666
.. ---------------------------------------------------------------------------
666667
.. _whatsnew_300.prior_deprecations:

pandas/core/internals/blocks.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,18 @@ def shift(self, periods: int, fill_value: Any = None) -> list[Block]:
14491449
fill_value,
14501450
)
14511451
except LossySetitemError:
1452+
if self.dtype.kind not in "iub" or not is_valid_na_for_dtype(
1453+
fill_value, self.dtype
1454+
):
1455+
# GH#53802
1456+
warnings.warn(
1457+
"shifting with a fill value that cannot be held by "
1458+
"original dtype is deprecated and will raise in a future "
1459+
"version. Explicitly cast to the desired dtype before "
1460+
"shifting instead.",
1461+
Pandas4Warning,
1462+
stacklevel=find_stack_level(),
1463+
)
14521464
nb = self.coerce_to_target_dtype(fill_value, raise_on_upcast=False)
14531465
return nb.shift(periods, fill_value=fill_value)
14541466

pandas/tests/frame/methods/test_shift.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import numpy as np
22
import pytest
33

4+
from pandas.errors import Pandas4Warning
5+
46
import pandas as pd
57
from pandas import (
68
CategoricalIndex,
@@ -766,3 +768,29 @@ def test_series_shift_interval_preserves_closed(self):
766768
result = ser.shift(1)
767769
expected = Series([np.nan, pd.Interval(1, 2, closed="right")])
768770
tm.assert_series_equal(result, expected)
771+
772+
def test_shift_invalid_fill_value_deprecation(self):
773+
# GH#53802
774+
df = DataFrame(
775+
{
776+
"a": [1, 2, 3],
777+
"b": [True, False, True],
778+
}
779+
)
780+
781+
msg = "shifting with a fill value that cannot"
782+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
783+
df.shift(1, fill_value="foo")
784+
785+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
786+
df["a"].shift(1, fill_value="foo")
787+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
788+
df["b"].shift(1, fill_value="foo")
789+
790+
# An incompatible null value
791+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
792+
df.shift(1, fill_value=NaT)
793+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
794+
df["a"].shift(1, fill_value=NaT)
795+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
796+
df["b"].shift(1, fill_value=NaT)

0 commit comments

Comments
 (0)