Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python re-sample at a uniform semiannual period (equivaent of 'BQ' in pandas resample)

Tags:

python

pandas

is there an 'BQ' equivalent semiannual resample in python? i didnt find it here

http://pandas.pydata.org/pandas-docs/dev/timeseries.html#up-and-downsampling

i've a set of records, some of them follow jun-dec, some jan-jul, some feb-auh etc. how do i resample all of them to jun-dec (concurrent for jun-dec, and following jun/dec for other records?

Thank you.

like image 936
user3905378 Avatar asked Dec 29 '25 18:12

user3905378


1 Answers

How about '2BQ'?

In [57]: ts = pd.Series(range(1000), index=pd.date_range('2000-4-15', periods=1000))

In [58]: ts.resample('2BQ', how='sum')
Out[58]: 
2000-06-30      2926
2000-12-29     30485
2001-06-29     63609
2001-12-31     98605
2002-06-28    127985
2002-12-31    166935
2003-06-30      8955
Freq: 2BQ-DEC, dtype: int64

The 2 Quarter offset will be based on the first timestamp in the series, so if your data happens to start in Jan-Mar or Jun-Sep, the anchor will be wrong. One way to fix it would be to fill a dummy date at the beginning of the series so the anchor is right.

ts = pd.Series(range(1000), index=pd.date_range('2000-3-15', periods=1000))

from datetime import datetime
if ts.index[0].month in [1,2,3]:
    ts.loc[datetime(ts.index[0].year - 1, 12, 1)] = np.nan
elif ts.index[0].month in [7,8,9]:
    ts.loc[datetime(ts.index[0].year, 6, 1)] = np.nan

Should give the right answer (and can drop the first entry).

In [85]: ts.resample('2BQ', how='sum')
Out[85]: 
1999-12-31       NaN
2000-06-30      5778
2000-12-29     36127
2001-06-29     69251
2001-12-31    104340
2002-06-28    133534
2002-12-31    150470
Freq: 2BQ-DEC, dtype: float64
like image 83
chrisb Avatar answered Jan 09 '26 00:01

chrisb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!