Numpy与多个where条件[TypeError:无效类型提升]

时间:2018-01-23 03:57:40

标签: python numpy

我正在尝试使用多个where条件(如下所述)来输出最终日期。

np.where( (loan_plans.days_since_end.astype('timedelta64[D]')>0.0) & (loan_plans.principal_outstanding.astype('float')<1000.0), loan_plans.max_repaid_date, 
     (np.where( (loan_plans.days_since_end.astype('timedelta64[D]')>0.0) & (loan_plans.principal_outstanding.astype('float')>1000.0), pd.to_datetime('today')
               (np.where( loan_plans.days_since_end.astype('timedelta64[D]')<0.0, loan_plans.final_repayment_date, 0.0))))) 

这些是其他变量的数据类型

loan_plans.max_repaid_date=datetime64[ns]
loan_plans.final_repayment_date = datetime64[ns]

我收到以下错误:

  

TypeError:无效的类型提升

我认为问题与数据类型有关,并且是一个相对较小的错误,但我一直在搜索,无法找到信息来帮助我解决问题

1 个答案:

答案 0 :(得分:1)

我已经找到了答案。问题出在pd.to_datetime('今天')部分。必须更改它以反映其他日期时间部分的数据类型

loan_plans['final_date'] = np.where( (loan_plans.days_since_end.astype('timedelta64[D]')>=0) & (loan_plans.principal_outstanding.astype('float')<1000), pd.to_datetime(loan_plans['max_repaid_date']),
     np.where( (loan_plans.days_since_end.astype('timedelta64[D]')>0) & (loan_plans.principal_outstanding.astype('float')>1000), np.datetime64('today'),
               np.where( loan_plans.days_since_end.astype('timedelta64[D]')<0, pd.to_datetime(loan_plans['final_repayment_date']), pd.to_datetime(loan_plans['final_repayment_date']))))