Home > front end >  'TimedeltaProperties' object has no attribute 'strftime'
'TimedeltaProperties' object has no attribute 'strftime'

Time:01-13

Every time I use this for the below problem I get: 'TimedeltaProperties' object has no attribute 'strftime'

df = pd.DataFrame({'Start_Date':pd.to_datetime(['2018-01-27','2018-08-31']),'End_Date':pd.to_datetime(['2019-03-13','2019-12-13'])})
Start_Date End_Date
2018-01-27 00:00:00 2019-12-13 00:00:00
2018-08-31 00:00:00 2019-03-13 00:00:00
diff = df['End_Date'] - df['Start_Date']

I would like diff to be like :('%Y :: %M :: %D')

How I can get that result???

CodePudding user response:

You can do that with the dateutil library:

pip install dateutil

It's not vectorized so you have to do it in a loop, which is a lot slower than built-in pandas functions:

from dateutil.relativedelta import relativedelta

def get_duration(row):
    delta = relativedelta(row["End_Date"], row["Start_Date"])
    return f"{delta.years} years {delta.months} months {delta.days} days"

df.apply(get_duration, axis=1)

CodePudding user response:

The difference could be in that similar format using the library relativedelta from dateutil, for example

import pandas as pd
from dateutil.relativedelta import relativedelta

df = pd.DataFrame({'Start_Date':pd.to_datetime(['2018-01-27','2018-08-31']),'End_Date':pd.to_datetime(['2019-03-13','2019-12-13'])})
cols = ['End_Date', 'Start_Date']
df['relativedelta'] = df[cols].apply(lambda x: relativedelta(x[0], x[1]), axis=1)
print(df)

Output:

  Start_Date   End_Date                                 relativedelta
0 2018-01-27 2019-03-13  relativedelta(years= 1, months= 1, days= 14)
1 2018-08-31 2019-12-13  relativedelta(years= 1, months= 3, days= 13)

And to have it in your desired format ('%Y :: %M :: %D'), you could do

df['diff'] = df['relativedelta'].apply(lambda x: f'{x.years} :: {x.months} :: {x.days}')
print(df)

Output

  Start_Date   End_Date                                 relativedelta           diff 
0 2018-01-27 2019-03-13  relativedelta(years= 1, months= 1, days= 14)   1 :: 1 :: 14 
1 2018-08-31 2019-12-13  relativedelta(years= 1, months= 3, days= 13)   1 :: 3 :: 13 
  • Related