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

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

    No branches or pull requests

    Issue actions