在pandas中使用小时单位的to_timedelta
在pandas的文档中,pandas.to_timedelta(arg, box=True, unit='ns')的unit参数有如下解释:
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 个回答
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')