June Smith
June Smith

Reputation: 165

Concatenating data frames

I have scraped some tables from Yahoo finance and I've tried concatenating them into one data frame but I'm getting an error message:

"ValueError: No objects to concatenate"

Here's the script:

CalendarDays = 3 #<-- specify the number of calendar days you want to grab earnings release info for
tables = [] #<-- initialize an empty list to store your tables

for i in trange(CalendarDays, file = sys.stdout, desc = 'Grabbing companies with earnings releases in the next ' + str(CalendarDays) + ' days'):

    for i in range(CalendarDays): #<-- Grabs earnings release info for the next x days on the calendar
        try: 
            date = (datetime.date.today() + datetime.timedelta(days = i )).isoformat() #get tomorrow in iso format as needed'''
            pd.set_option('display.max_column',None)
            url = pd.read_html("https://finance.yahoo.com/calendar/earnings?day="+date, header=0)
            table = url[0]
            table['Earnings Release Date'] = date
            tables.append(table) #<-- append each table into your list of tables
        except ValueError:
            continue

df = pd.concat(tables, ignore_index = True) #<-- take your list of tables into 1 final dataframe

Upvotes: 0

Views: 48

Answers (1)

Marek Złakowski
Marek Złakowski

Reputation: 331

First of all there are 2 loops that seems to me to repeat the same task. for i in trange(CalendarDays...): and for i in range(CalendarDays):

all the rest in the code works OK. Try to take longer period for testing. Not each day earnings are released. I have tested for 6 days and 4 were with data. For day without earnings release no table is returned and you effectively mute this error with try... except:... block.

However in case all 3 days consecutive days have no release, your list contains null tables and script ends abruptly.

You can test for this condition with if ... else... block as this can happen frequently and exit script gracefully

Upvotes: 1

Related Questions