【问题标题】:How to properly download stocks data in python如何在python中正确下载股票数据
【发布时间】:2019-10-26 11:28:28
【问题描述】:

我一直在尝试使用 pandas_datareader 提取股票价格。数据,但是 我一直收到错误消息。

我检查了与此问题相关的其他线程,我尝试使用 conda install DataReader 下载数据阅读器,还尝试了 pip install DataReader。

import pandas as pd

 import datetime

from pandas import Series,DataFrame

import pandas_datareader.data as web

pandas_datareader.__version__

'0.6.0'

start=datetime.datetime(2009,1,1)

end=datetime.datetime(2019,1,1)

df=web.DataReader( 'AT&T Inc T',start,end)

df.head()

我的预期结果应该是包含股票所有特征和行的数据框。

以下是我收到的错误消息: 请问,这个问题怎么解决?

谢谢。

<ipython-input-45-d75bedd6b2dd> in <module>


      1 start=datetime.datetime(2009,1,1)

      2 end=datetime.datetime(2019,1,1)

----> 3 df=web.DataReader( 'AT&T Inc T',start,end)

      4 df.head()

~\Anaconda3\lib\site-packages\pandas_datareader\data.py in DataReader(name,

 data_source, start, end, retry_count, pause, session, access_key)

    456     else:

    457         msg = "data_source=%r is not implemented" % data_source

--> 458         raise NotImplementedError(msg)
    459 
    460 

NotImplementedError: data_source=datetime.datetime(2009, 1, 1, 0, 0) is not implemented

【问题讨论】:

    标签: python pandas web pandas-datareader


    【解决方案1】:

    这就是我的做法。

    import pandas as pd  
    import numpy as np
    from pandas_datareader import data as wb
    import datetime as dt
    
    start = '2019-6-20'
    end = '2019-7-20'
    
    tickers = ['CSCO',
    'AXP',
    'HD',
    'PG']
    
    thelen = len(tickers)
    
    price_data = []
    for ticker in tickers:
        prices = wb.DataReader(ticker, start = start, end = end, data_source='yahoo')[['Adj Close']]
        price_data.append(prices.assign(ticker=ticker)[['ticker', 'Adj Close']])
    
    df = pd.concat(price_data)
    df.dtypes
    df.head()
    df.shape
    
    pd.set_option('display.max_columns', 500)
    
    df = df.reset_index()
    df = df.set_index('Date')
    table = df.pivot(columns='ticker')
    # By specifying col[1] in below list comprehension
    # You can select the stock names under multi-level column
    table.columns = [col[1] for col in table.columns]
    table.head()
    

    结果:

                       AXP       CSCO          HD          PG
    Date                                                     
    2019-06-20  124.530563  57.049965  211.250000  111.021019
    2019-06-21  124.341156  56.672348  209.389999  110.484497
    2019-06-24  123.752991  56.821407  205.500000  111.607231
    2019-06-25  122.776054  55.728306  204.740005  111.001152
    2019-06-26  123.204704  56.245041  206.419998  109.023956
    

    【讨论】:

      【解决方案2】:

      以下工作:

      import pandas as pd
      
      import datetime
      
      from pandas import Series,DataFrame
      
      import pandas_datareader
      import pandas_datareader.data as web
      
      pandas_datareader.__version__
      
      start=datetime.datetime(2009,1,1)
      
      end=datetime.datetime(2019,1,1)
      
      df=web.DataReader( 'T', "yahoo", start,end)
      
      print(df.head())
      

      数据日志如下:

                       High        Low    ...          Volume  Adj Close
      Date                                ...                           
      2009-01-02  29.459999  28.430000    ...      21879800.0  16.438549
      2009-01-05  28.889999  28.059999    ...      32414700.0  15.885386
      2009-01-06  28.700001  28.000000    ...      28746100.0  15.812749
      2009-01-07  27.650000  27.000000    ...      30532700.0  15.427205
      2009-01-08  27.350000  26.820000    ...      21431200.0  15.410195
      
      [5 rows x 6 columns]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-20
        • 1970-01-01
        • 1970-01-01
        • 2014-08-14
        • 1970-01-01
        • 2020-09-15
        • 2013-05-29
        相关资源
        最近更新 更多