Skip to content

ENH: Add prefix, suffix and sep arguments to shift method #61696

Open
@ArturoSbr

Description

@ArturoSbr

Feature Type

  • Adding new functionality to pandas

    Changing existing functionality in pandas

    Removing existing functionality in pandas

Problem Description

Hi all!

I'm currently working on a forecasting model and had to create multiple lags of many columns. Doing this made me realize that the shift method does not have prefix, prefix_sep, suffix nor suffix_sep arguments.
I think adding some (or all) of these arguments would be super useful and could help standardize the method with others such as pd.get_dummies. Additionally, this is already implemented to some extent because when periods is a list, it adds a suffix to each lagged column.

Feature Description

Obviously this is redundant because the method calls itself, but I think it conveys the idea well.

Suppose suffix and suffix_sep are strings (eg 'lag' and '_') and that columns is an iterable.

if suffix and suffix_sep:
    for column in columns:
        for period in periods:
            data[f'{column}{suffix_sep}{suffix}{period}] = data[column].shift(period)

Alternative Solutions

Here's what I'm currently doing to add _lagX as a suffix:

lags = [1, 2, 3, 6, 9, 12] 
_temp = df[cols_og_feats].shift(periods=lags)  # Lag each column by each lag in lags
_temp.columns = [
    '_'.join(col.split('_')[:-1]) + '_lag' + col.split('_')[-1] for col in _temp.columns
]  # add '_lagX' suffix

Additional Context

No response

Activity

sajansshergill

sajansshergill commented on Jun 23, 2025

@sajansshergill

lags = [1, 2, 3]
cols = ['sales', 'revenue']

Efficiently create lagged columns with suffix _lag{n}

df_lagged = pd.concat(
[df[col].shift(lag).rename(f"{col}_lag{lag}") for col in cols for lag in lags],
axis=1
)

Optionally combine with original dataframe

df = pd.concat([df, df_lagged], axis=1)

ArturoSbr

ArturoSbr commented on Jun 24, 2025

@ArturoSbr
Author

Thank you!
I see what you mean @sajansshergill but I wanted to pass an iterable to the method.
Is list comp more efficient than passing an iterable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @ArturoSbr@sajansshergill

      Issue actions

        ENH: Add prefix, suffix and sep arguments to shift method · Issue #61696 · pandas-dev/pandas