TypeError: cannot astype a datetimelike from [datetime64[ns]] to [timedelta64[D]]

  datetime, python

So I am trying to calculate the duration between two days but I just want the number of days instead of minutes and seconds for the duration. The code I am using is the following:

df['duration']=df['deadline'].sub(df['created_at'].astype('datetime64[D]'))

However, this gives me sth like : 35 days 12:42:40. But I don’t want the "days 12:42:40." Instead, I want to extract the 35 as an interger. So I tried to replace the datetime64[D] with [timedelta64[D], which gave me the TypeError. Is there any way for me to do this?

Source: Python Questions

LEAVE A COMMENT