使用日期格式对时间序列进行插值

问题描述 投票:0回答:1

我有以下时间序列数据:

Image

我想按月或按年插值数据,我的代码如下:

df = pd.read_csv('Data/data_processing.csv', encoding='latin1')
df['Date'] = pd.to_datetime(df['Date'])

sns.scatterplot(data=df, x=df['Date'], y=df['Value'])
plt.show()

df['Date'] = pd.to_datetime(df['Date'])
df.set_index('Date', inplace=True)

# Resample and interpolate
df_resampled = df.resample('1Y')
df_interp = df_resampled.interpolate(method='time')

df_interp.to_csv(f'interpolated_data_polynomial.csv', index=True)

结果是不理想的,因为它给出了 Nan 值或完全线性的结果。如何解决问题?

python pandas time-series interpolation resampling
1个回答
0
投票

这里的问题是你的日期索引。文档很清楚

interpolate(method = "time")
如果提供线性日期索引,将产生线性结果。

© www.soinside.com 2019 - 2024. All rights reserved.