Resampling doesn't work on the first data

99 Views Asked by At

I tried to convert yearly data to monthly, starting from 2001 to 2020, however 2001 data was not converted to monthly.

The original dataset looks like:

0 2001-12-31 16.5648 4.9887 11.1706 ... 6.4568 7.9525 7.7998 11.2796

1 2002-12-31 15.3589 14.1851 11.4080 ... 7.7080 7.6465 8.1067 11.6526

2 2003-12-31 10.0559 15.1041 11.5154 ... 8.6358 9.0530 9.4951 11.8169

3 2004-12-31 13.4166 15.6389 12.9674 ... 9.9278 10.7078 11.1832 13.0509

4 2005-12-31 13.3857 16.7083 8.9411 ... 11.0563 12.7320 11.9903 13.0580

What I tried:

df4['Date']=pd.to_datetime(df4['Date'])

df4 = df4.set_index('Date').resample('M').bfill().reset_index()

The output:

2001-12-31  16.5648   4.9887  11.1706  ...   7.9525   7.7998  11.2796

2002-01-31  15.3589  14.1851  11.4080  ...   7.6465   8.1067  11.6526

2002-02-28  15.3589  14.1851  11.4080  ...   7.6465   8.1067  11.6526

2002-03-31  15.3589  14.1851  11.4080  ...   7.6465   8.1067  11.6526

2002-04-30  15.3589  14.1851  11.4080  ...   7.6465   8.1067  11.6526

Can anybody tell me what I missed? Thank you

1

There are 1 best solutions below

2
On

What you're doing is resampling the data from yearly to monthly and then calling bfill to backwards fill the data gaps with constant value.

If you would like to interpolate the gaps, you should call resample('M').interpolate() instead.