AttributeError: 'numpy.int64' object has no attribute 'lower' when trying to run Cerebro after importing data from Pandas
-
Hello,
Kind of new to all this so apologies if I am missing something basic.
I have tried to search through all the information about using pd dataframes with backtrader and have had a look through other people's posts on here and one was kind of similar i think but i couldn't see a solution and for some reason couldn't reply to it.
When i add the data i don't get an error but when i run i get a long error ending in this "AttributeError: 'numpy.int64' object has no attribute 'lower'".
Here is my code in case that helps:
import matplotlib.pyplot as plt import numpy as np import pandas as pd import scipy.stats as st import datetime import requests import backtrader as bt import backtrader.feeds as btfeeds from strategies import * cerebro = bt.Cerebro() #Add data to Cerebro def get_bitfinex_asset(asset, ts_ms_start, ts_ms_end): url = 'https://api.bitfinex.com/v2/candles/trade:1D:t' + asset + '/hist' params = { 'start': ts_ms_start, 'end': ts_ms_end, 'sort': 1} r = requests.get(url, params = params) data_dl = r.json() return pd.DataFrame(data_dl) start_date = 1514768400000 # 1 January 2018, 00:00:00 end_date = 1527811199000 # 31 May 2018, 23:59:59 global_asset = 'ETHBTC' print('Downloading ' + global_asset) dataframe = get_bitfinex_asset(asset = global_asset, ts_ms_start = start_date, ts_ms_end = end_date) print(dataframe) data = bt.feeds.PandasData(dataname=dataframe) cerebro.adddata(data) cerebro.addstrategy(PrintClose) cerebro.run()
If i comment out the cerebro.run() line I dont get the error, and the rest of the console output looks like this:
Any ideas about how I might be able to get backtrader to properly process my data?
-
@_will_derness you may want to read docs one more time to learn how data feeds are organized, and how pandas data frame should be formatted to be used with
bt
.https://www.backtrader.com/docu/datafeed/
https://www.backtrader.com/docu/pandas-datafeed/pandas-datafeed/ -
It seems a very similar question was discussed already not so long ago. Take a look:
https://community.backtrader.com/topic/3411/data-feed-format
-
I could be off, but check your dataframe columns. "lower" sounds suspicious. Should be 'low' me thinks.
-
@run-out said in AttributeError: 'numpy.int64' object has no attribute 'lower' when trying to run Cerebro after importing data from Pandas:
I could be off, but check your dataframe columns. "lower" sounds suspicious. Should be 'low' me thinks.
I was off. This error is most likely due to you haveing no headers in your data set. Here's the error I get when I try to run a backtest with no headers.
File "/home/runout/a_bt_env/backtrader/backtrader/feed.py", line 203, in _start self.start() File "/home/runout/a_bt_env/backtrader/backtrader/feeds/pandafeed.py", line 212, in start colnames = [x.lower() for x in self.p.dataname.columns.values] File "/home/runout/a_bt_env/backtrader/backtrader/feeds/pandafeed.py", line 212, in <listcomp> colnames = [x.lower() for x in self.p.dataname.columns.values] AttributeError: 'numpy.int64' object has no attribute 'lower'
This is a known Issue #43 which I dropped the ball on a couple of months ago. I will pick it up now.
In the mean time, if you can add headers ['open', 'high', 'low', 'close', 'volume'] headers to your data you should be ok for now.