quantopian / pyfolio

Portfolio and risk analytics in Python

Home Page:https://quantopian.github.io/pyfolio

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AttributeError: 'numpy.int64' object has no attribute 'to_pydatetime'

Sergey80 opened this issue · comments

Problem Description

That is supposed to be fixed by now: #520
But I still get the same error (with latest deps):

AttributeError: 'numpy.int64' object has no attribute 'to_pydatetime'

Please provide a minimal, self-contained, and reproducible example:

from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import argparse

import backtrader as bt
import backtrader.feeds as btfeeds

import pandas


def runstrat():
    args = parse_args()

    # Create a cerebro entity
    cerebro = bt.Cerebro(stdstats=False)

    # Add a strategy
    cerebro.addstrategy(bt.Strategy)

    # Get a pandas dataframe
    datapath = ('../../datas/2006-day-001.txt')

    # Simulate the header row isn't there if noheaders requested
    skiprows = 1 if args.noheaders else 0
    header = None if args.noheaders else 0

    dataframe = pandas.read_csv(datapath,
                                skiprows=skiprows,
                                header=header,
                                parse_dates=True,
                                index_col=0)

    if not args.noprint:
        print('--------------------------------------------------')
        print(dataframe)
        print('--------------------------------------------------')

    # Pass it to the backtrader datafeed and add it to the cerebro
    data = bt.feeds.PandasData(dataname=dataframe)

    cerebro.adddata(data)

    # Run over everything
    cerebro.run()

    # Plot the result
    cerebro.plot(style='bar')


def parse_args():
    parser = argparse.ArgumentParser(
        description='Pandas test script')

    parser.add_argument('--noheaders', action='store_true', default=False,
                        required=False,
                        help='Do not use header rows')

    parser.add_argument('--noprint', action='store_true', default=False,
                        help='Print the dataframe')

    return parser.parse_args()


if __name__ == '__main__':
    runstrat()

to be copy paste from: https://www.backtrader.com/docu/pandas-datafeed/pandas-datafeed/

data from the file is:

,date,open,high,low,close,volume,average,barCount
0,2021-10-07 09:30:00,129.325,131.055,128.745,130.52,-1,-1.0,-1
1,2021-10-07 09:45:00,130.52,131.215,129.82,131.2,-1,-1.0,-1
2,2021-10-07 10:00:00,131.2,131.845,130.98,131.8,-1,-1.0,-1
3,2021-10-07 10:15:00,131.8,132.185,131.625,131.86,-1,-1.0,-1
4,2021-10-07 10:30:00,131.86,132.285,131.645,131.97,-1,-1.0,-1
5,2021-10-07 10:45:00,131.97,132.115,131.735,131.87,-1,-1.0,-1
6,2021-10-07 11:00:00,131.87,132.005,131.235,131.56,-1,-1.0,-1

Please provide the full traceback:

File "/home/user/project/venv/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3444, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-7dc148349399>", line 1, in <module>
runfile('/home/user/project/src/workbench/1_3_csv_to_bt.py', wdir='/home/user/project/src/workbench')
File "/home/sergii/.local/share/JetBrains/IntelliJIdea2021.1/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
File "/home/sergii/.local/share/JetBrains/IntelliJIdea2021.1/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/home/user/project/src/workbench/1_3_csv_to_bt.py", line 73, in <module>
runstrat()
File "/home/user/project/src/workbench/1_3_csv_to_bt.py", line 50, in runstrat
cerebro.run()
File "/home/user/project/venv/lib/python3.9/site-packages/backtrader/cerebro.py", line 1127, in run
runstrat = self.runstrategies(iterstrat)
File "/home/user/project/venv/lib/python3.9/site-packages/backtrader/cerebro.py", line 1212, in runstrategies
data.preload()
File "/home/user/project/venv/lib/python3.9/site-packages/backtrader/feed.py", line 438, in preload
while self.load():
File "/home/user/project/venv/lib/python3.9/site-packages/backtrader/feed.py", line 479, in load
_loadret = self._load()
File "/home/user/project/venv/lib/python3.9/site-packages/backtrader/feeds/pandafeed.py", line 268, in _load
dt = tstamp.to_pydatetime()
AttributeError: 'numpy.int64' object has no attribute 'to_pydatetime'

Please provide any additional information below:

Versions

same thing. reproducable. #520
in oct 10 2021.
all latest v.:

  • Backtrader version: 1.9.76.123
  • Empyrical version: 0.5.5
  • Pyfolio version: 0.9.2
  • Python version: 3.9.7
  • Pandas version: 1.3.3
  • Matplotlib version: 3.4.3

not an issue. closing. (was related to the scv data)

You have indexes bro, and it is trying to use it as date