2020-11-04
阅读量:
4778
DateFrame中两个日期字段的天数差值怎么求?

Dataframe中的时间是不能直接进行相加减的。如果将两列时间进行加减,会弹出类型错误:
TypeError: unsupported operand type(s) for -: 'str' and 'str'
需要先用pandas的to_datetime()方法,转化成时间格式进行加减
pd.to_datetime(data.REPICK_DATE)-pd.to_datetime(data.INST_DATE)

但是最后显示的是有天数,还有时间的差,如果只想保留天数,可以值行这个代码
(pd.to_datetime(data.INST_DATE)-pd.to_datetime(data.REPICK_DATE)).apply(lambda x:x.days)

28.0639
2
1
关注作者
收藏
推荐帖子
0条评论
0条评论
0条评论

发表评论
