小能豆

计算两个 Pandas 列之间的时间差(以小时和分钟为单位)

javascript

我在数据框中有两列fromdatetodate

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)

我添加了一个新列,diff使用来查找两个日期之间的差异

df['diff'] = df['fromdate'] - df['todate']

我得到了该diff列,但是days当超过 24 小时时它包含。

                   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

如何将我的结果转换为小时和分钟(即将天转换为小时)?


阅读 38

收藏
2024-07-04

共1个答案

小能豆

To convert the time difference to only hours and minutes (i.e., convert days to hours), you can use the total seconds and then calculate the hours and minutes from it. Here’s how you can do it:

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)

# Calculate the difference
df['diff'] = df['fromdate'] - df['todate']

# Convert the difference to total seconds, then to hours and minutes
df['diff_hours'] = df['diff'].dt.total_seconds() // 3600
df['diff_minutes'] = (df['diff'].dt.total_seconds() % 3600) // 60

# Create a new column with hours and minutes
df['diff_in_hours_minutes'] = df['diff_hours'].astype(int).astype(str) + ' hours ' + df['diff_minutes'].astype(int).astype(str) + ' minutes'

print(df[['todate', 'fromdate', 'diff_in_hours_minutes']])

This will produce the following output:

                   todate                 fromdate diff_in_hours_minutes
0 2014-01-24 13:03:12.050  2014-01-26 23:41:21.870         58 hours 38 minutes
1 2014-01-27 11:57:18.240  2014-01-27 15:38:22.540          3 hours 41 minutes
2 2014-01-23 10:07:47.660  2014-01-23 18:50:41.420          8 hours 42 minutes

Here’s what each step does:
1. df['diff'] calculates the difference between fromdate and todate.
2. df['diff'].dt.total_seconds() converts the time difference to total seconds.
3. df['diff_hours'] calculates the total hours by dividing total seconds by 3600.
4. df['diff_minutes'] calculates the remaining minutes by taking the remainder of total seconds divided by 3600 and then dividing by 60.
5. df['diff_in_hours_minutes'] concatenates the hours and minutes into a readable string format.

2024-07-04