tushare接口get_realtime_quotes报错:AssertionError: 33 columns passed, passed data had 34 columns

tushare接口get_realtime_quotes,今天报错了:

AssertionError: 33 columns passed, passed data had 34 columns

看了一下源码,在获取股票代码为600063时,报了错。

dataframe5 = ts.get_realtime_quotes('600063')

与正常的代码比较

dataframe5 = ts.get_realtime_quotes('000001')

定位是在list转换成dataframe是出错了。

df = pd.DataFrame(data_list, columns=ct.LIVE_DATA_COLS)

其中,LIVE_DATA_COLS的定义是有33个元素:

LIVE_DATA_COLS = ['name', 'open', 'pre_close', 'price', 'high', 'low', 'bid', 'ask', 'volume', 'amount',
                  'b1_v', 'b1_p', 'b2_v', 'b2_p', 'b3_v', 'b3_p', 'b4_v', 'b4_p', 'b5_v', 'b5_p',
                  'a1_v', 'a1_p', 'a2_v', 'a2_p', 'a3_v', 'a3_p', 'a4_v', 'a4_p', 'a5_v', 'a5_p', 'date', 'time', 's']

报错是,data_list的值:

"皖维高新,3.060,3.090,3.050,3.090,3.030,3.050,3.060,15402659,47026593.000,86945,3.050,229700,3.040,345100,3.030,430300,3.020,412000,3.010,274500,3.060,538593,3.070,224300,3.080,190700,3.090,295800,3.100,2019-08-01,15:00:00,00,";

正常的data_list的值:

"平安银行,14.060,14.130,14.100,14.190,13.940,14.100,14.110,52798128,742308285.200,71100,14.100,64800,14.090,61000,14.080,181435,14.070,676618,14.060,224200,14.110,99100,14.120,103000,14.130,102700,14.140,148346,14.150,2019-08-01,15:00:03,00";

通过对比,很明显,报错时,data_list的值,最后多了一个分号,dataframe认为有34个元素,比LIVE_DATA_COLS的定义是有33个元素多一个,不匹配,所以报错了。

问题找到了,解决办法有无数个,我选择简单的那个,将data_list只取33个元素:

    for index, row in enumerate(data):
        if len(row)>1:
            data_list.append([astr for astr in row.split(',')])
            syms_list.append(syms[index])
    if len(syms_list) == 0:
        return None
    #AssertionError: 33 columns passed, passed data had 34 columns
    for i in range(0,len(data_list)):
        data_list[i] = data_list[i][0:len(ct.LIVE_DATA_COLS)]

    df = pd.DataFrame(data_list, columns=ct.LIVE_DATA_COLS)

 

你可能感兴趣的:(python,量化交易系统)