Skip to content

ENH: Enabled prefix, suffix, and sep to DataFrame.shift #61710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5866,7 +5866,9 @@ def shift(
freq: Frequency | None = None,
axis: Axis = 0,
fill_value: Hashable = lib.no_default,
prefix: str | None = None,
suffix: str | None = None,
sep: str ="_",
) -> DataFrame:
if freq is not None and fill_value is not lib.no_default:
# GH#53832
Expand Down Expand Up @@ -5899,11 +5901,17 @@ def shift(
shifted_dataframes.append(
super()
.shift(periods=period, freq=freq, axis=axis, fill_value=fill_value)
.add_prefix(f"{prefix}{sep}" if prefix else "")
.add_suffix(f"{suffix}_{period}" if suffix else f"_{period}")
.rename(
columns=lambda col: col.replace(f"{sep}{sep}", sep)
)
)
return concat(shifted_dataframes, axis=1)
elif suffix:
raise ValueError("Cannot specify `suffix` if `periods` is an int.")
elif prefix:
raise ValueError("Cannot specify `prefix` if `periods` is an int.")
periods = cast(int, periods)

ncols = len(self.columns)
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10112,13 +10112,17 @@ def mask(
)

@doc(klass=_shared_doc_kwargs["klass"])
if self.ndim == 1 and (prefix or suffix):
raise ValueError("`prefix` and `suffix` are only supported on DataFrame.shift when `periods` is a list. ")
def shift(
self,
periods: int | Sequence[int] = 1,
freq=None,
axis: Axis = 0,
fill_value: Hashable = lib.no_default,
prefix: str | None = None,
suffix: str | None = None,
sep: str = "_"
) -> Self | DataFrame:
"""
Shift index by desired number of periods with an optional time `freq`.
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/frame/methods/test_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,16 @@ def test_shift_with_iterable_check_other_arguments(self):
expected = DataFrame({"a_suffix_0": [1, 2], "a_suffix_1": [np.nan, 1.0]})
tm.assert_frame_equal(shifted, expected)

# test prefix and sep
shifted = df[["a"]].shift(shifts, prefix="pre", suffix="suf", sep="-")
expected = DataFrame(
{
"pre-a-suf-0": [1, 2],
"pre-a-suf-1": [np.nan, 1.0]
}
)
tm.assert_frame_equal(shifted, expected)

# check bad inputs when doing multiple shifts
msg = "If `periods` contains multiple shifts, `axis` cannot be 1."
with pytest.raises(ValueError, match=msg):
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/series/methods/test_diff.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import pytest
import pandas as pd

from pandas import (
Series,
Expand Down Expand Up @@ -89,3 +90,12 @@ def test_diff_object_dtype(self):
result = ser.diff()
expected = ser - ser.shift(1)
tm.assert_series_equal(result, expected)

def test_series_shift_rejects_prefix_suffix():
s = pd.Series([1, 2, 3])

with pytest.raises(ValueError, match="prefix.*DataFrame.shift"):
s.shift(1, prefix="PRE")

with pytest.raises(ValueError, match="suffix.*DataFrame.shift"):
s.shift(1, suffix="POST")
Loading