首页 > 解决方案 > condition statements in pandas

问题描述

I have a dataframe that has the following columns:

Where Date2 = Date1+duration.

I want to add a third date (date3) where date3 = date2+x,

where:

 Date3 = date2+15 if duration <30days   
 Date3 = date2+20 if  durations is from >=30days to  <=180days  
 Date3 = date2+30 if duration >180days.

My code is:

conditions =[
    (df['duration'] <30),
    (df['duration'] >= 30) & df['duration'] <=180),
    (df['duration'] >180)]
choices = [15,20,30]
df['date3'] = np.select(conditions,df['date2']+pd.to_timedelta(np.select(conditions, choices, default='null'),unit='d'))

If date2 = 2019-09-21, and duration is <30,

then date3 is outputted as 2019-09-21 00:00:00.00000015

so it’s considering the 15 as nanoseconds and not as days.

Any thoughts on how to make it understand that the 15 is days not nanoseconds?

I have the correct syntax for the to_timedelta statement, but not sure why its taking the delta as nanoseconds and not days.

Any help would be greatly appreciated.

Cheers,

-Big_ears

标签: python-3.xpandasconditional-statements

解决方案


OK...so I solved it... In the choices, instead of

choices[15,20,30]

I had to write:

choices[pd.to_timedelta(15,units='days'),pd.to_timedelta(20,units='days'),pd.to_timedelta(30,units='days')]

-big_ears


推荐阅读