为什么以下代码段表现如此糟糕:
import numpy
import pandas
time = numpy.array(range(0, 1000000, 10), dtype = numpy.uint32)
index = [ pandas.Timedelta(str(t) + 'ms') for t in time ]
在像样的桌面上大约需要 1.5 秒,我们说的只是一百万个 pandas.Timedelta。有什么想法可以重写最后一行吗?
请您参考如下方法:
如果需要TimedeltaIndex可以使用 to_timedelta或 TimedeltaIndex :
index = pd.to_timedelta(time, unit='ms')
或者:
index = pd.TimedeltaIndex(time, unit='ms')
