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

Viewed 7957

I am new to Python. I have started to use pyfolio library and when I typed the following code

pf.create_returns_tear_sheet(data['New_Adjusted_Returns'],benchmark_rets=None)

An error named occurred as follow:

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

data['New_Adjusted_Returns'] consists of the following data:

Date
2020-02-14   -0.004500
2020-02-17   -0.022107
2020-02-18   -0.000029
2020-02-19   -0.000800
2020-02-20   -0.017102
2020-02-21   -0.000028
2020-02-24    0.014400
2020-02-25    0.007900
2020-02-26   -0.001000
2020-02-27   -0.000517
2020-02-28   -0.000029


Would someone be able to help me on this issue? Thank you very much.
4 Answers

fixed this by changing line 893 in file timeseries.py

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

I met the same question in my huawei laptop,but it's ok in my Surface. I solved this problem by following corrections.

On line 894, replace

valley = np.argmin(underwater)

with

valley=underwater.index[np.argmin(underwater)]

On line 897, replace

peak=valley = np.argmin(underwater)

with

temp1=underwater[:valley][underwater[:valley] == 0].dropna(axis=0,how='any')
peak = temp1.index[-1]

On line 901, replace

recovery=underwater[valley:][underwater[valley:] == 0]

with

temp2=underwater[valley:][underwater[valley:] == 0].dropna(axis=0,how='any')
    recovery = temp2.index[0]

#####################

And if there are still errors about to_pydatetime after applying above solutions, you can replace .to_pydatetime().strftime('%Y-%m-%d')) with .strftime('%Y-%m-%d')) on lines 1010, 1013, and 1018.

From the error message retrieve the error code file, for example:

!cat /usr/local/lib/python3.7/site-packages/pyfolio/timeseries.py

Copy the output to notepade++, and replace line 1005, 1008, and 1015 expressions involving *.to_pydatetime() by

pd.to_datetime(peak)
pd.to_datetime(valley
pd.to_datetime(recovery)

respectively.

Copy the revised contents to a cell beginning with

%%writefile /usr/local/lib/python3.7/site-packages/pyfolio/timeseries.py

and execute it to overwrite the original file.

Then execute the following before rerun the pf.create_full_tear_sheet(data) that for example bring about this error:

%load_ext autoreload
%autoreload 2

I pip installed pyfolio and was getting the same error.

After some digging I found this function :

def get_max_drawdown_underwater(underwater):
   ...

And that the valley variable was ill-defined resulting in the returned variable being of type nump.int64 and not timestamp as stated in the function description.

vallay = underwater.index[np.argmin(underwater)] # np.argmin(underwater)

In white is what I have written and commented out is what I got from the pip install.

All the metrics and graphs appear now:

figure output

Related