Interpolating time-series with the date format

63 Views Asked by At

I have the following time series data:

Image

I want to interpolate the data monthly or yearly, and my code is as follows:

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)

The results are undesirable as it gives the Nan values or completely linear results. How to resolve the problem?

1

There are 1 best solutions below

0
TJM1769 On

The question here is your date index. The documentation is clear that interpolate(method = "time") will produce linear results if presented with a linear date index.