Python 三次样条插值只生成NaN或抛出错误

Python 三次样条插值只生成NaN或抛出错误,python,pandas,interpolation,cubic-spline,Python,Pandas,Interpolation,Cubic Spline,我正在尝试将数据帧的采样从querterly降到monthly i = ['2000-01-01','2000-04-01','2000-07-01','2000-10-01','2001-01-01','2001-04-01','2001-07-01','2001-10-01'] d = [0,54957.84767,0,0,0,56285.54879,0,0] df = pd.DataFrame(index=i, data=d) df.index = pd.to_datetime(df.i

我正在尝试将数据帧的采样从querterly降到monthly

i = ['2000-01-01','2000-04-01','2000-07-01','2000-10-01','2001-01-01','2001-04-01','2001-07-01','2001-10-01']
d = [0,54957.84767,0,0,0,56285.54879,0,0]

df = pd.DataFrame(index=i, data=d)
df.index = pd.to_datetime(df.index,infer_datetime_format=True)
df.index = df.index.to_period('Q')

df.resample('M').interpolate(method='cubic')
这会引发一个类型错误:

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
如果我跳过这一行:

    df.index = df.index.to_period('Q')
然后它只会产生NAN

这是有效的:

import pandas as pd
import matplotlib.pyplot as plt

i = pd.to_datetime(['2000-01-01','2000-04-01','2000-07-01','2000-10-01','2001-01-01','2001-04-01','2001-07-01','2001-10-01'])
d = [0,54957.84767,0,0,0,56285.54879,0,0]

df = pd.DataFrame({'Values': d}, index=i)
df_resampled = df.resample('M').first().interpolate(method='cubic')

# Display the fit
df['Values'].plot(linestyle='none', marker='D', color='red')
df_resampled['Values'].plot(label='fit')
plt.xlabel('Date')
plt.ylabel('Values, a.u.')
plt.legend()

谢谢,这确实会对数据进行重新采样,但现在的问题是数据应该要低得多。我上面的数据是按季度(求和)的,因此每个月的数据应该是下一季度的三分之一。然后你可以将所有数据除以3得到每月的平均值,例如d=np.array([054957.84767,0,0,056285.54879,0,0])/3,然后是代码的其余部分。