我有一个数据框,想把它转换成一个 numpy 数组来绘制它的值。数据框如下所示:
>>> df_ohlc
open high low close
Date
2018-03-07 03:35:00 62.189999 62.189999 62.169998 62.180000
2018-03-07 03:36:00 62.180000 62.180000 62.160000 62.180000
2018-03-07 03:37:00 62.169998 62.220001 62.169998 62.209999
2018-03-07 03:38:00 62.220001 62.220001 62.189999 62.200001
...
[480 rows x 4 columns]
>>> df_ohlc.index
DatetimeIndex(['2018-03-07 03:35:00', '2018-03-07 03:36:00',
'2018-03-07 03:37:00', '2018-03-07 03:38:00',
'2018-03-07 03:39:00', '2018-03-07 03:40:00',
'2018-03-07 03:41:00', '2018-03-07 03:42:00',
'2018-03-07 03:43:00', '2018-03-07 03:44:00',
...
'2018-03-07 11:25:00', '2018-03-07 11:26:00',
'2018-03-07 11:27:00', '2018-03-07 11:28:00',
'2018-03-07 11:29:00', '2018-03-07 11:30:00',
'2018-03-07 11:31:00', '2018-03-07 11:32:00',
'2018-03-07 11:33:00', '2018-03-07 11:34:00'],
dtype='datetime64[ns]', name='Date', length=480, freq='T')
>>> df_ohlc.index[0]
Timestamp('2018-03-07 03:35:00', freq='T') # and why is it Timestamp when it said ```dtype=datetime64[ns]```` right before?
但是当我尝试转换它时,索引类型(日期列)从datetime64[ns]
变为Timestamp
.
>>> df_ohlc.reset_index().values
array([[Timestamp('2018-03-07 03:35:00'), 62.189998626708984,
62.189998626708984, 62.16999816894531, 62.18000030517578],
[Timestamp('2018-03-07 03:36:00'), 62.18000030517578,
62.18000030517578, 62.15999984741211, 62.18000030517578],
[Timestamp('2018-03-07 03:37:00'), 62.16999816894531,
62.220001220703125, 62.16999816894531, 62.209999084472656],
...,
[Timestamp('2018-03-07 11:32:00'), 61.939998626708984,
61.95000076293945, 61.93000030517578, 61.93000030517578],
[Timestamp('2018-03-07 11:33:00'), 61.93000030517578,
61.939998626708984, 61.900001525878906, 61.90999984741211],
[Timestamp('2018-03-07 11:34:00'), 61.90999984741211,
61.91999816894531, 61.900001525878906, 61.91999816894531]], dtype=object)
为什么会发生这种情况,如何将类型保持为 datetime64?
我尝试分离数据框的索引并将其与之后的值连接起来,但它显示错误。我想知道我做错了什么。
>>> index_ohlc = np.array([ df_ohlc.index.values.astype('datetime64[s]'), ]).T
>>> index_ohlc.shape
(480, 1)
>>> value_ohlc = df_ohlc.values
>>> value_ohlc.shape
(480, 4)
>>> type(index_ohlc)
<class 'numpy.ndarray'>
>>> type(value_ohlc)
<class 'numpy.ndarray'>
>>> new_array = np.concatenate( (index_ohlc, value_ohlc), axis = 1 )
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: invalid type promotion