This code can count the frequency of dates in this way : Monday, Tuesday,Wednesday and Thursday together and Saturday,Sunday together. How to change the arguments of map function to get repeatation of dates in two groups: 1. 9 am to 5pm weekdays 2. the rest of hours in the week (5pm to 9am weekdays and weekends).
此代码可以通过这种方式计算日期的频率:周一,周二,周三和周四以及周六,周日。如何更改地图功能的参数以重复两组中的日期:1。工作日上午9点至下午5点2.一周中的其余时间(工作日和周末下午5点至9点)。
d = ['10/3/2013 18:36', '10/3/2013 23:40', '10/3/2013 20:56', '10/4/2013 9:35', '11/7/2013 10:02', '11/11/2013 14:45', '12/1/2013 12:04']
df = pd.DataFrame(pd.to_datetime(d), columns=["DATE"])
df["DATE"].dt.weekday.map({0:0,1:0,2:0,3:0,4:0,5:1,6:1}).value_counts()
2 个解决方案
#1
0
Pandas has some built-in methods for identifying business days. There might actually be some built-in functions for business hours but I'm not sure as I don't deal with datetimes that often:
熊猫有一些内置的方法来识别工作日。实际上可能有一些内置函数用于营业时间,但我不确定,因为我不经常处理日期时间:
df['in_business_hours'] = (
df['DATE'].map(pd.datetools.isBusinessDay) &
((9 <= df.DATE.dt.hour) & (df.DATE.dt.hour <= 16))
)
df['in_business_hours'].value_counts()
Out[14]:
False 4
True 3
dtype: int64
#2
0
Since .map()
can be applied directly on the Series and also takes an arbitrary function, you can use:
由于.map()可以直接应用于Series并且还可以使用任意函数,因此您可以使用:
df['DATE'].map(lambda dt:
'Office' if dt.weekday() in {0,1,2,3,4} and 9 <= dt.hour < 17
else 'Out of office'
).value_counts()
The result is:
结果是:
Out of office 4
Office 3
dtype: int64
#1
0
Pandas has some built-in methods for identifying business days. There might actually be some built-in functions for business hours but I'm not sure as I don't deal with datetimes that often:
熊猫有一些内置的方法来识别工作日。实际上可能有一些内置函数用于营业时间,但我不确定,因为我不经常处理日期时间:
df['in_business_hours'] = (
df['DATE'].map(pd.datetools.isBusinessDay) &
((9 <= df.DATE.dt.hour) & (df.DATE.dt.hour <= 16))
)
df['in_business_hours'].value_counts()
Out[14]:
False 4
True 3
dtype: int64
#2
0
Since .map()
can be applied directly on the Series and also takes an arbitrary function, you can use:
由于.map()可以直接应用于Series并且还可以使用任意函数,因此您可以使用:
df['DATE'].map(lambda dt:
'Office' if dt.weekday() in {0,1,2,3,4} and 9 <= dt.hour < 17
else 'Out of office'
).value_counts()
The result is:
结果是:
Out of office 4
Office 3
dtype: int64