4

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.
7
  • The ,benchmark_rets=None is superfluous, as this is the default value for that named parameter. Apparently, the data you're passing in to .create_returns_tear_sheet() is not in the expected format and a pandas datetime was expected instead of a numpy 64-bit integer. You've provided a print out of the data after loading, but how do you load it and from what? Commented Aug 24, 2020 at 4:30
  • I extracted the date index and column values from the dataframe, so I assume it should not be in numpy. I checked the data, the date is datetime object and the values are float 64. I really don't know why the error occurred like that.. Commented Aug 24, 2020 at 5:39
  • What is the type of data, how did you construct it and where did the data come from? Commented Aug 24, 2020 at 5:47
  • The type of data is float64. I construct them by making use of data from the imported csv file using pandas library. The data could be acquired here:hk.investing.com/indices/sse-50-futures-historical-data Commented Aug 24, 2020 at 5:58
  • 1
    The solution in this answer solved the problem for me: stackoverflow.com/questions/65418898/…. An alternative solution is to use this package instead: pypi.org/project/QuantStats Commented May 16, 2021 at 2:48

4 Answers 4

12

fixed this by changing line 893 in file timeseries.py

valley = underwater.index[np.argmin(underwater)] # end of the period
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much Anoop. I solved my problem as well
2

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.

Comments

1

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

Comments

0

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

1 Comment

Hi @Mandlenkosi Ngcobo. Please don't post picture of code. Instead use the code block because it is easy to copy and past. And the formatting is easyier for you, too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.