1

How can I convert a yearly timeseries into monthly, if my df index have '01-01-2000','01-01-2001','01-01-2002' and so on. The resample method with 'M' works but the monthly series stops at '01-01' of the last year but I would like to have '12-01' of the last year in the range. Appreciate any help.

RanjaniR
  • 13
  • 2
  • 1
    You would need to add one more data point by `df.loc['12-01-last_year'] = np.NaN`. – Quang Hoang Jun 28 '19 at 19:49
  • 1
    This is just a guess, as the questions is not very clear, but you can interpolate only the data between two existing data points. You could try extrapolation (predicting the new values based on the historical known data). – Lenka Vraná Jun 28 '19 at 19:49
  • I am not trying to interpolate but to use the values given for yearly for all the months in the given year. Something similar to what was asked in here. https://stackoverflow.com/questions/45306105/pandas-convert-yearly-to-monthly – RanjaniR Jun 28 '19 at 19:53
  • Maybe you should try to attach an example of your data set and the desired output. – Lenka Vraná Jun 28 '19 at 19:54
  • I would like to get Month start instead of month end in the last print statement df = pd.DataFrame({'date':pd.date_range('2001-01-01',periods=15, freq='AS'), 'Value_1':np.random.randint(0,100,15)}) print (df) df=df.set_index('date') df_out = df.resample('M').ffill() print(df_out) – RanjaniR Jun 28 '19 at 20:04

1 Answers1

0

You may try adding the last fake observation (as Quang Hoang proposed):

import pandas as pd
import numpy as np

df = pd.DataFrame({'date': pd.date_range('2001-01-01', periods = 5, freq = 'AS'), 'Value_1': np.random.randint(0, 100, 4).tolist() + [np.NaN]})
df = df.set_index('date')
print(df)

The original series ends on 2004-01-01.

df_out = df.resample('MS').ffill()
print(df_out)

The new series ends on 2004-12-01.

Lenka Vraná
  • 1,686
  • 2
  • 19
  • 29
  • 1
    Thanks Quang and Lenka. This worked. But I am trying to see any other way of doing this without adding another fake data point – RanjaniR Jun 28 '19 at 21:05