I have the following dataframe:
M P
2022-01-10 250
2022-04-03 100
2022-05-26 350
2022-08-01 200
... ...
^just an example. Basically I have some dates and some prices associated with them.
I'm trying to create a daily time-series curve using the above fixed points with the cubic splining method.
I've looked at using the following -
from scipy.interpolate import CubicSpline
cs = CubicSpline(df['M'], df['P'])
But I get the following error -
TypeError: float() argument must be a string or a number, not 'datetime.date'
because it doesn't like the date variable type. Two questions:
a) Basically, how do I interpolate the x, y to produce my curve?
b) Secondly, I'm using this method to try and replicate the 'S' curve in the following documentation chart:
https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.CubicSpline.html
...however what I'm not them sure of is how I construct a daily time series with dates / calculated prices which corresponds to this curve? Will that automatically happen when the CubicSpline method is called?
Any help would be greatly appreciated!
Create DatetimeIndex
by DataFrame.set_index
, add missing values for daily frequency by DataFrame.asfreq
and last use DataFrame.interpolate
with method cubicspline
:
'krogh', 'piecewise_polynomial', 'spline', 'pchip', 'akima', 'cubicspline': Wrappers around the SciPy interpolation methods of similar names. See Notes.
#convert date objects to datetimes
curve0['M'] = pd.to_datetime(curve0['M'])
df = curve0.set_index('M').asfreq('d').interpolate(method='cubicspline')
print (df)
P
M
2022-01-10 250.000000
2022-01-11 238.482096
2022-01-12 227.285579
2022-01-13 216.407317
2022-01-14 205.844180
...
2022-07-28 238.817308
2022-07-29 229.568679
2022-07-30 220.018338
2022-07-31 210.163156
2022-08-01 200.000000
[204 rows x 1 columns]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.