[英]pandas monthly resample 15th day
I am trying to resample to monthly values but with respect to 15th day我正在尝试重新采样到每月值,但关于第 15 天
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) M 月末频率 SM 半月末频率(15 日和月末) MS 月开始频率 SMS 半月开始频率(1 日和 15 日)
while I need just the 15th day而我只需要第15天
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我尝试使用 pd.offsets.MonthBegin 和 MonthOffset 没有结果
Aggregate by starts of months MS
and then adjust the resampled time labels by loffset
parameter: 在
MS
月份开始前进行汇总,然后通过loffset
参数调整重新采样的时间标签:
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
The other answer is deprecated in pandas 1.4.2
and comes with a warning FutureWarning: 'loffset' in.resample() and in Grouper() is deprecated.
另一个答案在 pandas
1.4.2
中被弃用,并带有警告FutureWarning: 'loffset' in.resample() and in Grouper() is deprecated.
The recommended alternative is do first resample normally, then add a Timedelta
to the index:推荐的替代方法是先正常重新采样,然后将
Timedelta
添加到索引中:
df1 = df.resample('MS').sum()
df1.index += pd.Timedelta(14, 'd')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.