For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
Pandas data frame Error
-
I'm going to do back testing using panda data frame. This is my code:
import backtrader as bt import pandas as pd df = pd.read_csv('EURUSD-h1.csv',skiprows=1, parse_dates=True, index_col=0) train_size = int(len(df) * 0.66) train, test = df[0:train_size], df[train_size:len(df)] print(train) data0 =bt.feeds.PandasData(dataname=train,timeframe='Minutes',openinterest=None) cerebro = bt.Cerebro(stdstats=False) cerebro.addstrategy(bt.Strategy) cerebro.adddata(data0) cerebro.broker.setcash(100000.0) print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) cerebro.run() print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
And this is my data :
But there is error at the output.
-
Show the error message.
-
this is the error:
Traceback (most recent call last): File "<ipython-input-1-f7930e186e47>", line 1, in <module> runfile('C:/python/spyder/untitled6.py', wdir='C:/python/spyder') File "c:\users\sorosh\appdata\local\programs\python\python37-32\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile execfile(filename, namespace) File "c:\users\sorosh\appdata\local\programs\python\python37-32\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "C:/python/spyder/untitled6.py", line 18, in <module> cerebro.run() File "c:\users\sorosh\appdata\local\programs\python\python37-32\lib\site-packages\backtrader\cerebro.py", line 1127, in run runstrat = self.runstrategies(iterstrat) File "c:\users\sorosh\appdata\local\programs\python\python37-32\lib\site-packages\backtrader\cerebro.py", line 1212, in runstrategies data.preload() File "c:\users\sorosh\appdata\local\programs\python\python37-32\lib\site-packages\backtrader\feed.py", line 435, in preload while self.load(): File "c:\users\sorosh\appdata\local\programs\python\python37-32\lib\site-packages\backtrader\feed.py", line 476, in load _loadret = self._load() File "c:\users\sorosh\appdata\local\programs\python\python37-32\lib\site-packages\backtrader\feeds\pandafeed.py", line 268, in _load dt = tstamp.to_pydatetime() AttributeError: 'str' object has no attribute 'to_pydatetime'
-
@siavash said in Pandas data frame Error:
df = pd.read_csv('EURUSD-h1.csv',skiprows=1, parse_dates=True, index_col=0)
Content of column
0
:EURUSD
Error
@siavash said in Pandas data frame Error:
AttributeError: 'str' object has no attribute 'to_pydatetime'
It seems to be a straightforward thing. In any case you probably want to read how
PandasData
work: -
Thanks. It was helpful