Trying to get the time delta between two date columns in a dataframe, where one column can possibly be “NaT”

我们两清 提交于 2019-12-11 15:13:32

问题


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'])

回答1:


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


来源:https://stackoverflow.com/questions/48647089/trying-to-get-the-time-delta-between-two-date-columns-in-a-dataframe-where-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!