我正在尝试遍历一个数据框,该数据框有两列,里面都有日期时间变量。我正在尝试遍历此数据库并生成一个新列,其中包含两个日期之间的工作日数。我尝试使用 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
请您建议使用另一种方法或对工作日功能进行调整以使其正常工作,到目前为止,我有以下代码。我希望我对此进行了足够详细的介绍。
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'])