Stats DUB01
Stats DUB01

Reputation: 441

Numpy with multiple where conditions [TypeError: invalid type promotion]

I am trying to use multiple where conditions(mentioned below) in order to output a final date.

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))))) 

These are the datatypes for the other variables

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

I am getting the following error:

TypeError: invalid type promotion

I think the problem is regarding the data types and is a relatively small error, but I have been searching and can't find information to help me solve the problem

Upvotes: 1

Views: 980

Answers (1)

Stats DUB01
Stats DUB01

Reputation: 441

I have figured out the answer. The problem was with the pd.to_datetime('today') part. This had to be changed to reflect the data types of the other datetime parts

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']))))

Upvotes: 1

Related Questions