1

Sorry for the beginner-friendly question, but this code does not work for me and I can't find out why. It runs without errors, but it does not fill the values.

data['date'] = data['date'].replace(np.nan, '22.02.2021')

I tried using "fillna" function like this:

data['date'].fillna('22.02.2021', inplace=True)

but it gives error and says that fill value must be in categories.

ela3
  • 33
  • 1
  • 6
  • 1
    Does this answer your question? [How to replace NaN values by Zeroes in a column of a Pandas Dataframe?](https://stackoverflow.com/questions/13295735/how-to-replace-nan-values-by-zeroes-in-a-column-of-a-pandas-dataframe) – buran Mar 14 '21 at 11:27
  • Try this maybe: https://stackoverflow.com/questions/53664948/pandas-fillna-throws-valueerror-fill-value-must-be-in-categories – Kris Mar 14 '21 at 11:33

1 Answers1

1

Normally you can use pandas pd.to_datetime()

https://datatofish.com/replace-nan-values-with-zeros/ :

single column:

df['DataFrame Column'] = df['DataFrame Column'].fillna(value=pd.to_datetime('2/22/2021'), inplace=True)

entire dataframe:

df.fillna(value=pd.to_datetime('2/22/2021'), inplace=True)

However because of your comment that error 'Value error : fill value must be in categories' is thrown anyway you have to change the dtype of column date in your dataframe from category to i.e. datetime64 see How do I convert strings in a Pandas data frame to a 'date' data type? and Change column type in pandas

When casting the datetime string (22.02.2021) using astype() you can skip using pd.datetime()

ralf htp
  • 9,149
  • 4
  • 22
  • 34