Search code examples
pythonpandasnumpynanfillna

Cannot assign "nan"/empty value in np.where


I'm trying to assign an empty cell (blank/nan) in my "np.where" condition to my pandas dataframe, but nothing seems to work.

The reason for this is to run fillna,ffill on the missing values.

Np.Where code:

df['x'] = np.where(df['y']>0.05,1,np.nan)

Fillna code:

df['x'] = df['x'].fillna(method="ffill")

Anybody know where I'm going wrong?


Solution

  • I was able to fix it using pandas.NA instead which fillna, for some reason, recognizes as blanks to fill with ffill

    Fix:

    df['x'] = np.where(df['y']>0.05,1,pd.NA)
    df['x'] = df['x'].fillna(method="ffill")