0

I want to subtract the time between 2 column: I have been using this:

row['window'] = datetime.datetime.strptime(row['Time'], '%H:%M:%S.%f') - datetime.datetime.strptime(row['announcement_time'], '%H:%M:%S')

This works and gives a result that look like this:

 window
-1 day, 23:29:00
-1 day, 23:29:05
-1 day, 23:29:10
-1 day, 23:29:15
-1 day, 23:29:20
-1 day, 23:29:25

However, I only want the time difference i.e. - 00:00:30. I have tried :

 row['window'] = datetime.datetime.strptime(row['Time'], '%H:%M:%S.%f').time() - datetime.datetime.strptime(row['announcement_time'], '%H:%M:%S').time()

But this gives an error :

TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time'

I have also tried .hour, .minute and .second but does not work very well. Can someone please help.Thanks

duckman
  • 687
  • 1
  • 15
  • 30
  • Have you tried to calculate the difference before formating it? I would try sth like datetime.datetime.strptime(row['time'] - row['announcement_time'], '%H:%M:%S') – fuuman May 24 '16 at 07:10
  • Could you post some of the actual input data as well? – acdr May 24 '16 at 07:13
  • Maybe you want to store only seconds: `(datetime.datetime.strptime(row['Time'], '%H:%M:%S.%f') - datetime.datetime.strptime(row['announcement_time'], '%H:%M:%S')).total_seconds()` – Daniel May 24 '16 at 07:16
  • @fuuman: I tried that but they are in different formats so I need to convert to datetime object – duckman May 25 '16 at 00:39
  • @acdr: the row['Time[L]) has this format HH:MM:SS.000 eg: 14:20:04.000 and the row['announcement_time'] has HH:MM:SS format eg: 15:44:22T – duckman May 25 '16 at 00:42

2 Answers2

0

Time objects don't have arithmetic that I'm aware of. For best practice you should use (datetime.datetime object - datetime.datetime object).days for example.

You can read more about it here.

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Rotem Vil
  • 224
  • 1
  • 8
0

-1 day, 23:29:25 is how negative timedeltas are printed. To get 00:00:30, use abs():

>>> from datetime import timedelta
>>> -timedelta(seconds=30)
datetime.timedelta(-1, 86370)
>>> str(-timedelta(seconds=30))
'-1 day, 23:59:30'
>>> str(abs(-timedelta(seconds=30)))
'0:00:30'

If timedelta() is more than a day, you could apply modulo arithmetic, to get the time difference within a day:

>>> str(abs(-timedelta(days=10, seconds=30)))
'10 days, 0:00:30'
>>> str(abs(-timedelta(days=10, seconds=30)) % timedelta(days=1))
'0:00:30'

On Python 2, % timedelta() is not implemented. You could use .total_seconds() % 3600 instead or if you need only the whole number of seconds then just access .seconds attribute (it is always less than a day):

>>> abs(-timedelta(days=10, seconds=30)).seconds
30
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • thank you for the detailed answer. However, I would like to get the -30 second or -10:20 (negative 10 minutes and 20 secs). or is there away to reformat "-1 day, 23:29:05"? – duckman May 25 '16 at 00:46
  • @duckman yes. If you don't need to change the value and you want only to format it differently then it is a [separate question](http://stackoverflow.com/q/538666/4279). In your case, you could add at the end: `if diff < timedelta(0): result = '-' + result` – jfs May 25 '16 at 03:08