0

我有一个包含 ohlc 数据的列表:

[(1360710000.0, 69.06, 69.67, 68.53, 69.53, 550000.0, 69.53),
(1360623600.0, 68.98, 69.05, 68.39, 68.94, 604200.0, 68.94)]

并希望它转换成一个结构如下的 numpy 数组:

array([ (1352934000, 63.130001068115234, 63.59000015258789, 62.900001525878906, 63.16999816894531, 816300.0, 63.16999816894531),
   (1353020400, 62.970001220703125, 63.529998779296875, 62.599998474121094, 62.61000061035156, 942000.0, 62.61000061035156),
   (1353279600, 63.0, 64.97000122070312, 63.0, 64.87999725341797, 812800.0, 64.87999725341797),
   (1353366000, 64.44999694824219, 65.27999877929688, 64.29000091552734, 65.23999786376953, 467700.0, 65.23999786376953),
   (1353452400, 65.16999816894531, 65.4000015258789, 64.58999633789062, 64.8499984741211, 417700.0, 64.8499984741211)], 
  dtype=[('time', '<i4'), ('open', '<f4'), ('high', '<f4'), ('low', '<f4'), ('close', '<f4'), ('volume', '<f4'), ('amount', '<f4')])

任何尝试都会导致预期的错误结构:

np.array(list, dtype=Day)

array([[1360710000.0, 69.06, 69.67, 68.53, 69.53, 550000.0, 69.53],
   [1360623600.0, 68.98, 69.05, 68.39, 68.94, 604200.0, 68.94]], dtype=object)
4

1 回答 1

4
lst = [(1360710000.0, 69.06, 69.67, 68.53, 69.53, 550000.0, 69.53),
       (1360623600.0, 68.98, 69.05, 68.39, 68.94, 604200.0, 68.94)]
np.array(lst, dtype=[('time', '<i4'), ('open', '<f4'), ('high', '<f4'), ('low', '<f4'), ('close', '<f4'), ('volume', '<f4'), ('amount', '<f4')])

返回

array([ (1360710000, 69.05999755859375, 69.66999816894531, 68.52999877929688, 69.52999877929688, 550000.0, 69.52999877929688),
       (1360623600, 68.9800033569336, 69.05000305175781, 68.38999938964844, 68.94000244140625, 604200.0, 68.94000244140625)], 
      dtype=[('time', '<i4'), ('open', '<f4'), ('high', '<f4'), ('low', '<f4'), ('close', '<f4'), ('volume', '<f4'), ('amount', '<f4')])

这不是你想要的吗?

于 2013-02-14T15:20:21.070 回答