Skip to content

ENH: allow pct change to generate ghost dates to avoid missing values #48983

@DanielGoncalves47264

Description

@DanielGoncalves47264

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

I'm taking advantage of the DateOffset to calculate % changes. However, this approach generates some NaN values due to irregularities in months. I would like to be able to avoid this NaN by filling with the values of the adjacent days. eg. monthly_change of 30 of March be calculated as 30 March / 28 or 29 February.

  returns_calculation = {
      'daily_change': {'days': 1},
      'weekly_change': {'weeks': 1},
      'monthly_change': {'months': 1},
      'quarterly_change': {'months': 3},
      'semester_change': {'months': 6},
      'yearly_change': {'years': 1},
      'five_year_change': {'years': 5},
  }
   for period, freq in returns_calculation.items():
            df[period] = df['col1'].pct_change(
                freq=pd.tseries.offsets.DateOffset(**freq))

Feature Description

Add a new parameter to pct_change method that would temporarily reindex the series by introducing this shadow dates, and back fill if the closest date is available.

def pct_change(...., ghost_dates: bool = True):
    if ghost_dates:
         # Do something
´´´

### Alternative Solutions

I believe one could simply map every occurrence of such irregularities and use loc to fill those dates although it seems hardcoding and hard to scale.

### Additional Context

_No response_

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions