1

In time column, there are values like:

Time
========
 59:47
 59:52
 59:53
 59:55
 1:00:01
 1:00:03
 1:00:12

Now, I need to reshape the values like hh:mm:ss

I have tried something like this:

time_list = df7['Time'].tolist()

for i in time_list:
    print(datetime.strptime(i,'%H:%M:%S'))

ValueError: time data ' 36:21' does not match format '%H:%M:%S'

FObersteiner
  • 22,500
  • 8
  • 42
  • 72

1 Answers1

0

If you can normalise your values to always be of the form mm:ss or hh:mm:ss, with no additional components to them, then it's as simple as doing something like this:

for time_string in time_list:
    if time_string.count(':') == 1:
        time_string = '00:' + time_string
    print(datetime.strptime(time_string,'%H:%M:%S'))

Let's consider the sample data posted in the question:

time_list = ['59:47', '59:52', '59:53', '59:55', '1:00:01', '1:00:03', '1:00:12']

Here's what the output of this will look like:

1900-01-01 00:59:47
1900-01-01 00:59:52
1900-01-01 00:59:53
1900-01-01 00:59:55
1900-01-01 01:00:01
1900-01-01 01:00:03
1900-01-01 01:00:12

I think, though, that since this is time data (without a date component), time.strptime() would be a much better candidate: just use time.strptime() instead of datetime.strptime() to get something like this:

time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=59, tm_sec=47, tm_wday=0, tm_yday=1, tm_isdst=-1)

Hope that helps! :)

pradeepcep
  • 902
  • 2
  • 8
  • 24