![](/img/trans.png)
[英]Need to count the total sum of operations from 15th day to 15th day of next month
[英]pandas monthly resample 15th day
我正在嘗試重新采樣到每月值,但關於第 15 天
我檢查了時間序列偏移文檔,但只有
M 月末頻率 SM 半月末頻率(15 日和月末) MS 月開始頻率 SMS 半月開始頻率(1 日和 15 日)
而我只需要第15天
就像是
2000-01-15 8.7
2000-02-15 6.9
2000-03-15 15.8
2000-04-15 12.4
我嘗試使用 pd.offsets.MonthBegin 和 MonthOffset 沒有結果
在MS
月份開始前進行匯總,然后通過loffset
參數調整重新采樣的時間標簽:
df1 = df.resample('MS', loffset=pd.Timedelta(14, 'd')).sum()
樣品:
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
另一個答案在 pandas 1.4.2
中被棄用,並帶有警告FutureWarning: 'loffset' in.resample() and in Grouper() is deprecated.
推薦的替代方法是先正常重新采樣,然后將Timedelta
添加到索引中:
df1 = df.resample('MS').sum()
df1.index += pd.Timedelta(14, 'd')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.