0

I have a dataframe of the format below. I need to smooth the value column using univariate splines. For the purpose I am trying to use the scipy interpolate univariate spline function. I converted the datetime column of the dataframe below to datetime64 type.

When I try to feed it to the splines function however as x along with the value column as y I am getting the following error: UFuncTypeError: Cannot cast ufunc 'greater_equal' input 1 from dtype(float64) to dtype(<m8[ns]) with casting rule 'same_kind'. I can understand that the issue comes from the fact that my x column is a datetime object but I am not sure how to resolve it given that my data is time series data.

enter image description here

import pandas as pd
from scipy.interpolate import UnivariateSpline

d = {'datetime': ['08/07/2022  00:02:03.000','08/07/2022  00:15:06.050','08/07/2022  00:30:06.369','08/07/2022  00:31:03.025','08/07/2022  00:35:08.369','08/07/2022  00:37:59.258','08/07/2022  00:45:06.258','08/07/2022  00:45:25.025','08/07/2022  00:49:15.326','08/07/2022  00:51:45.058','08/07/2022  00:33:09.258'], 'value': [10,10.5,10.3,10.8,10.5,10.6,10.9,10.33,10.35,10.36,10.37]}

df = pd.DataFrame(data=d)
df['datetime']=pd.to_datetime(df['datetime'])
x=df['datetime']
y=df['value']
spl=UnivariateSpline(x,y)
VSP
  • 359
  • 3
  • 14

1 Answers1

0

You can convert your data from datetime to the timestamp given in seconds (or any other resolution you want) using the code from this answer.

So I replaced line 8 in your code accordingly:

x = df.datetime.values.astype(np.int64) // 10 ** 9

This, however, still resulted in the error: x must be strictly increasing, since your datetime data was not sorted. I added some sorting and the resulting code is:

x = df.datetime.values.astype(np.int64) // 10 ** 9
y=df['value']

inds = np.argsort(x)
x = x[inds]
y = y[inds]

spl=UnivariateSpline(x,y)

Plotting the result with the code below gave this figure:

enter image description here

plt.plot(x, y, "o")
xx = np.linspace(x[0], x[-1], 100)
plt.plot(xx, spl(xx))
Iddo Hanniel
  • 1,636
  • 10
  • 18