打印时出现数据但不显示在数据框中

问题描述

#! /usr/lib/python3

import yfinance as yf
import pandas as pd

pd.set_option('display.max_rows',None,'display.max_columns',None)

# Request stock data from yfinance
ticker = yf.Ticker('AAPL')

# Get all option expiration dates in the form of a list
xdates = ticker.options

# Go through the list of expiry dates one by one
for xdate in xdates:
    # Get option chain info for that xdate
    option = ticker.option_chain(xdate)
    # print out this value,get back 15 columns and 63 rows of @R_287_4045@ion
    print(option)
    # Put that same data in dataframe
    df = pd.DataFrame(data = option)
    # Show dataframe
    print(df)
    

预期:df显示包含与运行 DataFrame显示的信息相同的信息的 print(option),即 15 列和 63 行数据,或至少其中的一部分

实际:

  • df显示两列没有信息
  • df.shape 结果为 (2,1)
  • print(df.columns.tolist()) 结果为 [0]

由于打印时会出现所需的信息,我很困惑为什么它没有出现在数据框中。

解决方法

特定到期日期的 option_chain 数据可在对象的 calls 属性中作为数据帧使用。您不必创建新的数据框。

ticker = yf.Ticker('AAPL')
xdates = ticker.options
option = ticker.option_chain(xdates[0])
option.calls # DataFrame 

GitHub - yfinance