python-3.x - condition statements in pandas
问题描述
I have a dataframe that has the following columns:
- Date1
- Date2
- Duration (in days)
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
解决方案
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
推荐阅读
- python - 运行“conda update --all”会导致错误。[苹果系统]
- python - Python - 将 numpy 数组转换为 JSON
- caching - 即使我将元数据缓存控制设置为几天,Amazon S3 缓存也不起作用
- python - 从技术上讲,什么是 numpy.mgrid?
- node.js - Mongoose - 使用后更新字段的单个语句
- spring - JPA,为什么不替换本机查询中的命名参数?
- javascript - 通过多个键过滤对象数组
- javascript - 解析html文件时如何强制不发送其他请求?
- c++ - 从图中按与给定顶点共享的相邻点数的顺序打印顶点
- javascript - 用嵌套对象反应更新状态