Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with a ValueError
#1
I am currently trying to work through a YouTube tutorial and am stuck on the following error:

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

The error is being caused by this line in the following code:

if currentMax == max(Range, default = 0):

Any help would be greatly appreciated – thanks!

import yfinance as yf
import datetime as dt
import pandas as pd
from pandas_datareader import data as pdr
import matplotlib.pyplot as plt

yf.pdr_override()
start = dt.datetime(2019, 6, 1)
now = dt.datetime.now()

while stock != "quit":
    
    df = pdr.get_data_yahoo(stock, start, now)
    df['High'].plot(label = 'high')
    
    pivots = [] 
    dates = [] 
    counter = 0  
    lastPivot = 0 
    
    Range = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    
    dateRange = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    
    for i in df.index:
        currentMax = max(Range, default = 0)
        value = round(df["High"], 2)
        
        Range = Range[1:9] 
        Range.append(value) 
        dateRange = dateRange[1:9] 
        dateRange.append(i)
        
        if currentMax == max(Range, default = 0):
            counter += 1
            
        else:
            counter = 0
        
        if counter == 5:
            lastPivot = currentMax
            dateloc = Range.index(lastPivot)
            lastDate = dateRange[dateloc]
            
            pivots.append(currentMax)
            dates.append(lastDate)
    print()
    
    timeD = dt.timedelta(days = 30)
    
    for index in range(len(pivots)):
        print(str(pivots[index]) + ": "+ str(dates[index]))
        
        plt.plot_date([dates[index] - (timeD * .075), dates[index] + timeD],
                     [pivots[index], pivots[index]], linestyle = "-", linewidth = 2, marker = ",")
    
    plt.show()
    
    print()

    stock = input("Enter the stock symbol: ")
Reply
#2
You need to post the error message and traceback.
Reply
#3
Whoops, I totally forgot to copy that:


ValueError                                Traceback (most recent call last)
<ipython-input-7-3522fdff8944> in <module>
     28         dateRange.append(i) # adds newest date to the array
     29 
---> 30         if currentMax == max(Range, default = 0):
     31             counter += 1
     32 

/opt/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in __nonzero__(self)
   1328     def __nonzero__(self):
   1329         raise ValueError(
-> 1330             f"The truth value of a {type(self).__name__} is ambiguous. "
   1331             "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
   1332         )

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Reply
#4
See here: https://pandas.pydata.org/pandas-docs/ve...tchas.html

and here: https://stackoverflow.com/questions/3692...em-a-any-o

A little hard to give specific advice without knowing what you are trying to achieve.
Reply
#5
(Dec-12-2020, 07:24 AM)palladium Wrote: See here: https://pandas.pydata.org/pandas-docs/ve...tchas.html

and here: https://stackoverflow.com/questions/3692...em-a-any-o

A little hard to give specific advice without knowing what you are trying to achieve.

Thanks for your help. The program is supposed to look for resistance/pivot points on a given stock chart. The code that is triggering the error is supposed to determine the maximum value within the array; this max value is the potential point of resistance/pivot point.

The error was being caused because I did not include [i] when defining the value variable:

    for i in df.index:
        currentMax = max(Range, default = 0)
        value = round(df["High"][i],2)
Reply


Forum Jump:

User Panel Messages

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