以小时和分钟计算两列之间的Pandas DataFrame时差

时间:2020-12-22 19:16:26

I have two columns from and to date in a dataframe

我在数据框中有两列和迄今为止的列

when I try add new column diff with to find the difference between two date using

当我尝试添加新的列差异时,找到两个日期之间的差异使用

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

I get the diff column in days if more than 24 hours.

如果超过24小时,我会在几天内得到差异列。

2014-01-24 13:03:12.050000,2014-01-26 23:41:21.870000,"2 days, 10:38:09.820000"
2014-01-27 11:57:18.240000,2014-01-27 15:38:22.540000,03:41:04.300000
2014-01-23 10:07:47.660000,2014-01-23 18:50:41.420000,08:42:53.760000

How do I convert my results only in hours and minutes ignoring days and even seconds.

如何仅在小时和分钟内转换结果,忽略天数甚至秒数。

2 个解决方案

#1


54  

Pandas timestamp differences returns a datetime.timedelta object. This can easily be converted into hours by using the *as_type* method, like so

Pandas时间戳差异返回datetime.timedelta对象。这可以通过使用* as_type *方法轻松转换为小时,就像这样

import pandas
df = pandas.DataFrame(columns=['to','fr','ans'])
df.to = [pandas.Timestamp('2014-01-24 13:03:12.050000'), pandas.Timestamp('2014-01-27 11:57:18.240000'), pandas.Timestamp('2014-01-23 10:07:47.660000')]
df.fr = [pandas.Timestamp('2014-01-26 23:41:21.870000'), pandas.Timestamp('2014-01-27 15:38:22.540000'), pandas.Timestamp('2014-01-23 18:50:41.420000')]
(df.fr-df.to).astype('timedelta64[h]')

to yield,

屈服,

0    58
1     3
2     8
dtype: float64

#2


19  

This was driving me bonkers as the .astype() solution above didn't work for me. But I found another way. Haven't timed it or anything, but might work for others out there:

这让我疯狂,因为上面的.astype()解决方案对我不起作用。但我发现了另一种方式。没有时间或任何时间,但可能为那里的其他人工作:

t1 = pd.to_datetime('1/1/2015 01:00')
t2 = pd.to_datetime('1/1/2015 03:30')

print pd.Timedelta(t2 - t1).seconds / 3600.0

...if you want hours. Or:

......如果你想要几个小时。要么:

print pd.Timedelta(t2 - t1).seconds / 60.0

...if you want minutes.

......如果你想要分钟。

#1


54  

Pandas timestamp differences returns a datetime.timedelta object. This can easily be converted into hours by using the *as_type* method, like so

Pandas时间戳差异返回datetime.timedelta对象。这可以通过使用* as_type *方法轻松转换为小时,就像这样

import pandas
df = pandas.DataFrame(columns=['to','fr','ans'])
df.to = [pandas.Timestamp('2014-01-24 13:03:12.050000'), pandas.Timestamp('2014-01-27 11:57:18.240000'), pandas.Timestamp('2014-01-23 10:07:47.660000')]
df.fr = [pandas.Timestamp('2014-01-26 23:41:21.870000'), pandas.Timestamp('2014-01-27 15:38:22.540000'), pandas.Timestamp('2014-01-23 18:50:41.420000')]
(df.fr-df.to).astype('timedelta64[h]')

to yield,

屈服,

0    58
1     3
2     8
dtype: float64

#2


19  

This was driving me bonkers as the .astype() solution above didn't work for me. But I found another way. Haven't timed it or anything, but might work for others out there:

这让我疯狂,因为上面的.astype()解决方案对我不起作用。但我发现了另一种方式。没有时间或任何时间,但可能为那里的其他人工作:

t1 = pd.to_datetime('1/1/2015 01:00')
t2 = pd.to_datetime('1/1/2015 03:30')

print pd.Timedelta(t2 - t1).seconds / 3600.0

...if you want hours. Or:

......如果你想要几个小时。要么:

print pd.Timedelta(t2 - t1).seconds / 60.0

...if you want minutes.

......如果你想要分钟。