Home > database >  Calculate difference between two dates in python
Calculate difference between two dates in python

Time:07-02

I have two columns date1 and date2 in 2/23/2022 12:30:26 format ,i want to calculate difference in hours. How to implement .

CodePudding user response:

You can convert two columns to datetime type then subtract at last get hours from timedelta object.

df['date1'] = pd.to_datetime(df['date1'])
df['date2'] = pd.to_datetime(df['date2'])

df['diff'] = (df['date1']-df['date2']) / pd.Timedelta(hours=1)
  • Related