1

I am trying to downsample a dataframe from querterly to 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')

This throws a TypeError:

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''

if I skip this line:

    df.index = df.index.to_period('Q')

Then it just produces nans

delica
  • 1,647
  • 13
  • 17

1 Answers1

2

This works:

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()

enter image description here

KRKirov
  • 3,854
  • 2
  • 16
  • 20
  • Thank you, that does resample the data, but now the issue is the numbers should be much lower. The data I have above are quarterly (summed), so the data for each month should be about a third of the following quarter. – delica Jul 10 '18 at 10:00
  • Then you can divide all data by a factor of 3 to get the average per month, e.g. d = np.array([0,54957.84767,0,0,0,56285.54879,0,0])/3 followed by the rest of the code. – KRKirov Jul 10 '18 at 10:52