quantopian / pyfolio

Portfolio and risk analytics in Python
https://quantopian.github.io/pyfolio
Apache License 2.0
5.52k stars 1.74k forks source link

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

Open Jiujiuwhoami opened 6 months ago

Jiujiuwhoami commented 6 months ago

Problem Description

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

import pandas as pd
import numpy as np
import pyfolio as pf

np.random.seed(42)
dates = pd.date_range('2022-01-01', periods=100)

returns_data = np.random.randn(100) / 100  
returns = pd.DataFrame({'returns': returns_data}, index=dates)
returns.index = pd.to_datetime(returns.index)
print(returns)
pf.plot_drawdown_periods(returns)

Please provide the full traceback:

Traceback (most recent call last):
  File "D:\quant_ai\mm\backtrader\text.py", line 14, in <module>
    pf.plot_drawdown_periods(returns)
  File "D:\python\lib\site-packages\pyfolio\plotting.py", line 432, in plot_drawdown_periods
    df_drawdowns = timeseries.gen_drawdown_table(returns, top=top)
  File "D:\python\lib\site-packages\pyfolio\timeseries.py", line 1008, in gen_drawdown_table
    df_drawdowns.loc[i, 'Valley date'] = (valley.to_pydatetime()
AttributeError: 'numpy.int64' object has no attribute 'to_pydatetime'

Please provide any additional information below:

Versions

LexieZhuang commented 2 weeks ago

Same! Do you know how to fix it?

Atendimento2005 commented 1 week ago

Here's something that worked for me

Pyfolio version: 0.9.2 Python version: 3.11.7 Pandas version: 1.5.3 Matplotlib version: 3.8.0

I had to change the following function in the site-packages/pyfolio/timeseries.py file, from the commented line to the uncommented one.

def get_max_drawdown_underwater(underwater):

    # valley = np.argmin(underwater)  # end of the period
    valley = underwater.index[np.argmin(underwater)]

Source: here

I would highly recommend you use the pyfolio-reloaded library instead as this project does not seem to be under active development