PineNuts0
PineNuts0

Reputation: 5234

Python Pandas: Convert timedelta Value From Subtracting Two Dates Into Integer Datatype (AttributeError)

I have the following data set output (shown below) that was produced by the following code:

df_EVENT5_5['dtin'] = pd.to_datetime(df_EVENT5_5['dtin'])
df_EVENT5_5['age'] = df_EVENT5_5['dtin'].apply(dt.datetime.date) - df_EVENT5_5['dtbuilt'].apply(dt.datetime.date)

id  age
1   6252 days, 0:00:00
2   1800 days, 0:00:00
3   5873 days, 0:00:00

In the above data set, after running dtypes on the data frame, age appears to be an object.

I want to convert the "age" column into an integer datatype that only has the value of days. Below is my desired output:

id  age
1   6252 
2   1800 
3   5873 

I tried the following code:

df_EVENT5_5['age_no_days'] = df_EVENT5_5['age'].dt.total_seconds()/ (24 * 60 * 60) 

Below is the error:

AttributeError: Can only use .dt accessor with datetimelike values

Upvotes: 2

Views: 205

Answers (1)

sacuL
sacuL

Reputation: 51335

The fact that you are getting an object column suggests to me that there are some values that can't be interpreted as proper timedeltas. If that's the case, I would use pd.to_timedelta with the argument errors='coerce', then call dt.days:

df['age'] = pd.to_timedelta(df['age'],errors='coerce').dt.days
>>> df
   id   age
0   1  6252
1   2  1800
2   3  5873

Upvotes: 1

Related Questions