[Python] Why does pct_change() still raise FutureWarning after using .ffill() in pandas?

Discussão em 'Python' iniciado por Stack, Maio 21, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to compute percentage changes in a pandas Series, while handling NaN values correctly and avoiding any FutureWarning. However, I'm stuck between two conflicting warnings.

    sub_df[f"Δ {col}"] = sub_df[col].ffill().pct_change()


    This seems like the proper way to handle the deprecation of internal fill behavior in pct_change() — I explicitly call .ffill() before pct_change().

    Problem is that despite that, I still get this warning:


    FutureWarning: The default fill_method='pad' in Series.pct_change is deprecated and will be removed in a future version. Call ffill before calling pct_change to retain current behavior and silence this warning.
    ​

    It seems contradictory, because I already am calling .ffill().

    So I tried the alternative:

    sub_df[f"Δ {col}"] = sub_df[col].ffill().pct_change(fill_method=None)


    This now raises a different warning:


    FutureWarning: The 'fill_method' and 'limit' keywords in Series.pct_change are deprecated and will be removed in a future version. Call ffill before calling pct_change instead.
    ​

    Calling .ffill().pct_change() raises a warning that suggests I should call .ffill() — which I already do.

    Setting fill_method=None to suppress that warning raises another FutureWarning that fill_method itself is deprecated.

    How can I use pct_change() in a way that:

    1. handles missing values as before (with forward-fill),
    2. avoids any FutureWarning, and aligns with future pandas behavior?

    Is this a bug in pandas, or am I misunderstanding something?

    Continue reading...

Compartilhe esta Página