Zanam Zanam - 1 month ago 32
Python Question

Python pandas interpolating series

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'

Answer

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()

enter image description here

Comments