Python Forum
ValueError: The truth value of a Series is ambiguous ? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: ValueError: The truth value of a Series is ambiguous ? (/thread-20206.html)



ValueError: The truth value of a Series is ambiguous ? - firebird - Jul-31-2019

Hello Everyone!
I have a UDF that outputs a list of stocks and their open and current prices in a dictionary format. That output is then sent to another UDF that calculates the rate of change of each one of those stocks. Subsequently, i try finding the maximum of the rate of change and try matching it back with the stock (key in my original dictionary) that has that produce the given maximum value.

These are the typical values that are produced with the rate of change function (ROC):
0.42731725348858923 (which is AAPL's)
-35.87468810645966 (which is MSFT's)
13.56390352093153 (which is TSLA's)
However, when i run the script, this is the error message i get:
Error:
Traceback (most recent call last): File "C:\Users\bgeor\Desktop\multiROCvalues.py", line 39, in <module> maxKey = [k for k, v in data.items() if v == maxVal] File "C:\Users\bgeor\Desktop\multiROCvalues.py", line 39, in <listcomp> maxKey = [k for k, v in data.items() if v == maxVal] File "C:\Users\bgeor\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\generic.py", line 1479, 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().
This is the script:
#import all modules
from yahoofinancials import YahooFinancials
import pandas as pd

#Stock stickers to get data - declared globally
stocks = ['AAPL','MSFT','TSLA']

def getStockData():
    yahoo_financials = YahooFinancials(stocks)
    price = yahoo_financials.get_current_price()
    Open = yahoo_financials.get_open_price()
    return(price, Open)
openVal = lambda: getStockData()[1]
curVal = lambda: getStockData()[0]
openDict = {}
openDict.update(openVal())
curDict = {}
curDict.update(curVal())
    
def ROC(op_price, cur_price):
    rate_of_Change = float((cur_price - op_price)/op_price)*100
    return rate_of_Change
if __name__ == "__main__":
    dataSet1 = openVal()
    dict_price1 = {}
    dict_price1.update(dataSet1)
    for curPrice in dict_price1.values():
        dataSet2 = curVal()
        dict_price2 = {}
        dict_price2.update(dataSet2)
        for opPrice in dict_price2.values():
            rateChangeVal = ROC(curPrice, opPrice)
            ##print(rateChangeVal)

            dF = pd.DataFrame([rateChangeVal])
            data = {}
            data.update(dF)
            maxVal = max(data.values())#maximum value
            maxKey = [k for k, v in data.items() if v == maxVal]
            print(maxKey, maxVal)



RE: ValueError: The truth value of a Series is ambiguous ? - scidam - Aug-01-2019

You got this error because v (or maxVal) in line #39 is a vector or a matrix.
It is unclear why do you introduce data dict (line # 36). You can do everything using pandas.
If you are tyring to select rows where the maximum value is located, you can do it as follows:
df[(df == df.values.max()).any(axis=1)]