[英]pandas Groupby MonthStart with two business days offset
I have a DataFrame
that is indexed by date and has daily data. 我有一个按日期索引并有每日数据的
DataFrame
。
As described I wish to group and aggregate this data by calendar month start minus 2 business days. 如前所述,我希望按日历月开始减去2个工作日对数据进行分组和汇总。 My idea is to use
groupby
and MonthBegin
with a 2 days BDay
offset to this. 我的想法是将
groupby
和MonthBegin
与BDay
偏移2天。
When I try run the code 当我尝试运行代码
import pandas as pd
import pandas.tseries.offsets as of
days = of.MonthBegin() - of.BDay(2)
g = df.groupby(pd.Grouper(freq=days, level='Date')).sum()
I get an error 我得到一个错误
TypeError: Argument 'other' has incorrect type (expected datetime.datetime, got BusinessDay)
TypeError:参数“ other”具有错误的类型(预期datetime.datetime,为BusinessDay)
Perhaps I need to use the rollback
method on MonthBegin
but when I try 也许我需要在
MonthBegin
上使用rollback
方法,但是当我尝试
days = of.MonthBegin()
days.rollback(of.BDay(2))
g_df = df.groupby(pd.Grouper(freq=days, level='Date')).sum()
TypeError: Cannot convert input [<2 * BusinessDays>] of type to Timestamp
TypeError:无法将类型的输入[<2 * BusinessDays>]转换为时间戳
Does anyone have any ideas how to correctly use the offsets
to groupby MonthBegin - 2BDay
? 有谁知道如何正确使用
offsets
对MonthBegin - 2BDay
?
It is hard to tell, what you want to achieve without any data of yours, but here is how you could do it: 很难说,没有您的任何数据您想要实现什么,但是这是您可以做到的:
df = pd.DataFrame({"dates": ["2018-01-02", "2018-01-03", "2018-02-02", "2018-01-04"],
"vals": [10, 20, 10, 5]})
df.groupby((pd.to_datetime(df.dates) - of.MonthBegin() - of.BDay(2)).dt.month).vals.sum()
Output: 输出:
dates
1 10
12 35
Name: vals, dtype: int64
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.