For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

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,
    train_size = int(len(df) * 0.66)
    train, test = df[0:train_size], df[train_size:len(df)]
    data0 =bt.feeds.PandasData(dataname=train,timeframe='Minutes',openinterest=None)
    cerebro = bt.Cerebro(stdstats=False)
    print('Starting Portfolio Value: %.2f' %
    print('Final Portfolio Value: %.2f' %

    And this is my data :0_1534025271866_Untitled.png

    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/', wdir='C:/python/spyder')
      File "c:\users\sorosh\appdata\local\programs\python\python37-32\lib\site-packages\spyder_kernels\customize\", line 668, in runfile
        execfile(filename, namespace)
      File "c:\users\sorosh\appdata\local\programs\python\python37-32\lib\site-packages\spyder_kernels\customize\", line 108, in execfile
        exec(compile(, filename, 'exec'), namespace)
      File "C:/python/spyder/", line 18, in <module>
      File "c:\users\sorosh\appdata\local\programs\python\python37-32\lib\site-packages\backtrader\", line 1127, in run
        runstrat = self.runstrategies(iterstrat)
      File "c:\users\sorosh\appdata\local\programs\python\python37-32\lib\site-packages\backtrader\", line 1212, in runstrategies
      File "c:\users\sorosh\appdata\local\programs\python\python37-32\lib\site-packages\backtrader\", line 435, in preload
        while self.load():
      File "c:\users\sorosh\appdata\local\programs\python\python37-32\lib\site-packages\backtrader\", line 476, in load
        _loadret = self._load()
      File "c:\users\sorosh\appdata\local\programs\python\python37-32\lib\site-packages\backtrader\feeds\", line 268, in _load
        dt = tstamp.to_pydatetime()
    AttributeError: 'str' object has no attribute 'to_pydatetime'

  • administrators

    @siavash said in Pandas data frame Error:

    df = pd.read_csv('EURUSD-h1.csv',skiprows=1,

    Content of column 0:



    @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