【问题标题】:Speed up conversion of timestamp to datetime Python加快时间戳到日期时间 Python 的转换
【发布时间】:2016-10-11 22:01:53
【问题描述】:

我正在使用 python 和 pytables 以及相当大的数据集 (+200GB) 制作期货市场报价数据回放系统。

据我所知,pytables 只能为我的时间戳存储 numpy datetime64 对象。这是一个问题,因为我需要将它们转换为 datetime 对象或 pandas 时间戳,以便交易模块可以调用传入数据的时间或工作日或月份等方法。试图在运行时转换数十亿行基本上会使系统无法使用。

pd.to_datetime(my_datetime64)
datetime.datetime(my_datetime64)

两者都太慢了。

以下是我将数千个原始 csv 导入 pytables 存储的方法。请注意,索引采用 pandas 日期时间格式,它允许我获取有关时间戳的信息,例如时间、月份、年份等

from pandas import HDFStore
store = HDFStore(store_dir)

for file in files:
            df = pd.read_csv("/TickData/"+file)
            df.index = pd.to_datetime(df['date'].apply(str) + " " + df['time'], format = '%Y%m%d %H:%M:%S.%f')
            df.drop(['date', 'time'], axis=1, inplace=True)
            store.append('ticks', df, complevel=9, complib='blosc')

当我使用 PyTables table.read 方法读回一个块时,数据如下所示 - 您可以看到 pandas 时间戳都已转换为 datetime64

array([(1220441851000000000, [b'ESU09'], [1281.0], [1]),
       (1226937439000000000, [b'ESU09'], [855.75], [2]),
       (1230045292000000000, [b'ESU09'], [860.0], [1]), ...,
       (1244721917000000000, [b'ESU09'], [943.75], [1]),
       (1244721918000000000, [b'ESU09'], [943.75], [2]),
       (1244721920000000000, [b'ESU09'], [944.0], [15])], 
      dtype=[('index', '<i8'), ('values_block_0', 'S5', (1,)), ('values_block_1', '<f8', (1,)), ('values_block_2', '<i8', (1,))])

这就是我如何将它们从表中分块读出

    chunksize = 100000
    nrows = 1000000000
    n_chunks =  nrows//chunksize + 1
    h5f = tables.open_file(store_directory, 'r')
    t = h5f.get_node('/', 'ticks')

    for i in range(n_chunks):
         chunk = t.table.read(i*chunksize, (i+1)*chunksize)
             for c in chunk:
                  #this is where we would convert c[0] which is the timestamp , 
pd.to_datetime(c[0]) or datetime.datetime(c[0]), both are too slow

我的问题最终是:

1:有没有更快的方法将 datetime64 转换回 datetimes 或 pandas 时间戳,可能与 cython 有关?

OR 2:有没有办法将 pandas 时间戳存储在 HDF 中,这样它们就不需要在读取时进行转换?

谢谢

【问题讨论】:

  • 您的时间戳有多独特,分辨率是多少?
  • @rrauenza ,它们可能是 85% 唯一的毫秒分辨率
  • 好的,如果值重复很多,我会建议 lru memoization。
  • 顺便说一句,我无法让datetime.datetime(numpy.datetime64(datetime.utcnow())) 工作:*** TypeError: don't know how to convert scalar number to int

标签: python datetime cython hdf5 pytables


【解决方案1】:

试试这个:

import numpy
from datetime import datetime

npdt = numpy.datetime64(datetime.utcnow())
dt = npdt.astype(datetime)

我发现它要快一个数量级:

from datetime import datetime
import numpy
import pandas
import timeit

foo = numpy.datetime64(datetime.utcnow())
print(foo.astype(datetime))
print(pandas.to_datetime(foo))

print(timeit.timeit('foo.astype(datetime)',    setup='import numpy; import pandas; from datetime import datetime; foo = numpy.datetime64(datetime.utcnow())'))
print(timeit.timeit('pandas.to_datetime(foo)', setup='import numpy; import pandas; from datetime import datetime; foo = numpy.datetime64(datetime.utcnow())'))

输出:

2016-06-10 20:51:11.745616
2016-06-10 20:51:11.745616
1.916042190976441
37.38387820869684

【讨论】:

猜你喜欢
  • 2012-05-04
  • 2020-06-15
  • 2014-08-23
  • 2019-02-03
  • 2016-03-21
  • 1970-01-01
  • 2014-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多