首页 > 解决方案 > 在 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_initWeek_enddtypes,但我尝试更改它的类型但没有成功,如下所示:

df['Week_init'] = df['Week_init'].values.astype('datetime64[D]')

但是, dtype 保持为:

In [140]: df['Week_init'].dtype
Out[140]: dtype('<M8[ns]')

请问有什么帮助吗?

标签: pythonpandas

解决方案


推荐阅读