Heather Heather - 28 days ago 15
Python Question

Converting from hours and minutes to total minutes in Python

I have a Pandas DataFrame with a column of time strings in hours and minutes (e.g. 1 hour 8 mins). Some cells are only minutes (e.g. 47 mins).
I'm trying to convert from this format to just the integer value of the total number of minutes (e.g. 1 hour 8 mins would be 68).

I tried hard coding it but am having trouble with this as I am relatively new to Python.
Is there a library that would be able to help me with this?

In [10]: df_times = pd.DataFrame(times)
df_times.columns = ["times"]
df_times
Out[10]: times
0 31 mins
1 1 hour 28 mins
2 1 hour 1 min
3 1 min
... ...
22849 ERROR
22850 7 mins


In [11]: (pd.to_timedelta(df_times["times"].str.replace('mins','min')).dt.total_seconds()//60).astype(int)
ValueError: unit abbreviation w/o a number


And when I use errors="coerce":

In [12]: (pd.to_timedelta(df_times["times"].str.replace('mins','min'), errors="coerce").dt.total_seconds()//60).astype(int)
ValueError: Cannot convert NA to integer

Answer

you can use pandas.to_timedelta() and Series.dt.total_seconds() methods:

In [244]: df
Out[244]:
                  time
0        1 hour 8 mins
1              47 mins
2  10 hours 12 minutes
3                1 min

In [245]: (pd.to_timedelta(df.time.str.replace('mins', 'min'))
     ...:    .dt.total_seconds()//60).astype(int)
     ...:
Out[245]:
0     68
1     47
2    612
3      1
Name: time, dtype: int32
Comments