熊猫数据框产量错误

时间:2018-06-27 13:48:06

标签: python-3.x pandas dataframe yield-keyword

我正在尝试为熊猫数据帧逐行生成1行,但出现错误。该数据框是一个股票价格数据,包括每日开盘价,收盘价,最高价,最低价和数量信息。

以下是我的代码。此类将从MySQL数据库获取数据

class HistoricMySQLDataHandler(DataHandler):

def __init__(self, events, symbol_list):
    """
    Initialises the historic data handler by requesting
    a list of symbols.

    Parameters:
    events - The Event Queue.
    symbol_list - A list of symbol strings.
    """
    self.events = events
    self.symbol_list = symbol_list

    self.symbol_data = {}
    self.latest_symbol_data = {}
    self.continue_backtest = True       

    self._connect_MySQL()
def _connect_MySQL(self): #get stock price for symbol s 
    db_host = 'localhost'
    db_user = 'sec_user'
    db_pass = 'XXX'
    db_name = 'securities_master'
    con = mdb.connect(db_host, db_user, db_pass, db_name)
    for s in self.symbol_list:
       sql="SELECT * FROM daily_price where symbol= s
       self.symbol_data[s] = pd.read_sql(sql, con=con, index_col='price_date')"

def _get_new_bar(self, symbol):
    """
    Returns the latest bar from the data feed as a tuple of 
    (sybmbol, datetime, open, low, high, close, volume).
    """

    for row in self.symbol_data[symbol].itertuples():
        yield tuple(symbol, datetime.datetime.strptime(row[0],'%Y-%m-%d %H:%M:%S'), 
                    row[15], row[17], row[16], row[18],row[20])

def update_bars(self):
    """
    Pushes the latest bar to the latest_symbol_data structure
    for all symbols in the symbol list.
    """
    for s in self.symbol_list:
        try:
            bar = self._get_new_bar(s).__next__()
        except StopIteration:
            self.continue_backtest = False

在主要功能中:

# Declare the components with respective parameters
symbol_list=["GOOG"]
events=queue.Queue()
bars = HistoricMySQLDataHandler(events,symbol_list)


while True:
# Update the bars (specific backtest code, as opposed to live trading)
   if bars.continue_backtest == True:
      bars.update_bars()
   else:
      break
   time.sleep(1)

数据示例:

symbol_data["GOOG"] =             
  price_date id exchange_id ticker instrument name   ...    high_price  low_price close_price adj_close_price  volume                              
  2014-03-27  29        None   GOOG      stock  Alphabet Inc Class C   ...      568.0000   552.9200      558.46          558.46    13100

update_bars函数将调用_get_new_bar移至下一行(隔天价格)

我的目标是每天获取股价(迭代数据框的行),但是self.symbol_data[s]中的_connect_MySQL是数据框,而_get_new_bar中的'generator'是生成器,因此我得到了这个错误

  

AttributeError:'itertuples'对象没有属性self.symbol_data

有人有什么想法吗?

我正在使用python 3.6。谢谢

dictsymbolself.symbol_data["GOOG"]是用于获取数据帧的字符串键。该数据是股票价格数据。例如,yield返回一个数据框,其中包含按日期显示的Google每日股票价格信息索引,每行包括开盘价,低价,高价,收盘价和交易量。我的目标是使用_connect_MySQL逐日迭代此价格数据。

const sizes = [{ id: [{ value: '2496', label: 'XS' }, { value: '2499', label: 'S' }], type: 'First Size' }, { id: [{ value: '2863', label: 34 }, { value: '2866', label: 36 }], type: 'Shoe Sizes' }, { id: [{ value: '3561', label: 'XS' }, { value: '3563', label: 'S' }, { value: '3565', label: 'L' }, , { value: '3567', label: 'XL' }] }, { id: [{ value: '3523', label: 34 }, { value: '2866', label: 36 }], type: 'Shoe Sizes' }] 将从数据库中获取数据 在此示例中,函数中的s =“ GOOG”

1 个答案:

答案 0 :(得分:0)

我发现了错误。 我在其他地方的代码将数据框更改为生成器。 一个愚蠢的错误哈哈

我没有在问题中添加此行,但此行更改了数据类型

# Reindex the dataframes
    for s in self.symbol_list:
        self.symbol_data[s] = self.symbol_data[s].reindex(index=comb_index, method='pad').iterrows()