在pandas to_timedelta中使用小时单位

时间:2014-09-01 02:20:44

标签: python pandas

pandas文件中的pandas.to_timedelta(arg,box = True,unit ='ns'),单位解释如下:

unit of the arg (D,h,m,s,ms,us,ns) denote the unit, which is an integer/float number

所以我认为“h”应该是指小时。但似乎我错了,因为下面的例子没有按预期工作:

import pandas as pd

base = pd.to_datetime("00:00", format="%H:%M")
print "base:",base
x = base + pd.to_timedelta(1,unit='s')
print "x:",x
y = base + pd.to_timedelta(1,unit='h')
print "y:",y

输出结果为:

base: 1900-01-01 00:00:00
x: 1900-01-01 00:00:01
y: 1900-01-01 00:00:00.000000001

但我希望y应该是01:00:00。什么是以小时为单位的正确方法?

1 个答案:

答案 0 :(得分:3)

[从评论中移开关闭它:]

这是一个错误,请参阅GH #7611,并修复为0.14.1:

>>> pd.__version__
'0.14.1'
>>> base = pd.to_datetime("00:00", format="%H:%M")
>>> base + pd.to_timedelta(1,unit='s')
Timestamp('1900-01-01 00:00:01')
>>> base + pd.to_timedelta(1,unit='h')
Timestamp('1900-01-01 01:00:00')