首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从Pandas Timedelta获取总小时数?

从Pandas Timedelta获取总小时数?
EN

Stack Overflow用户
提问于 2015-07-08 11:24:53
回答 3查看 47K关注 0票数 57

如何获取Pandas时间增量中的总小时数?

例如:

代码语言:javascript
运行
复制
>>> td = pd.Timedelta('1 days 2 hours')
>>> td.get_total_hours()
26

注意:根据文档,.hours属性将返回小时组件

代码语言:javascript
运行
复制
>>> td.hours
2
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-07-08 11:32:31

只需找出其中包含多少个1小时的timedelta

代码语言:javascript
运行
复制
import numpy as np

>> td / np.timedelta64(1, 'h')
26.0
票数 84
EN

Stack Overflow用户

发布于 2015-07-08 16:28:31

试着告诉我们为什么熊猫会返回2个小时。

代码语言:javascript
运行
复制
import pandas as pd

td = pd.Timedelta('1 days 2 hours')

td.components

Out[45]: Components(days=1, hours=2, minutes=0, seconds=0, milliseconds=0, microseconds=0, nanoseconds=0)

td / pd.Timedelta('1 hour')

Out[46]: 26.0
票数 30
EN

Stack Overflow用户

发布于 2018-06-02 05:15:15

我得到以秒为单位的时间增量,然后除以3600得到小时数。

round(td.total_seconds() / 3600)

当我在jupyter notebook上测试时,这种方法运行得更快。

代码语言:javascript
运行
复制
%timeit td / np.timedelta64(1, 'h') 
The slowest run took 19.10 times longer than the fastest. This could mean that an intermediate result is being cached. 
100000 loops, best of 3: 4.58 µs per loop

%timeit round(td.total_seconds() / 3600)
The slowest run took 18.08 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 401 ns per loop
票数 20
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31283001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档