Home >Backend Development >Python Tutorial >How Can I Efficiently Calculate the Time Difference in Hours and Minutes Between Two Pandas DataFrame Columns?
Calculate Time Difference Between Two Pandas Columns in Hours and Minutes
Determining the time difference between two date columns in a DataFrame can be useful for various applications. However, the standard approach using the subtraction operator results in a datetime.timedelta object that includes days. To convert this into hours and minutes, we can leverage the as_type method.
Suppose we have two columns in our DataFrame, fromdate and todate, representing the start and end times respectively. Using the subtraction operator, we get the time difference as follows:
import pandas as pd data = {'todate': [pd.Timestamp('2014-01-24 13:03:12.050000'), pd.Timestamp('2014-01-27 11:57:18.240000'), pd.Timestamp('2014-01-23 10:07:47.660000')], 'fromdate': [pd.Timestamp('2014-01-26 23:41:21.870000'), pd.Timestamp('2014-01-27 15:38:22.540000'), pd.Timestamp('2014-01-23 18:50:41.420000')]} df = pd.DataFrame(data) df['diff'] = df['fromdate'] - df['todate']
This gives us the following output, which includes days:
todate fromdate diff 0 2014-01-24 13:03:12.050 2014-01-26 23:41:21.870 2 days 10:38:09.820000 1 2014-01-27 11:57:18.240 2014-01-27 15:38:22.540 0 days 03:41:04.300000 2 2014-01-23 10:07:47.660 2014-01-23 18:50:41.420 0 days 08:42:53.760000
To convert the time difference to only hours and minutes, we can use the as_type method to convert the datetime.timedelta object into a timedelta64 object with a specific unit of measure:
df['diff_hours'] = df['diff'].astype('timedelta64[h]')
This gives us the output:
todate fromdate diff diff_hours 0 2014-01-24 13:03:12.050 2014-01-26 23:41:21.870 2 days 10:38:09.820 58 1 2014-01-27 11:57:18.240 2014-01-27 15:38:22.540 0 days 03:41:04.300 3 2 2014-01-23 10:07:47.660 2014-01-23 18:50:41.420 0 days 08:42:53.760 8
As you can see, the diff_hours column now contains the time difference between the two dates in hours.
The above is the detailed content of How Can I Efficiently Calculate the Time Difference in Hours and Minutes Between Two Pandas DataFrame Columns?. For more information, please follow other related articles on the PHP Chinese website!