[英]Business days between two dates in Python
I have two columns "Blk Start"
and "Blk End"
in a dataframe.我在数据框中有两列
"Blk Start"
和"Blk End"
。 I need to create the 3rd column by calculating the number of working days between two dates.我需要通过计算两个日期之间的工作日数来创建第三列。 I am using the below code, but its not working
我正在使用下面的代码,但它不起作用
df[x] = np.busday_count(df['Blk_start'],df['Blk_end'])
Do I need to use any other function?我需要使用任何其他功能吗?
The error from the above code is:上面代码的错误是:
TypeError Traceback (most recent call last) in ----> 1 df[x] = np.busday_count(df['Blk_start'],df['Blk_end']) TypeError: Iterator operand 0 dtype could not be cast from
dtype('<M8[ns]')
todtype('<M8[D]')
according to the rule 'safe'TypeError Traceback(最近一次调用最后一次)在----> 1 df[x] = np.busday_count(df['Blk_start'],df['Blk_end']) TypeError:迭代器操作数 0 dtype 无法从
dtype('<M8[ns]')
到dtype('<M8[D]')
根据规则 'safe'
Column formats are:列格式为:
fundmgr_id int64
user_name object
Blk_start. datetime64[ns]
Blk_end datetime64[ns]
np.busday_count says it works on an array_like of datetime64[D] so you can cast the columns to that format first, then perform the operation. np.busday_count表示它适用于datetime64[D]的array_like,因此您可以先将列转换为该格式,然后执行操作。
en = df["Blk_end"].values.astype('datetime64[D]')
st = df["Blk_start"].values.astype('datetime64[D]')
df["x"] = np.busday_count(st, en)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.