多索引熊猫数据框上的值错误

Posted

技术标签:

【中文标题】多索引熊猫数据框上的值错误【英文标题】:Value error on multiindex pandas dataframe 【发布时间】:2020-09-30 11:15:53 【问题描述】:

我正在使用来自here 的包。我要做的是计算每个股票代码的随机值。 我有以下代码:

import lxml
from urllib.request import urlopen
from bs4 import BeautifulSoup
import pandas as pd
from pandas import datetime
from pandas import DataFrame as df
# import matplotlib
from pandas_datareader import data as web
import matplotlib.pyplot as plt
import datetime
import numpy as np
import ta


html = urlopen('https://en.wikipedia.org/wiki/NIFTY_50')
soup = BeautifulSoup(html,'lxml')
niftylist_raw = soup.find('table', 'class': 'wikitable sortable')

nifty_symbol =[]

for row in niftylist_raw.findAll('tr')[1:]:
    nifty_symbols = row.findAll('td')[1].text
    nifty_symbol.append(nifty_symbols)
tickerss = nifty_symbol

df = web.DataReader(tickerss, 'yahoo')


stoch1 = ta.momentum.StochasticOscillator(high= df['High'], low = df['Low'], close = df['Close'], n=14, fillna=False)

上面的代码有效,但是当我尝试时:

stoch1.stoch()

它会产生以下错误:

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我可以做些什么来修复这个错误?

【问题讨论】:

【参考方案1】:

似乎StochasticOscillator 方法需要时间序列,单列。但是,当您从 web.DataReader 构建数据框时,它会为您提供与股票行情一样多的列。

所以你只需要遍历代码,并将输出时间序列附加到一个新的数据帧:

import lxml
from urllib.request import urlopen
from bs4 import BeautifulSoup

import pandas as pd
from pandas_datareader import data as web
import ta

html = urlopen('https://en.wikipedia.org/wiki/NIFTY_50')
soup = BeautifulSoup(html,'lxml')
niftylist_raw = soup.find('table', 'class': 'wikitable sortable')

tickers = []

for row in niftylist_raw.findAll('tr')[1:]:
    nifty_symbols = row.findAll('td')[1].text
    tickers.append(nifty_symbols)

print(tickers)

df = web.DataReader(tickers, 'yahoo')

stoch_list = pd.DataFrame(columns = tickers, index = df.index)

for t in tickers:
    stoch1 = ta.momentum.StochasticOscillator(
        high = df['High'][t], 
        low = df['Low'][t], 
        close = df['Close'][t], 
        n = 14, 
        fillna = True
    )
    stoch_list[t] = stoch1.stoch()

print(stoch_list)

输出:

            ADANIPORTS.NS  ASIANPAINT.NS  AXISBANK.NS  BAJAJ-AUTO.NS  BAJFINANCE.NS  BAJAJFINSV.NS  BHARTIARTL.NS  ...   TECHM.NS   TITAN.NS  ULTRACEMCO.NS     UPL.NS    VEDL.NS   WIPRO.NS    ZEEL.NS
Date                                                                                                               ...                                                                                 
2015-06-15      21.909922       9.347667    48.704666      91.231693      77.791892      44.191114      30.140809  ...  53.666585  13.368975      14.346124  11.691838  14.367821  56.043549  91.428571
2015-06-16      72.363725      45.265156    74.247676      91.487233      66.564368      40.100098      55.669462  ...  36.333415  64.983097      88.990210  40.529881  21.072792  52.328376  87.301587
2015-06-17      56.363636      66.856091    53.025895      75.153149      91.088110      50.700195      85.667732  ...  21.000163  91.388957      80.205145  86.905950   3.869011  66.513717  92.325610
2015-06-18      56.363636      86.940831    57.636766      89.124719      94.661550      60.670907      71.193579  ...   5.263327  92.608643      78.613103  60.171097   7.142838  84.943018  70.000000
2015-06-19      11.428615      90.558724    66.282281      89.443078      86.336954      60.956522      88.276724  ...  19.378201  87.341779      81.569343  47.826973  22.928220  64.704798  39.333360
...                   ...            ...          ...            ...            ...            ...            ...  ...        ...        ...            ...        ...        ...        ...        ...
2020-06-08      91.351874      57.958253    87.042001      82.309507      94.710350      94.247354      60.630541  ...  90.171597  89.217165      71.472360  88.654333  75.702800  98.275850  78.462774
2020-06-09      88.169607      59.439045    77.926708      79.988254      86.386837      85.820652      35.190610  ...  79.905597  77.139145      68.528865  83.201404  73.694768  80.155220  70.400641
2020-06-10      89.285678      55.534959    84.539778      62.649461      85.347188      80.135976      38.385475  ...  84.417652  65.700137      64.559014  73.540428  79.718864  76.537583  64.615390
2020-06-11      71.651770      29.268271    72.028606      62.966014      72.695166      75.927493      15.018624  ...  64.094315  53.441767      41.152918  62.931034  59.437760  65.384598  40.940166
2020-06-12      85.508938      35.945931    66.097784      77.878054      87.243356      84.211742      37.430114  ...  45.712787  62.894536      53.112690  54.729316  68.473904  53.365369  17.986311

[1228 rows x 50 columns]

【讨论】:

我能问你为什么fillna = True吗?谢谢 @newcoder 您可以将其设置为 False,根据school.stockcharts.com/…,您将获得每个系列的前 13 个值的 NaN 值。 @newcoder 如果您在将stoch_list["HDFC.NS"] 设置为False 时查看它,您会注意到它在2015-12-24 之前具有NaN 值。查看"HDFC.NS" 的初始值后,从2015-06-152015-12-24 的最高价、最低价和收盘价似乎相同

以上是关于多索引熊猫数据框上的值错误的主要内容,如果未能解决你的问题,请参考以下文章

多索引上的熊猫数据框 groupby

关键错误多索引数据框熊猫

3维numpy数组到多索引熊猫数据框

对唯一顺序索引的多索引熊猫数据框进行切片和赋值

访问熊猫数据框中内部多索引级别的最后一个元素

熊猫数据框多索引合并