0

Is there any way to use pd.to_datetime() to convert string in H:M:S to time only?

When I use pd.to_datetime(), it give me 01/01/1900 09:08:20.

And subsequently, I want to compute deltaT = endTime - beginTime. Can I just subtract the time elements?

André Kool
  • 4,880
  • 12
  • 34
  • 44
JOHN
  • 1,411
  • 3
  • 21
  • 41
  • 1
    You can use [pandas.Series.dt.time](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.dt.time.html) attribute to get time. `pd.to_datetime(df, format='%H:%M:%S').dt.time` like this. And you can just subtract the columns to find the timedelta. Or do something like [this answer](https://stackoverflow.com/a/22924683/826970). Can you edit in a sample input and expected output to your question? – umutto Feb 23 '18 at 08:15

2 Answers2

1

I think not.

But you can convert both columns to datetime and then get difference. Dates are some so get difference of times only. Last you can convert difference to seconds by seconds or total_seconds if possible time1 < time2:

df['diff'] = (pd.to_datetime(df['time1']) - 
              pd.to_datetime(df['time2']))

Sample:

df = pd.DataFrame({'time1':['09:08:20','09:20:20','19:08:20'],
                   'time2':['09:07:20','09:20:20','19:08:10']})

print (df)
      time1     time2
0  09:08:20  09:07:20
1  09:20:20  09:20:20
2  19:08:20  19:08:10

df['diff'] = abs(pd.to_datetime(df['time1']) - 
              pd.to_datetime(df['time2'])).dt.seconds

print (df)

      time1     time2  diff
0  09:08:20  09:07:20    60
1  09:20:20  09:20:20     0
2  19:08:20  19:08:10    10
Anton vBR
  • 18,287
  • 5
  • 40
  • 46
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Yep I also think you need to include full daettime to handle time. +1Specifying format is not necessary in this case. – Anton vBR Feb 23 '18 at 09:27
  • @AntonvBR - Thank you. Just one thing, OP yesterday change [this question](https://stackoverflow.com/q/48865605/2901002), I think you can edited answer ;) – jezrael Feb 23 '18 at 09:30
  • @AntonvBR - Thank you, I test it and it working for positive timedelta very nice. But for negative timedeltas seems better is `total_seconds`. – jezrael Feb 23 '18 at 09:42
  • 1
    @AntonvBR - It depends of data, so I think solution now is OK ;) – jezrael Feb 23 '18 at 09:43
  • added abs() before. SO is slow for my part and I'm going on lunch. I'll look at the linked question soon. – Anton vBR Feb 23 '18 at 09:47
  • @AntonvBR - I want ask it, today is SO totaly broken for me too. – jezrael Feb 23 '18 at 09:48
0

You should use the datetime module, first strip your time using strptime() and then rebuilt it with the strftime():

import datetime
time = "2017-10-16T08:27:16+0000"
stripedTime = datetime.datetime.strptime(time, '%Y-%m-%dT%I:%M:%S%z')

newTime = stripedTime.strftime('%H:%M:%S')
print(newTime)
Gwendal Grelier
  • 526
  • 1
  • 7
  • 21