ystockquote数据导入numpy数组,IndexError:数组索引太多

时间:2016-04-08 21:38:51

标签: python arrays numpy

在我从ystockquote导入股票报价后,有问题索引numpy数组:

import numpy as np
import ystockquote as ysq
startdate = "2016-04-06"
enddate = "2016-04-07"

q = np.array(ysq.get_historical_prices("AAPL", startdate, enddate)) 

print(q)

{'2016-04-07': {'Close': '108.540001', 'Volume': '30881000', 'Adj Close': '108.540001', 'High': '110.419998', 'Low': '108.120003', 'Open': '109.949997'}, '2016-04-06': {'Close': '110.959999', 'Volume': '26047800', 'Adj Close': '110.959999', 'High': '110.980003', 'Low': '109.199997', 'Open': '110.230003'}}



  q[0,0]

Traceback (most recent call last):

  File "<ipython-input-105-ad764cdee54e>", line 1, in <module>
    q[0,0]

IndexError: too many indices for array

    q[0,:]
Traceback (most recent call last):

  File "<ipython-input-106-069fcfa0a0f6>", line 1, in <module>
    q[0,:]

IndexError: too many indices for array


    q[:,0]

Traceback (most recent call last):

  File "<ipython-input-107-782637b90296>", line 1, in <module>
    q[:,0]

IndexError: too many indices for array

    q.shape

Out[108]: ()

似乎数组没有维度。谁能解释一下发生了什么?

1 个答案:

答案 0 :(得分:0)

你基本上是想把一个dict放在一个没有真正意义的数组中,也许项目会更接近你想要的东西:

d = {'2016-04-07': {'Close': '108.540001', 'Volume': '30881000', 'Adj Close': '108.540001', 'High': '110.419998', 'Low': '108.120003', 'Open': '109.949997'}, '2016-04-06': {'Close': '110.959999', 'Volume': '26047800', 'Adj Close': '110.959999', 'High': '110.980003', 'Low': '109.199997', 'Open': '110.230003'}}


q = np.array(list(d.items()))
print(q)
print q[0]
print(q.size)

这给了你:

[['2016-04-07'
  {'High': '110.419998', 'Adj Close': '108.540001', 'Volume': '30881000', 'Low': '108.120003', 'Close': '108.540001', 'Open': '109.949997'}]
 ['2016-04-06'
  {'High': '110.980003', 'Adj Close': '110.959999', 'Volume': '26047800', 'Low': '109.199997', 'Close': '110.959999', 'Open': '110.230003'}]]
['2016-04-07'
 {'High': '110.419998', 'Adj Close': '108.540001', 'Volume': '30881000', 'Low': '108.120003', 'Close': '108.540001', 'Open': '109.949997'}]
4

但我不确定这比仅使用dict本身更有用。