Python 以小时和分钟为单位计算两列之间的 Pandas DataFrame 时间差
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22923775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculate Pandas DataFrame Time Difference Between Two Columns in Hours and Minutes
提问by sbalajis
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.
我如何只在几小时和几分钟内转换我的结果,而忽略几天甚至几秒钟。
采纳答案by nitin
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
回答by elPastor
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.
...如果你想要几分钟。