Alt1139
Alt1139

Reputation: 21

Converting HH:MM:SS to seconds in python from a csv file

I want to convert this into seconds

[44206 rows x 30 columns]

    0        00:00:00
    1        00:00:10
    2        00:00:00
    3        00:00:20
    4        00:00:22
      
    44201    01:51:02
    44202    01:51:12
    44203    01:51:22
    44204    01:51:32
    44205    01:51:42
    Name: Cumulative Time, Length: 44206, dtype: object

For that I used the following code

def step_time(x):
    time_list=x.split(":")
    print (time_list)
    time = (3600 * float(time_list[-3]) + 60 * float(time_list[-2]) + float(time_list[-1]))
    return time

When I use this code I get the error message "AttributeError: 'Series' object has no attribute 'split'"

I then changed the x.split(":") to x.str.split(":")

Then I get the time_list as:

[44206 rows x 30 columns]
0        [00, 00, 00]
1        [00, 00, 10]
2        [00, 00, 00]
3        [00, 00, 20]
4        [00, 00, 22]
    
44201    [01, 51, 02]
44202    [01, 51, 12]
44203    [01, 51, 22]
44204    [01, 51, 32]
44205    [01, 51, 42]
Name: Cumulative Time, Length: 44206, dtype: object

Now it looks like I have an array in an array have no idea what I should do next.

Upvotes: 0

Views: 212

Answers (1)

jezrael
jezrael

Reputation: 862611

Use:

df['New'] = pd.to_timedelta(df['Cumulative Time']).dt.total_seconds()

Upvotes: 1

Related Questions