Reputation: 724
How can I interpolate the time series like this?
>>> df=pd.DataFrame([1,2,np.nan,4],columns=['val'],index=pd.to_timedelta([1,2,3,4],unit='s'))
>>> df
val
00:00:01 1
00:00:02 2
00:00:03 NaN
00:00:04 4
The following interpolation does not work.
df.interpolate(method='time')
...
TypeError: Cannot cast array data from dtype('<m8[ns]') to dtype('float64') according to the rule 'safe'
Does anyone know why or any workarounds? Thank you for your help!
Upvotes: 3
Views: 2496
Reputation: 375405
This looks like a bug/missing feature. Here's a workaround:
In [11]: ind = df.index
In [12]: df.index = df.index.total_seconds()
In [13]: df.interpolate(method="index")
Out[13]:
val
1 1
2 2
3 3
4 4
In [14]: df = df.interpolate(method="index")
In [15]: df.index = ind
In [16]: df
Out[16]:
val
00:00:01 1
00:00:02 2
00:00:03 3
00:00:04 4
or in one function:
def interpolate_delta(df, inplace=False):
if not inplace:
df = df.copy()
ind = df.index
df.index = df.index.total_seconds()
df.interpolate(method="index", inplace=True)
df.index = ind
return df
Upvotes: 1