试图获得数据帧中两个日期列之间的时间增量,其中一个列可能是"NaT". [英] Trying to get the time delta between two date columns in a dataframe, where one column can possibly be "NaT"

查看:123
本文介绍了试图获得数据帧中两个日期列之间的时间增量,其中一个列可能是"NaT".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从SQL数据库中提取两个日期之间的天数差异.一个是开始日期,另一个是完成日期.在这种情况下,完成日期可以并且是NaT值.本质上,我想遍历每一行,并求差,如果完成的日期是NaT,我想跳过它或在一个全新的delta列中分配NaN值.下面的代码给我这个错误:'member_descriptor'对象不可调用

I am trying to get the difference in days between two dates pulled from a SQL database. One is the start date, the other is a completed date. The completed date can and is, in this case, be a NaT value. Essentially I'd like to iterate through each row, and take the difference, if the completed date is NaT I'd like to skip it or assign a NaN value, in a completely new delta column. the code below is giving me this error: 'member_descriptor' object is not callable

for n in df.FINAL_DATE:
  df.DELTA = [0]
  if n is None:
    df.DELTA = None
    break
  else:
    df.DELTA = datetime.timedelta.days(df['FINAL_DATE'], df['START_DATE'])

推荐答案

因此,您首先必须检查是否有任何日期是NaT.如果没有,您可以通过以下方式计算:

So, you first have to check if any of the dates are NaT. If not, you can calculate by:

df.DELTA = (df['FINAL_DATE'] -  df['START_DATE']).days

这篇关于试图获得数据帧中两个日期列之间的时间增量,其中一个列可能是"NaT".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆