[英]Concatenating data frames
我从雅虎财经中抓取了一些表格,并尝试将它们连接到一个数据框中,但我收到一条错误消息:
“ValueError:没有要连接的对象”
这是脚本:
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
首先,在我看来,有 2 个循环重复相同的任务。 for i in trange(CalendarDays...):
和for i in range(CalendarDays):
代码中的所有 rest 工作正常。 尝试花更长的时间进行测试。 并非每天都会发布收入。 我已经测试了 6 天,其中 4 天有数据。 对于没有收益发布的日子,不会返回任何表格,并且您可以使用try... except:...
block 有效地消除此错误。
但是,如果连续 3 天都没有发布,您的列表将包含 null 表并且脚本会突然结束。
您可以使用if... else...
块测试这种情况,因为这可能经常发生并优雅地退出脚本
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.