pythonpandasdataframefillna

Alternative to fillna(method='pad', inplace=True) to avoid FutureWarning


I would like to fill NA/NaN values with the last valid option.

My code:

import pandas as pd
import numpy as np
df = pd.DataFrame({"Col1":["A", np.nan, 1, np.nan],
                   "Col2":["B", np.nan, "C", np.nan]})

df.fillna(method='pad', inplace=True)

Output:

Col1 Col2
A B
A B
1 C
1 C

Warning Massage:

FutureWarning: DataFrame.fillna with 'method' is deprecated and will raise in a future version. Use obj.ffill() or obj.bfill() instead.

Using: df.bfill

However the bfill provides different output from what I used to have.

df.bfill( inplace=True)

Output for bfill:

Col1 Col2
A B
1 C
1 C
nan nan

Using: df.pad

I've tried to use df.pad:

df.pad(inplace=True)

It returns the right output, but then I get another FutureWarning:

FutureWarning: DataFrame.pad/Series.pad is deprecated. Use DataFrame.ffill/Series.ffill instead

Question: What is alternative to fillna(method='pad', inplace=True) to avoid FutureWarning message?


Solution

  • Don't use fillna if your goal is to ffill/bfill, this behavior is deprecated.

    df.fillna(method='pad', inplace=True)
    

    should be replaced by:

    df.ffill(inplace=True)
    

    From the documentation:

    method: {‘backfill’, ‘bfill’, ‘ffill’, None}, default None

    Method to use for filling holes in reindexed Series:

    • ffill: propagate last valid observation forward to next valid.

    • backfill / bfill: use next valid observation to fill gap.

    Deprecated since version 2.1.0: Use ffill or bfill instead.

    NB. pad was a synonym to ffill prior to version 2.