python - 在 Pandas 中获取工作日时出错
问题描述
我有以下数据框,其中包含几列日期范围的开头和结尾我想检查之间的工作日:
In [139]: df.head()
Out[139]:
Num Material Kilos Sold Date Year Week_Number \
0 5881999 1020252 629 1434120 2019-06-14 2019 24
1 94623000 1022746 598 1572740 2019-06-12 2019 24
2 77274650 1022470 920 1684520 2019-06-25 2019 26
3 94623000 1022643 1215 2563650 2019-01-23 2019 4
40 94623000 1021478 581 1528030 2018-10-23 2018 43
Week_init Week_end
0 2019-06-10 2019-06-15
1 2019-06-10 2019-06-15
2 2019-06-24 2019-06-29
3 2019-01-21 2019-01-26
40 2018-10-22 2018-10-27
但是,当我尝试np.busday_count
按如下方式使用该功能时:
df["b_days"] = np.busday_count(df['Week_init'], df['Week_fin'],
weekmask=bday_custom.weekmask, holidays=bday_custom.holidays)
我收到以下错误:
类型错误:迭代器操作数 0 dtype 无法根据规则“安全”从 dtype('M8[ns]') 转换为 dtype('M8[D]')
看起来该函数正在抱怨Week_init
和Week_end
dtypes,但我尝试更改它的类型但没有成功,如下所示:
df['Week_init'] = df['Week_init'].values.astype('datetime64[D]')
但是, dtype 保持为:
In [140]: df['Week_init'].dtype
Out[140]: dtype('<M8[ns]')
请问有什么帮助吗?
解决方案
推荐阅读
- wso2 - 如何在 WSO2 Micro Integrator 的数据服务中配置数据源?
- push-notification - 通过百度使用 Azure Notification Hub 向 Xamarin Android 应用发送通知
- reactjs - 在 React 和 Redux 中成功执行异步操作后,如何执行方法?
- javascript - 为什么不显示谷歌地图示例?
- javascript - 以 PHP-Variable 作为传输参数的 JavaScript 调用函数
- vuejs2 - Devextreme Vue Datagrid 一起配置选择和焦点行
- objective-c - #if DEBUG 在某些设备上无法用于 TestFlight 版本
- javascript - 有没有办法修复我的 CSS 和 JavaScript 中的电子邮件和标题验证?
- r - ggplot - 堆叠的 geom_bar - 将每个 x 按 y 重新排序
- excel - 如何为特定工作表运行 VBA 代码(仅在 sheet1 中)(与活动工作表无关)?