Help with a ValueError - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Help with a ValueError (/thread-31446.html) |
Help with a ValueError - devansing - Dec-11-2020 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: ") RE: Help with a ValueError - deanhystad - Dec-11-2020 You need to post the error message and traceback. RE: Help with a ValueError - devansing - Dec-11-2020 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(). RE: Help with a ValueError - palladium - Dec-12-2020 See here: https://pandas.pydata.org/pandas-docs/version/0.15/gotchas.html and here: https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o A little hard to give specific advice without knowing what you are trying to achieve. RE: Help with a ValueError - devansing - Dec-13-2020 (Dec-12-2020, 07:24 AM)palladium Wrote: See here: https://pandas.pydata.org/pandas-docs/version/0.15/gotchas.html 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) |