Luis Valencia
Luis Valencia

Reputation: 33998

add calculated column to pandasdataframe depending on NULL values

I have a dataframe like this enter image description here

I need to add two columns:

df['paidInDays'] = df['sdDatePaidCancelled'].sub(df['sdBillDate'], axis=0) / np.timedelta64(1,'D')
df['toBePaidInDays'] = df['sdDueDate'].sub(df['sdBillDate'], axis=0) / np.timedelta64(1,'D')

However some rows have sdDatePaidCancelled NULL and I cant neither convert it to datetime neither make the calculation.

Any tips to get through this?

Upvotes: 1

Views: 125

Answers (1)

jezrael
jezrael

Reputation: 862671

It seems you need convert values to datetimes, then NULL are converted to NaT and you can subtract values:

cols = ['sdDatePaidCancelled','sdBillDate','sdDueDate']
df[cols] = df[cols].apply(pd.to_datetime)
#if possible some non datetimeslike values 
#df[cols] = df[cols].apply(pd.to_datetime, errors='coerce')

Upvotes: 1

Related Questions