Python 循环内的工作日

Python Business Days within loop

我正在尝试遍历一个数据框,该数据框有两列,里面都有日期时间变量。我正在尝试遍历此数据库并生成一个新列,其中包含两个 dates.I 之间的工作日计数 dates.I 已尝试使用 np.busdays_count < 这返回了如下所示的错误。

df_Temp['Maturity(Stlm -Report Date)'] = np.busday_count(df_Temp['Today'],df_Temp['STLMT_DTE2'])


Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "<__array_function__ internals>", line 6, in busday_count
TypeError: Iterator operand 0 dtype could not be cast from dtype('<M8[ns]') to dtype('<M8[D]') according to the rule 'safe'

我也尝试过使用以下功能:

import datetime

def working_days(start_dt,end_dt):
    num_days = (end_dt -start_dt).days +1
    num_weeks =(num_days)//7
    a=0
    #condition 1
    if end_dt.strftime('%a')=='Sat':
        if start_dt.strftime('%a') != 'Sun':
            a= 1
    #condition 2
    if start_dt.strftime('%a')=='Sun':
        if end_dt.strftime('%a') !='Sat':
            a =1
    #condition 3
    if end_dt.strftime('%a')=='Sun':
        if start_dt.strftime('%a') not in ('Mon','Sun'):
            a =2
    #condition 4        
    if start_dt.weekday() not in (0,6):
        if (start_dt.weekday() -end_dt.weekday()) >=2:
            a =2
    working_days =num_days -(num_weeks*2)-a

    return working_days

请您建议使用另一种方法或对工作日功能进行改编,以使其正常工作,到目前为止,我有以下内容code.I希望我对此进行了足够详细的介绍。

for ns in (NettingSets):

    df_Temp = dfNetY[dfNetY['ACCT_NUM'] == ns]
    df_Temp['Current Credit Exposure'] = np.where(df_Temp['All NPV Flags']==1,0,df_Temp['MTM_AMT'])
    df_Temp['Positive Current Credit Exposure'] = np.where(df_Temp['Current Credit Exposure'] > 0,df_Temp['Current Credit Exposure'],0)
    df_Temp['SupervisoryFactor'] = 0.04
    df_Temp['STLMT_DTE2'] = pd.to_datetime(df_Temp['STLMT_DTE2'].astype(str), format='%Y-%m-%d')
    df_Temp['Today'] = date1
    df_Temp['Today'] = pd.to_datetime(df_Temp['Today'].astype(str), format='%Y-%m-%d')

for rows in df_Temp:
    df_Temp['Maturity(Stlm -Report Date)'] = np.busday_count(df_Temp['Today'],df_Temp['STLMT_DTE2'])

要使 np.busday_count 生效,两个日期都需要转换为 'M8[D]' 格式。

import datetime
import pandas as pd
import numpy as np

# Create a toy data frame
dates_1 = pd.date_range(datetime.datetime(2018, 4, 5, 0, 
                      0), datetime.datetime(2018, 4, 20, 7, 0),freq='D')

dates_2 = pd.date_range(datetime.datetime(2019, 4, 5, 0, 
                      0), datetime.datetime(2019, 4, 20, 7, 0),freq='D')

df_Temp = pd.DataFrame({'STLMT_DTE2': dates_1, 'Today': dates_2})
df_Temp.head()

df_Temp['Maturity(Stlm-Report Date)'] = np.abs(
        np.busday_count(df_Temp['Today'].values.astype('M8[D]'),
                        df_Temp['STLMT_DTE2'].values.astype('M8[D]')))

df_Temp.head()

输出:

df_Temp.head()
Out[16]: # Before calculating business days
  STLMT_DTE2      Today
0 2018-04-05 2019-04-05
1 2018-04-06 2019-04-06
2 2018-04-07 2019-04-07
3 2018-04-08 2019-04-08
4 2018-04-09 2019-04-09

df_Temp.head()
Out[17]: # After calculating business days
  STLMT_DTE2      Today  Maturity(Stlm-Report Date)
0 2018-04-05 2019-04-05                         261
1 2018-04-06 2019-04-06                         261
2 2018-04-07 2019-04-07                         260
3 2018-04-08 2019-04-08                         260
4 2018-04-09 2019-04-09                         261