llSourcell / predicting_stock_prices

This is the coding challenge for "Predicting Stock Prices" by @Sirajology on Youtube
297 stars 196 forks source link

List out of index #11

Open Cipher4all opened 4 years ago

Cipher4all commented 4 years ago

I copied the file and run it in the kernel. It is showing error at line 14 that list out of index range. Why that's so?

Mohswalm545 commented 1 year ago

the code assumes that each row in the CSV file contains at least two elements (date and price). However, if the row is empty or does not have enough elements, this error will occur.

To resolve this issue, you can add some error handling to check if a row has the necessary elements before trying to access them. Here's an updated version of the get_data function that includes error handling:

python

def get_data(filename): with open(filename, 'r') as csvfile: csvFileReader = csv.reader(csvfile) next(csvFileReader) for row in csvFileReader: if len(row) >= 2: dates.append(int(row[0].split('-')[0])) prices.append(float(row[1])) return