Skip to content
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ Other Deprecations
- Deprecated the ``arg`` parameter of ``Series.map``; pass the added ``func`` argument instead. (:issue:`61260`)
- Deprecated using ``epoch`` date format in :meth:`DataFrame.to_json` and :meth:`Series.to_json`, use ``iso`` instead. (:issue:`57063`)
- 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`)
- 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`)

.. ---------------------------------------------------------------------------
.. _whatsnew_300.prior_deprecations:
Expand Down
12 changes: 12 additions & 0 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,18 @@ def shift(self, periods: int, fill_value: Any = None) -> list[Block]:
fill_value,
)
except LossySetitemError:
if self.dtype.kind not in "iub" or not is_valid_na_for_dtype(
fill_value, self.dtype
):
# GH#53802
warnings.warn(
"shifting with a fill value that cannot be held by "
"original dtype is deprecated and will raise in a future "
"version. Explicitly cast to the desired dtype before "
"shifting instead.",
Pandas4Warning,
stacklevel=find_stack_level(),
)
nb = self.coerce_to_target_dtype(fill_value, raise_on_upcast=False)
return nb.shift(periods, fill_value=fill_value)

Expand Down
28 changes: 28 additions & 0 deletions pandas/tests/frame/methods/test_shift.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import numpy as np
import pytest

from pandas.errors import Pandas4Warning

import pandas as pd
from pandas import (
CategoricalIndex,
Expand Down Expand Up @@ -766,3 +768,29 @@ def test_series_shift_interval_preserves_closed(self):
result = ser.shift(1)
expected = Series([np.nan, pd.Interval(1, 2, closed="right")])
tm.assert_series_equal(result, expected)

def test_shift_invalid_fill_value_deprecation(self):
# GH#53802
df = DataFrame(
{
"a": [1, 2, 3],
"b": [True, False, True],
}
)

msg = "shifting with a fill value that cannot"
with tm.assert_produces_warning(Pandas4Warning, match=msg):
df.shift(1, fill_value="foo")

with tm.assert_produces_warning(Pandas4Warning, match=msg):
df["a"].shift(1, fill_value="foo")
with tm.assert_produces_warning(Pandas4Warning, match=msg):
df["b"].shift(1, fill_value="foo")

# An incompatible null value
with tm.assert_produces_warning(Pandas4Warning, match=msg):
df.shift(1, fill_value=NaT)
with tm.assert_produces_warning(Pandas4Warning, match=msg):
df["a"].shift(1, fill_value=NaT)
with tm.assert_produces_warning(Pandas4Warning, match=msg):
df["b"].shift(1, fill_value=NaT)