1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| import pandas as pd import copy
ts = pd.read_csv('daily-min-temperatures.csv', parse_dates=['Date'], index_col='Date') print(ts.head())
def ts_fillna(data, date_col, val_col): data = copy.deepcopy(data) helper = pd.DataFrame({date_col: pd.date_range(data.index.min(), data.index.max())}) newdata = pd.merge(data, helper, on=date_col, how='outer').sort_values(date_col)
newdata[val_col] = newdata[val_col].interpolate(method='linear')
newdata[date_col] = pd.to_datetime(newdata[date_col]) newdata.set_index(date_col, inplace=True, verify_integrity=False) return newdata
ts2 = ts_fillna(ts, 'Date', 'Temp') print(ts2.head())
|