開始使用免費開始

擷取單一公司股票資料

DataReader 提供資料來源 'iex'。若要在 DataCamp 環境以外實驗資料,你需要一個 IEX Cloud 帳號。

IEX 對股價時間序列的存取僅限最近 5 年。

在匯入 DataReader 套件後,搭配以 date(YYYY, MM, DD) 形式提供的 start 與(或)end 引數,就能輕鬆從 IEX 取得股價資料:

stock_data = DataReader(ticker, data_source, start, end)

你在第 1 章已經學過,股票代號(stock ticker) 是用來查詢特定公司股票資訊的唯一符號。

在這個練習中,你會練習匯入 Apple 於 2016 年的資料,其股票代號為 'AAPL'

本練習屬於課程

在 Python 中匯入與管理財務資料

檢視課程

練習說明

  • pandas_datareader.data 匯入 DataReader,並從 datetime 函式庫匯入 date
  • 使用 date(),將 start 設為 2016 年 1 月 1 日,end 設為 2016 年 12 月 31 日。
  • ticker 設為 Apple 的股票代號 'AAPL'data_source 設為 'iex'
  • 建立 DataReader() 物件以匯入股價,並指定給變數 stock_prices
  • 使用 .head().info() 顯示並檢視結果。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Import DataReader
____

# Import date
____

# Set start and end dates
start = date(____)
end = date(____)

# Set the ticker
ticker = ____

# Set the data source
data_source = ____

# Import the stock prices
stock_prices = DataReader(____, ____, ____, ____)

# Display and inspect the result
print(stock_prices.head())
stock_prices.info()
編輯並執行程式碼