I've got a timeseries of intermitent daily data like this.
import pandas as pd
import numpy as np
df = pd.DataFrame({'Date': ['2020-01-01', '2020-01-02', '2020-01-02','2020-01-02','2020-01-03','2020-01-04','2020-01-07','2020-01-08','2020-01-08','2020-01-10','2020-01-13','2020-01-15'],
'Price': [200, 324, 320, 421, 240, np.NaN, 500, 520, 531, np.NaN, 571, np.NaN]})
df['Date']= pd.to_datetime(df['Date'])
df.set_index('Date')
df
Result:
+------------+-------+
| Date | Price |
+------------+-------+
| 2020-01-01 | 200 |
+------------+-------+
| 2020-01-02 | 324 |
+------------+-------+
| 2020-01-02 | 320 | -- 1st duplicate for 2020-01-02
+------------+-------+
| 2020-01-02 | 421 | -- 2nd duplicate for 2020-01-02
+------------+-------+
| 2020-01-03 | 240 |
+------------+-------+
| 2020-01-04 | NaN |
+------------+-------+
| 2020-01-07 | 500 |
+------------+-------+
| 2020-01-08 | 520 |
+------------+-------+
| 2020-01-08 | 531 | -- 1st duplicate for 2020-01-08
+------------+-------+
| 2020-01-10 | NaN |
+------------+-------+
| 2020-01-13 | 571 |
+------------+-------+
| 2020-01-15 | NaN |
+------------+-------+
I need to fill the NaN values with prices from nearest available date where there is more than 1 price recorded (duplicate) i.e.
- 320 should be moved from 2020-01-02 to 2020-01-04
- 421 from 2020-01-02 to 2020-01-10
- 531 from 2020-01-08 to 2020-01-15