Skip to content
Advertisement

Replacing NaN values in timeseries Pandas dataframe with mean values

I have a dataframe that has 2 columns, date and values. I want to replace NaN values in the dataframe with mean values, but with specific condition.

JavaScript

NaN values should be replaced with mean value of the values from the same period for the year that has that value (+/- 1 day).

Value for 2021-02-04 should be:

JavaScript

Because dates "2022-02-03", "2022-02-04", "2022-02-05" have values of 6,2 and 5.

I know how to fill nan values with just mean value, but I do not know how to solve the problem of +/- one day.

Advertisement

Answer

Really hard to say what exactly you want to do, but given your data:

JavaScript

You can try something like this:

JavaScript
JavaScript

Take a close look at, for example, 2021-02-04 which had a NaN value. I disregard the years (as mentioned in the comments) and just look at the months and days resulting in (6 + 2 + 5 + 3) / 4 = 4.0, since “2022-02-03”, “2022-02-04”, “2022-02-05”, and “2021-02-03” have values of 6, 2, 5, and 3.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement