Python Forum
ValueError: The truth value of a Series is ambiguous ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ValueError: The truth value of a Series is ambiguous ?
#1
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)
Reply
#2
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)]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Data cardinality is ambiguous: x sizes: 51 y sizes: 26 sidra 0 2,294 Oct-03-2020, 11:43 AM
Last Post: sidra
  error : value of a DataFrame is ambiguous Sonata 1 2,199 Apr-24-2020, 05:40 PM
Last Post: anbu23
  TensorFlow get error - array with more than one element is ambiguous vokoyo 3 5,495 Nov-07-2019, 01:12 PM
Last Post: ThomasL
  ValueError: The truth value of an array with more than one element is ambiguous. Eliza5 1 14,279 Apr-02-2018, 12:03 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020