【问题标题】:TypeError: cannot concatenate object of type '<class 'yfinance.ticker.Options'>'; only Series and DataFrame objs are validTypeError:无法连接类型为“<class 'yfinance.ticker.Options'>”的对象;只有 Series 和 DataFrame obj 是有效的
【发布时间】:2021-10-04 17:08:33
【问题描述】:

下面是我的代码,用于从股票代码列表中提取股票期权数据,然后将所有股票期权的数据框连接成一个。但是,我收到以下错误消息:“TypeError: cannot concatenate object of type ''; 只有 Series 和 DataFrame objs 有效”

opt_appended = []

for symbol in tickers:
    try:
        ticker = yf.Ticker(symbol)
        opt = ticker.option_chain('2021-07-30')
        opt_appended.append(opt)
    except ValueError:
        continue
opt_appended = pd.concat(opt_appended)

【问题讨论】:

    标签: python pandas dataframe concatenation yfinance


    【解决方案1】:

    对 DataFrame 的顺序追加非常昂贵,因为它需要每次迭代都构建一个新的 DataFrame。因此,通常会避免使用它们。由于option_chain 返回一个可迭代的,而不是附加到列表中,我们应该extend 列表。然后在最后执行一个concat

    import pandas as pd
    import yfinance as yf
    
    tickers = ['AAPL', 'AA', 'AAL']
    
    opts_list = []
    
    for symbol in tickers:
        try:
            ticker = yf.Ticker(symbol)
            opt = ticker.option_chain('2021-07-30')
            opts_list.extend(opt)
        except ValueError:
            continue
    
    new_df = pd.concat(opts_list)
    

    new_df:

             contractSymbol       lastTradeDate  ...  contractSize  currency
    0   AAPL210730C00065000 2021-07-28 19:37:45  ...       REGULAR       USD
    1   AAPL210730C00070000 2021-07-22 18:17:27  ...       REGULAR       USD
    2   AAPL210730C00075000 2021-07-28 17:19:38  ...       REGULAR       USD
    3   AAPL210730C00080000 2021-07-22 14:59:05  ...       REGULAR       USD
    4   AAPL210730C00085000 2021-07-27 16:09:57  ...       REGULAR       USD
    ..                  ...                 ...  ...           ...       ...
    28   AAL210730P00029000 2021-07-26 13:31:18  ...       REGULAR       USD
    29   AAL210730P00029500 2021-07-26 13:32:22  ...       REGULAR       USD
    30   AAL210730P00030000 2021-07-22 16:52:08  ...       REGULAR       USD
    31   AAL210730P00031000 2021-07-22 15:53:55  ...       REGULAR       USD
    32   AAL210730P00032000 2021-07-26 13:30:11  ...       REGULAR       USD
    
    [253 rows x 14 columns]
    

    【讨论】:

      【解决方案2】:

      为了绑定一个列表,我们不能使用pd.concat(),所以如果我们把初始值做成一个数据框,问题就解决了。

      import yfinance as yf
      import pandas as pd
      
      tickers = ['AAPL','AA','AAL']
      
      opt_appended = pd.DataFrame()
      
      for symbol in tickers:
          try:
              ticker = yf.Ticker(symbol)
              opt = ticker.option_chain('2021-07-30')
              opt_appended = opt_appended.append(opt)
          except ValueError:
              continue
      
      contractSymbol lastTradeDate strike lastPrice bid ask change percentChange volume openInterest impliedVolatility inTheMoney contractSize currency
      0 AAPL210730C00065000 2021-07-28 19:37:45 65 80.32 78.75 81.3 -1.18 -1.44785 5 81 4.1875 True REGULAR USD
      1 AAPL210730C00070000 2021-07-22 18:17:27 70 74.95 74.3 75.8 -2.26 -2.92708 2 153 4.01563 True REGULAR USD
      2 AAPL210730C00075000 2021-07-28 17:19:38 75 70.05 69.25 70.85 -3.39999 -4.62899 20 197 3.67188 True REGULAR USD
      3 AAPL210730C00080000 2021-07-22 14:59:05 80 67.8 63.9 66.25 0 0 67 133 3.46094 True REGULAR USD
      4 AAPL210730C00085000 2021-07-27 16:09:57 85 60.95 59.6 61.15 0 0 12 186 3.89063 True REGULAR USD

      【讨论】:

      • 如果我的回答对您有帮助,请考虑接受它作为正确答案。
      • 这很奇怪 - 当我昨晚尝试时,你的答案完美无缺,但今天早上没有,我收到错误,所以我接受另一个答案作为正确答案,因为那个答案正在工作
      猜你喜欢
      • 2021-12-11
      • 1970-01-01
      • 2022-01-21
      • 2020-05-17
      • 2020-11-05
      • 2020-08-17
      • 2021-10-23
      • 2021-01-15
      • 1970-01-01
      相关资源
      最近更新 更多