Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas monthly resample 15th day

I am trying to resample to monthly values but with respect to 15th day

I checked the timeseries offsets documentation but there is only

M month end frequency SM semi-month end frequency (15th and end of month) MS month start frequency SMS semi-month start frequency (1st and 15th)

while I need just the 15th day

Something like

2000-01-15 8.7
2000-02-15 6.9
2000-03-15 15.8
2000-04-15 12.4

I tried with pd.offsets.MonthBegin and MonthOffset with no results

like image 309
gis20 Avatar asked Mar 08 '23 15:03

gis20


1 Answers

Aggregate by starts of months MS and then adjust the resampled time labels by loffset parameter:

df1 = df.resample('MS', loffset=pd.Timedelta(14, 'd')).sum()

Sample:

rng = pd.date_range('2017-04-03', periods=15, freq='5D')
df = pd.DataFrame({'a': range(15)}, index=rng)  
print (df)
             a
2017-04-03   0
2017-04-08   1
2017-04-13   2
2017-04-18   3
2017-04-23   4
2017-04-28   5
2017-05-03   6
2017-05-08   7
2017-05-13   8
2017-05-18   9
2017-05-23  10
2017-05-28  11
2017-06-02  12
2017-06-07  13
2017-06-12  14

df1 = df.resample('MS', loffset=pd.Timedelta(14, 'd')).sum()
print (df1)
             a
2017-04-15  15
2017-05-15  51
2017-06-15  39

df1 = df.resample('SMS').sum()
print (df1)
             a
2017-04-01   3
2017-04-15  12
2017-05-01  21
2017-05-15  30
2017-06-01  39
like image 143
jezrael Avatar answered Mar 19 '23 17:03

jezrael