Cannot interpolate in pandas dataframe

How can I interpolate time series as follows?

>>> 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 some workarounds? Thanks for the help!

+4
source share
1 answer

It looks like an error / missing. Here is 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
+2
source

Source: https://habr.com/ru/post/1620372/


All Articles