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.
Jacky Law
  • 53
  • 1
  • 1
  • 6
  • 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? – Grismar Aug 24 '20 at 04: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.. – Jacky Law Aug 24 '20 at 05:39
  • What is the type of `data`, how did you construct it and where did the data come from? – Grismar Aug 24 '20 at 05: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:https://hk.investing.com/indices/sse-50-futures-historical-data – Jacky Law Aug 24 '20 at 05:58
  • So, the type of the `data` variable is a `pandas.DataFrame`? The URL you linked provides no easy way for an English speaker to obtain a .csv - please provide some sample data in your question, as well as an example of the code you use to read it. – Grismar Aug 24 '20 at 06:01
  • Yes, the type of the data variable is a pandas.Dataframe and the data in this dataframe are float64. I apologize for the wrong version provided. You may easily refer to this link for the data sample: https://www.investing.com/indices/sse-50-futures-historical-data – Jacky Law Aug 24 '20 at 06:08
  • 1
    The solution in this answer solved the problem for me: https://stackoverflow.com/questions/65418898/pyfolio-attributeerror-numpy-int64-object-has-no-attribute-to-pydatetime. An alternative solution is to use this package instead: https://pypi.org/project/QuantStats/ – StackG May 16 '21 at 02:48

4 Answers4

11

fixed this by changing line 893 in file timeseries.py

valley = underwater.index[np.argmin(underwater)] # end of the period
Anoop Jangra
  • 111
  • 4
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.

Claire Nielsen
  • 1,881
  • 1
  • 14
  • 31
limingming
  • 21
  • 1
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
Frank
  • 505
  • 5
  • 14
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

mosc9575
  • 5,618
  • 2
  • 9
  • 32
  • 1
    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. – mosc9575 Jan 03 '21 at 16:31