简体   繁体   English

尝试修复 Pandas / Python 上的“系列的真值不明确”错误

[英]Trying to fix the "Truth value of a Series is Ambiguous" Error on Pandas / Python

I am getting the following error:我收到以下错误:

Traceback (most recent call last):
  File "tests.py", line 101, in <module>
    test()
  File "tests.py", line 95, in test
    print(connorsRSI(df))
  File "/Users/WorkFLOW/app.py", line 14, in connorsRSI
    if data.loc[index, 'Diff'] > 0:
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/generic.py", line 1478, in __nonzero__
    .format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

On this line if data.loc[index, 'Diff'] > 0:在这一行if data.loc[index, 'Diff'] > 0:

And don't know how to fix it.并且不知道如何修复它。

Anybody knows any other way to rewrite it?有人知道重写它的其他方法吗?

I know there are other questions like this one but none has helped me solve this error.我知道还有其他类似的问题,但没有一个能帮助我解决这个错误。

-- UPDATE: df.head() ==> -- 更新:df.head() ==>

                 High        Low       Open      Close   Volume  Adj Close
Date                                                                      
2019-11-04  98.529999  95.599998  96.349998  98.150002  3841000  98.150002
2019-11-05  98.989998  96.629997  98.129997  98.260002  2719900  98.260002
2019-11-06  99.180000  97.970001  98.680000  98.669998  2447900  98.669998
2019-11-07  99.290001  97.019997  99.290001  97.230003  2156400  97.230003
2019-11-08  98.500000  96.320000  96.910004  98.269997  2306600  98.269997

Full code here:完整代码在这里:

import pandas_datareader.data as web

def connorsRSI(df):
    data = df.copy()
    data = data[['Close']]  
    data['Close'] = round(data['Close'], 2) 
    data['Diff'] = data['Close'].diff()
    data['UpDown'] = 0

    for i, index in enumerate(data.index[1:]):
        if data.loc[index, 'Diff'] > 0:
            if data.loc[data.index[i], 'Diff'] > 0:
                data.loc[index, 'UpDown'] = data.loc[data.index[i], 'UpDown'] + 1
            else:
                data.loc[index, 'UpDown'] = 1
        else:
            if data.loc[data.index[i], 'Diff'] > 0:
                data.loc[index, 'UpDown'] = -1
            else:
                data.loc[index, 'UpDown'] = data.loc[data.index[i], 'UpDown'] - 1

    xpct = data['Diff'][-1]
    data2 = data.copy()
    data2=data2[-100:]
    counter = data2[data2['Diff'] < xpct].count()['Diff']
    percentrank = round((counter/(len(data2)))*100,2)
    rsi = round(talib.RSI(data['Close'], timeperiod=3)[-1],2)
    streak = round(talib.RSI(data['UpDown'], timeperiod=2)[-1],2)
    crsi = round((rsi+streak+percentrank)/3,2)

    return crsi

def test():
    df = web.DataReader('EA', 'yahoo', start, end)
    print(connorsRSI(df))
test()
data['UpDown'] = (data['Diff'] > 0)
data['UpDown'] = data.groupby((df1['UpDown'] != data['UpDown'].shift()).cumsum()).cumcount() +1
data.loc[data['Diff'] < 0, 'UpDown'] *= -1

These three lines replace your loop.这三行替换了你的循环。 The first makes a bool to check whether diff is positive or negative.第一个生成一个 bool 来检查 diff 是正数还是负数。 The second line counts consecutive increases or decreases, and finally the third makes consecutive decreases negative.第二行计算连续增加或减少,最后第三行使连续减少为负数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM