问题
I have data in a csv file which appears as:
DateTime Temp
10/1/2016 0:00 20.35491156
10/1/2016 1:00 19.75320845
10/1/2016 4:00 17.62411292
10/1/2016 5:00 18.30190001
10/1/2016 6:00 19.37101638
I am reading this file from csv file as:
import numpy as np
import pandas as pd
d2 = pd.Series.from_csv(r'C:\PowerCurve.csv')
d3 = d2.interpolate(method='time')
My goal is to fill the missing hours 2 and 3 with interpolation based on nearby values. i.e. every time there is are missing data it should do the interpolation.
However, d3 doesn't show any interpolation.
Edit: Based on suggestions below my Python 2.7 still errors out. I am trying the following:
import pandas as pd
d2 = pd.Series.from_csv(r'C:\PowerCurve.csv')
d2.set_index('DateTime').resample('H').interpolate()
Error is:
File "C:\Python27\lib\site-packages\pandas\core\generic.py", line 2672, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute 'set_index'
回答1:
use the interpolate method after resample on an hourly basis.
d2.set_index('DateTime').resample('H').interpolate()
If d2 is a series then we don't need the set_index
d2.resample('H').interpolate()
回答2:
Use resample with datetime as index and use one of the methods of resampling that fits your need. For instance:
df.set_index('DateTime').resample('1H').pad()
Out[23]:
Temp
DateTime
2016-10-01 00:00:00 20.354912
2016-10-01 01:00:00 19.753208
2016-10-01 02:00:00 19.753208
2016-10-01 03:00:00 19.753208
2016-10-01 04:00:00 17.624113
2016-10-01 05:00:00 18.301900
2016-10-01 06:00:00 19.371016
来源:https://stackoverflow.com/questions/39949897/python-pandas-interpolating-series