Search code examples
pythonpandasstring-to-datetime

Extract hour/min/seconds from Series


I have to extract hour/min/sec form "session" column and add them to time. Is there any way to do this using pandas?

I have something like this:

0    22 hrs 7 min 27 sec
1          10 min 10 sec
2          31 min 19 sec
Name: session, dtype: object

And I want to convert this column value to seconds and then add the total seconds to my other time column. This will give me the total time spend.

Any suggestions on how to split this kind of column in df?


Solution

  • If you tweak the format you can use to_timedelta (which you can then add to a datetime columns):

    In [11]: s
    Out[11]:
    0    22 hrs 7 min 27 sec
    1          10 min 10 sec
    2          31 min 19 sec
    Name: delta, dtype: object
    
    In [12]: s.str.replace('rs|in|ec', '')
    Out[12]:
    0    22 h 7 m 27 s
    1        10 m 10 s
    2        31 m 19 s
    Name: delta, dtype: object
    
    In [13]: pd.to_timedelta(s.str.replace('rs|in|ec', ''))
    Out[13]:
    0   22:07:27
    1   00:10:10
    2   00:31:19
    Name: delta, dtype: timedelta64[ns]
    

    Perhaps a format argument to to_timedelta would be a good enhancement.