3

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?

Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
And_Dev
  • 113
  • 2
  • 12

1 Answers1

3

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.

Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
  • neh, just easier to add more aliases for things (e.g. hrs -> h) internally. These are unambiguous so its pretty clear what the units mean. And ``sec`` and ``min`` are already there. – Jeff Oct 12 '15 at 21:18
  • 2
    ```In [15]: pd.to_timedelta(s.str.replace('hrs','h')) Out[15]: 0 22:07:27 1 00:10:10 2 00:31:19 dtype: timedelta64[ns]``` – Jeff Oct 12 '15 at 21:19