2016-04-08 112 views
0

有問題索引一個numpy的陣列後,我從ystockquote導入股票報價:ystockquote數據導入numpy的陣列,IndexError:數組太多指數

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]:() 

看來,數組有沒有尺寸。任何人都可以解釋發生了什麼?

回答

0

你基本上是試圖把一個字典中的數組,這使得沒有真正意義上的,也許項目將接近你想要什麼:

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 

但我不確定這比使用字典本身更有用。

+0

謝謝Padraic。我對Python仍然很陌生。目標是將數據轉換爲一個numpy數組,每行表示一個日期的數據(即6個數據點)。 當我打電話給 print(q [0]) 時,我仍然得到索引錯誤,這可能是版本問題?我在Spyder 2.3.8中使用IPython(Python 3.5) – Thor

+0

@Thor,啊,好吧,我們需要爲python 3調用'list(d.items())' –

+0

現在工作 - thx! – Thor