0

I have two date-hour column A and B of type shown below. Both colum are in a Dataframe (pandas).

yyyy-mm-dd hh:mm:ss

I create

df['difference'] = df['A'] - df['B']

I get a format like

0 days 00:01:13

I would prefer to have a column which contains the seconds in integer. For instance, I need to get 73 in my above example instead of 1min13.

How to do that?

S12000
  • 3,345
  • 12
  • 35
  • 51
  • Possible duplicate of https://stackoverflow.com/questions/4362491/how-do-i-check-the-difference-in-seconds-between-two-dates – san Oct 13 '19 at 01:38

2 Answers2

2

We can use total_seconds

(df['A'] - df['B']).dt.total_seconds()
BENY
  • 317,841
  • 20
  • 164
  • 234
0
from datetime import datetime, time
#Specified date
date1 = datetime.strptime('2015-01-01 01:00:00', '%Y-%m-%d %H:%M:%S')
import datetime
old_time = date1
print(old_time)
new_time = old_time - datetime.timedelta(hours=2, minutes=10)
print(new_time)
Pasha
  • 134
  • 1
  • 7