Using resample to align multiple time series in pandas

Here's the installation code:

import pandas from datetime import datetime a_values = [1728, 1635, 1733] a_index = [datetime(2011, 10, 31), datetime(2012, 1, 31), datetime(2012, 4, 30)] a = pandas.Series(data=a_values, index=a_index) aa_values = [6419, 5989, 6006] aa_index = [datetime(2011, 9, 30), datetime(2011, 12, 31), datetime(2012, 3, 31)] aa = pandas.Series(data=aa_values, index=aa_index) apol_values = [1100, 1179, 969] apol_index = [datetime(2011, 8, 31), datetime(2011, 11, 30), datetime(2012, 2, 29)] apol = pandas.Series(data=apol_values, index=apol_index) 

Here the data looks like in the table (the third value for APOL is not shown):

enter image description here

The goal is to align data with calendar quarter markers so that 3 sets of data can be compared. Just by looking at the dates below, March 2012, December 2011 and September 2011 seem like reasonable markers for alignment.

Here is the output with fill_method = 'ffill':

 In [6]: a.resample('Q', fill_method='ffill') Out[6]: 2011-12-31 1728 2012-03-31 1635 2012-06-30 1733 Freq: Q-DEC In [7]: aa.resample('Q', fill_method='ffill') Out[7]: 2011-09-30 6419 2011-12-31 5989 2012-03-31 6006 Freq: Q-DEC In [8]: apol.resample('Q', fill_method='ffill') Out[8]: 2011-09-30 1100 2011-12-31 1179 2012-03-31 969 Freq: Q-DEC 

Which looks like this:

enter image description here

Please note how the latest numbers in each series do not line up.

And here is the output with fill_method = 'bfill':

 In [9]: a.resample('Q', fill_method='bfill') Out[9]: 2011-12-31 1635 2012-03-31 1733 2012-06-30 NaN Freq: Q-DEC In [10]: aa.resample('Q', fill_method='bfill') Out[10]: 2011-09-30 6419 2011-12-31 5989 2012-03-31 6006 Freq: Q-DEC In [11]: apol.resample('Q', fill_method='bfill') Out[11]: 2011-09-30 1179 2011-12-31 969 2012-03-31 NaN Freq: Q-DEC 

Which looks like this:

enter image description here

Again, the latest numbers in the series do not line up.

Is this the expected output of resample() in this script?

What can I do to get results that align the last 3 numbers above, and everything else follows properly?

EDIT . Here is the desired result:

enter image description here

+4
source share
1 answer
 df1 = DataFrame({'a':a}) df2 = DataFrame({'aa':aa}) df3 = DataFrame({'apol':apol}) df=df1.append([df2,df3]).sort_index() print df.resample('Q-APR',loffset='-1m').T 

Output:

  2011-09-30 2011-12-31 2012-03-31 a 1728 1635 1733 aa 6419 5989 6006 apol 1100 1179 969 
+5
source

Source: https://habr.com/ru/post/1443532/


All Articles