My code is to collect stock data and save it into a database, but after it gets the data I am getting an error trying to save it to the database.
code snippet is: (failing at cursor.execute)
all_tickers = pd.read_sql("SELECT ticker, id FROM security", conn)
ticker_index = dict(all_tickers.to_dict('split')['data'])
tickers = list(ticker_index.keys())
start = dt.datetime(2018, 8, 8)
end = dt.datetime.now()
for ticker in tickers:
# Download data
df = pdr.get_data_yahoo(ticker, start, end)
# Write to daily_price
for row in df.itertuples():
values = [YAHOO_VENDOR_ID, ticker_index[ticker]] + list(row)
sql_two = "INSERT INTO daily_price (data_vendor_id,ticker_id, price_date, open_price, high_price, low_price, close_price, adj_close_price, volume) " + \
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"
cursor.execute(sql_two, tuple(values)) #<--errorhere
Error message:
AttributeError: 'Timestamp' object has no attribute 'translate'
I've tried adjusting the start/end time because the 'timestamp' object makes me think it is something to do with the time I am passing, but I can't figure out what this should be.
I think MySQL isn't liking the pandas Timestamp object, but I don't have MySQL to test it out on... and this is too verbose to leave in a comment.
Can you try replacing your values
line with this?:
values = [YAHOO_VENDOR_ID, ticker_index[ticker]] +
[row[0].to_pydatetime()] + list(row[1:])