Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
kwangbkim opened this issue Feb 25, 2018 · 40 comments
Closed
Labels

Comments

@kwangbkim
Copy link

occurs when trying to run an example from the docs

stock_rets = pf.utils.get_symbol_rets('FB')
pf.create_returns_tear_sheet(stock_rets, live_start_date='2015-12-1')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-40-feb97330a052> in <module>()
----> 1 pf.create_returns_tear_sheet(df, benchmark_rets=benchmark_rets)

~/.pyenv/versions/3.6.0/envs/pyfolio/lib/python3.6/site-packages/pyfolio/plotting.py in call_w_context(*args, **kwargs)
     50         if set_context:
     51             with plotting_context(), axes_style():
---> 52                 return func(*args, **kwargs)
     53         else:
     54             return func(*args, **kwargs)

~/.pyenv/versions/3.6.0/envs/pyfolio/lib/python3.6/site-packages/pyfolio/tears.py in create_returns_tear_sheet(returns, positions, transactions, live_start_date, cone_std, benchmark_rets, bootstrap, return_fig)
    455                              live_start_date=live_start_date)
    456 
--> 457     plotting.show_worst_drawdown_periods(returns)
    458 
    459     # If the strategy's history is longer than the benchmark's, limit strategy

~/.pyenv/versions/3.6.0/envs/pyfolio/lib/python3.6/site-packages/pyfolio/plotting.py in show_worst_drawdown_periods(returns, top)
   1686     """
   1687 
-> 1688     drawdown_df = timeseries.gen_drawdown_table(returns, top=top)
   1689     utils.print_table(drawdown_df.sort_values('Net drawdown in %',
   1690                                               ascending=False),

~/.pyenv/versions/3.6.0/envs/pyfolio/lib/python3.6/site-packages/pyfolio/timeseries.py in gen_drawdown_table(returns, top)
    995                                                                 recovery,
    996                                                                 freq='B'))
--> 997         df_drawdowns.loc[i, 'Peak date'] = (peak.to_pydatetime()
    998                                             .strftime('%Y-%m-%d'))
    999         df_drawdowns.loc[i, 'Valley date'] = (valley.to_pydatetime()

AttributeError: 'numpy.int64' object has no attribute 'to_pydatetime'
@rkohli3
Copy link

rkohli3 commented Feb 25, 2018

I see the same error. I updated to the latest version of pyfolio and still the error doesn't go away. T

@brian-from-quantrocket
Copy link

I see this error as well. It was introduced by empyrical 0.4.0. Downgrading empyrical resolves the error:

pip install empyrical==0.3.4

@ssanderson
Copy link
Contributor

I think the issue here is that ep.cum_returns isn't preserving the index of its input on the latest empyrical:

On 0.3.4 I see:

In [4]: ep.cum_returns(s)
Out[4]:
2017-01-01    0
2017-01-02    0
2017-01-03    0
2017-01-04    0
2017-01-05    0
2017-01-06    0
2017-01-07    0
2017-01-08    0
2017-01-09    0
2017-01-10    0
Freq: D, dtype: int64

On 0.4.0 I see:

In [13]: ep.cum_returns(s)
Out[13]:
0    0
1    0
2    0
3    0
4    0
5    0
6    0
7    0
8    0
9    0
dtype: int64

@ssanderson
Copy link
Contributor

Tentative bugfix PR here: https://github.com/quantopian/empyrical/compare/fix-cum_returns-index?expand=1.

@ssanderson ssanderson added the bug label Feb 27, 2018
@ya2366
Copy link

ya2366 commented Feb 28, 2018

Hi, I tried to downgrade empyrical to 0.3.4 but it didn't solve the error. Is there any new progress on this issue?

@rkohli3
Copy link

rkohli3 commented Feb 28, 2018

As mentioned by ssanderson, there’s a bug with cum returns. I’d suggest go into the scrip and change the code manually. Worked for me on ep 0.4.0.

Find def cum_returns, change the code as suggested in the link, and reload module.

@ya2366
Copy link

ya2366 commented Feb 28, 2018

Thanks

@jtromans
Copy link

jtromans commented Mar 7, 2018

Was this fix already merged? When I load the link it says there is nothing to compare and therefore can't see the fix.

@richafrank
Copy link
Member

It was merged: quantopian/empyrical#85, but I don't think the fixed empyrical has been released to PyPI yet.

@jtromans
Copy link

jtromans commented Mar 7, 2018 via email

@ssanderson
Copy link
Contributor

I just released empyrical 0.4.1, which has the required bugfix. If you run into this issue with empyrical >=0.4.1, please let us know here.

@appusinghi
Copy link

How do I resolve this ?


AttributeError Traceback (most recent call last)
in
----> 1 pf.create_full_tear_sheet(data)

C:\ProgramData\Anaconda3\lib\site-packages\pyfolio\tears.py in create_full_tear_sheet(returns, positions, transactions, market_data, benchmark_rets, slippage, live_start_date, sector_mappings, bayesian, round_trips, estimate_intraday, hide_positions, cone_std, bootstrap, unadjusted_returns, style_factor_panel, sectors, caps, shares_held, volumes, percentile, turnover_denom, set_context, factor_returns, factor_loadings, pos_in_dollars, header_rows, factor_partitions)
209 turnover_denom=turnover_denom,
210 header_rows=header_rows,
--> 211 set_context=set_context)
212
213 create_interesting_times_tear_sheet(returns,

C:\ProgramData\Anaconda3\lib\site-packages\pyfolio\plotting.py in call_w_context(*args, **kwargs)
50 if set_context:
51 with plotting_context(), axes_style():
---> 52 return func(*args, **kwargs)
53 else:
54 return func(*args, **kwargs)

C:\ProgramData\Anaconda3\lib\site-packages\pyfolio\tears.py in create_returns_tear_sheet(returns, positions, transactions, live_start_date, cone_std, benchmark_rets, bootstrap, turnover_denom, header_rows, return_fig)
502 header_rows=header_rows)
503
--> 504 plotting.show_worst_drawdown_periods(returns)
505
506 vertical_sections = 11

C:\ProgramData\Anaconda3\lib\site-packages\pyfolio\plotting.py in show_worst_drawdown_periods(returns, top)
1662 """
1663
-> 1664 drawdown_df = timeseries.gen_drawdown_table(returns, top=top)
1665 utils.print_table(
1666 drawdown_df.sort_values('Net drawdown in %', ascending=False),

C:\ProgramData\Anaconda3\lib\site-packages\pyfolio\timeseries.py in gen_drawdown_table(returns, top)
1004 recovery,
1005 freq='B'))
-> 1006 df_drawdowns.loc[i, 'Peak date'] = (peak.to_pydatetime()
1007 .strftime('%Y-%m-%d'))
1008 df_drawdowns.loc[i, 'Valley date'] = (valley.to_pydatetime()

AttributeError: 'datetime.date' object has no attribute 'to_pydatetime'

@stnatter
Copy link

stnatter commented Mar 3, 2020

I am having the same problem with empirical 0.5.3

@sokol11
Copy link

sokol11 commented Mar 6, 2020

I have the same problem. pyfolio 0.9.2, empyrical 0.5.3

@stnatter
Copy link

stnatter commented Mar 6, 2020

seems this has been fixed in #634

@sokol11
Copy link

sokol11 commented Mar 7, 2020

@stnatter Hi Stephen. Your suggestion worked like a charm. I appreciate it.

@bviorst
Copy link

bviorst commented Mar 22, 2020

I am getting this error (empirical 0.5.3) when I run this line of code:
pf.create_full_tear_sheet(data). Not sure how to fix. Please help, thanks.


AttributeError Traceback (most recent call last)
in
1 import numpy as np
----> 2 pf.create_full_tear_sheet(data)

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyfolio/tears.py in create_full_tear_sheet(returns, positions, transactions, market_data, benchmark_rets, slippage, live_start_date, sector_mappings, bayesian, round_trips, estimate_intraday, hide_positions, cone_std, bootstrap, unadjusted_returns, style_factor_panel, sectors, caps, shares_held, volumes, percentile, turnover_denom, set_context, factor_returns, factor_loadings, pos_in_dollars, header_rows, factor_partitions)
199 set_context=set_context,
200 sector_mappings=sector_mappings,
--> 201 estimate_intraday=False)
202
203 if transactions is not None:

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyfolio/plotting.py in call_w_context(*args, **kwargs)
50 if set_context:
51 with plotting_context(), axes_style():
---> 52 return func(*args, **kwargs)
53 else:
54 return func(*args, **kwargs)

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyfolio/tears.py in create_returns_tear_sheet(returns, positions, transactions, live_start_date, cone_std, benchmark_rets, bootstrap, turnover_denom, header_rows, return_fig)
502 if benchmark_rets is not None:
503 ax_rolling_beta = plt.subplot(gs[i, :], sharex=ax_rolling_returns)
--> 504 i += 1
505 ax_rolling_volatility = plt.subplot(gs[i, :], sharex=ax_rolling_returns)
506 i += 1

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyfolio/plotting.py in show_worst_drawdown_periods(returns, top)
1662 Parameters
1663 ----------
-> 1664 returns : pd.Series
1665 Daily returns of the strategy, noncumulative.
1666 - See full explanation in tears.create_full_tear_sheet.

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyfolio/timeseries.py in gen_drawdown_table(returns, top)
1006 df_drawdowns.loc[i, 'Peak date'] = (peak.to_pydatetime()
1007 .strftime('%Y-%m-%d'))
-> 1008 df_drawdowns.loc[i, 'Valley date'] = (valley.to_pydatetime()
1009 .strftime('%Y-%m-%d'))
1010 if isinstance(recovery, float):

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

@ivannp
Copy link

ivannp commented Apr 3, 2020

Same here

@ivannp
Copy link

ivannp commented Apr 3, 2020

Fixed by using:

pip install git+https://github.com/quantopian/pyfolio

This has been fixed in #634 as stated by stnatter above.

@Ivyzxok
Copy link

Ivyzxok commented May 21, 2020

I already install the git+, but still not fix this problem

@Chin-I
Copy link

Chin-I commented May 31, 2020

Same problem.

=my error message =
\pyfolio\timeseries.py in gen_drawdown_table(returns, top)
1006 df_drawdowns.loc[i, 'Peak date'] = (peak.to_pydatetime()
1007 .strftime('%Y-%m-%d'))
-> 1008 df_drawdowns.loc[i, 'Valley date'] = (valley.to_pydatetime()
1009 .strftime('%Y-%m-%d'))
1010 if isinstance(recovery, float):

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

=my conda list=
empyrical 0.5.3 pypi_0 pypi
pyfolio 0.9.2+73.gcfdf82a pypi_0 pypi

@nickgeoca
Copy link

Same problem here

~/.local/lib/python3.8/site-packages/pyfolio/timeseries.py in gen_drawdown_table(returns, top)
   1006         df_drawdowns.loc[i, 'Peak date'] = (peak.to_pydatetime()
   1007                                             .strftime('%Y-%m-%d'))
-> 1008         df_drawdowns.loc[i, 'Valley date'] = (valley.to_pydatetime()
   1009                                               .strftime('%Y-%m-%d'))
   1010         if isinstance(recovery, float):

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

pandas 1.0.5
pyfolio 0.9.2
empyrical 0.5.3

@VHKA1729
Copy link

To fix the error, if working on Google Colab, use:

pip install git+https://github.com/quantopian/pyfolio

@nickgeoca
Copy link

Cool seems to work now. @VHKA1729 , I think I was doing this before. Not sure what changed, but thanks anyway 👍

pandas 1.0.5
pyfolio 0.9.2+73.gcfdf82a
empyrical 0.5.3

@steelep
Copy link

steelep commented Jul 5, 2020

Hmmm im still getting that error - even running the patch:

pandas                     1.0.5 
empyrical                 0.5.3       
pyfolio                     0.9.2+73.gcfdf82a  

@euribate
Copy link

euribate commented Jul 31, 2020

Hi all I getting the same issue, is anybody looking at this? with this error I cannot use pyfolio.
python 3.6
pandas 1.0.5
numpy 1.18.5
pyfolio 0.9.2
empyrical 0.5.3

I got the foloowing error:

AttributeError Traceback (most recent call last)
in
----> 1 pf.create_full_tear_sheet(Globale, benchmark_rets=Bench)

~\Miniconda3\envs\lavoro\lib\site-packages\pyfolio\tears.py in create_full_tear_sheet(returns, positions, transactions, market_data, benchmark_rets, slippage, live_start_date, sector_mappings, bayesian, round_trips, estimate_intraday, hide_positions, cone_std, bootstrap, unadjusted_returns, style_factor_panel, sectors, caps, shares_held, volumes, percentile, turnover_denom, set_context, factor_returns, factor_loadings, pos_in_dollars, header_rows, factor_partitions)
209 turnover_denom=turnover_denom,
210 header_rows=header_rows,
--> 211 set_context=set_context)
212
213 create_interesting_times_tear_sheet(returns,

~\Miniconda3\envs\lavoro\lib\site-packages\pyfolio\plotting.py in call_w_context(*args, **kwargs)
50 if set_context:
51 with plotting_context(), axes_style():
---> 52 return func(*args, **kwargs)
53 else:
54 return func(*args, **kwargs)

~\Miniconda3\envs\lavoro\lib\site-packages\pyfolio\tears.py in create_returns_tear_sheet(returns, positions, transactions, live_start_date, cone_std, benchmark_rets, bootstrap, turnover_denom, header_rows, return_fig)
502 header_rows=header_rows)
503
--> 504 plotting.show_worst_drawdown_periods(returns)
505
506 vertical_sections = 11

~\Miniconda3\envs\lavoro\lib\site-packages\pyfolio\plotting.py in show_worst_drawdown_periods(returns, top)
1662 """
1663
-> 1664 drawdown_df = timeseries.gen_drawdown_table(returns, top=top)
1665 utils.print_table(
1666 drawdown_df.sort_values('Net drawdown in %', ascending=False),

~\Miniconda3\envs\lavoro\lib\site-packages\pyfolio\timeseries.py in gen_drawdown_table(returns, top)
1006 df_drawdowns.loc[i, 'Peak date'] = (peak.to_pydatetime()
1007 .strftime('%Y-%m-%d'))
-> 1008 df_drawdowns.loc[i, 'Valley date'] = (valley.to_pydatetime()
1009 .strftime('%Y-%m-%d'))
1010 if isinstance(recovery, float):

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

@akalingking
Copy link

This seems to work for me 'pip install git+https://github.com/quantopian/pyfolio' as mentioned above.

@euribate
Copy link

Dear Ariel, I tried but got the same message. While installing "pip install git+https://github.com/quantopian/pyfolio" i got this message:

Collecting git+https://github.com/quantopian/pyfolio
Cloning https://github.com/quantopian/pyfolio to c:\users\u19040\appdata\local\temp\pip-req-build-8_z5l1d7
Note: you may need to restart the kernel to use updated packages.

Running command git clone -q https://github.com/quantopian/pyfolio 'C:\Users\u19040\AppData\Local\Temp\pip-req-build-8_z5l1d7'
ERROR: Error [WinError 2] Impossibile trovare il file specificato while executing command git clone -q https://github.com/quantopian/pyfolio 'C:\Users\u19040\AppData\Local\Temp\pip-req-build-8_z5l1d7'
ERROR: Cannot find command 'git' - do you have 'git' installed and in your PATH?

@euribate
Copy link

This seems to work for me 'pip install git+https://github.com/quantopian/pyfolio' as mentioned above.

This seems to work for me 'pip install git+https://github.com/quantopian/pyfolio' as mentioned above.

Dear Ariel, I tried but got the same message. While installing "pip install git+https://github.com/quantopian/pyfolio" i got this message:

Collecting git+https://github.com/quantopian/pyfolio
Cloning https://github.com/quantopian/pyfolio to c:\users\u19040\appdata\local\temp\pip-req-build-8_z5l1d7
Note: you may need to restart the kernel to use updated packages.

Running command git clone -q https://github.com/quantopian/pyfolio 'C:\Users\u19040\AppData\Local\Temp\pip-req-build-8_z5l1d7'
ERROR: Error [WinError 2] Impossibile trovare il file specificato while executing command git clone -q https://github.com/quantopian/pyfolio 'C:\Users\u19040\AppData\Local\Temp\pip-req-build-8_z5l1d7'
ERROR: Cannot find command 'git' - do you have 'git' installed and in your PATH?

@akalingking
Copy link

It says you may not have git and need to install it, also if you're on python3 use pip3

@euribate
Copy link

It says you may not have git and need to install it, also if you're on python3 use pip3

I tried not working...Now I am trying to install different version of Python and numpy/pandas. Do you know if with a particolar version pyfolio work? or it is just a waste of time?

@euribate
Copy link

for the benefit of all, I tried with python 3.7 and the 'pip install git+https://github.com/quantopian/pyfolio' and it is working

@filipefeitosa
Copy link

I've already installed 'pip install git+https://github.com/quantopian/pyfolio' and I'm using versions:

pandas 1.0.5
pyfolio 0.9.2
empyrical 0.5.3

But I'm having problems with charts as you can see in the images bellow. Someone can help me?

Screen Shot 2020-09-02 at 11 13 51

@ghost
Copy link

ghost commented Oct 17, 2020

fixed this by changing line 893 in file timeseries.py
valley = underwater.index[np.argmin(underwater)-1] # end of the period

@PhanikumarCh
Copy link

Thanks Anoop. Fixed it.

@Ramoncontabil
Copy link

Fixed:
df_drawdowns.loc[i, 'Peak date'] = (peak.dt.to_pydatetime()
.strftime('%Y-%m-%d'))
df_drawdowns.loc[i, 'Valley date'] = (valley.dt.to_pydatetime()
.strftime('%Y-%m-%d'))
if isinstance(recovery, float):
df_drawdowns.loc[i, 'Recovery date'] = recovery
else:
df_drawdowns.loc[i, 'Recovery date'] = (recovery.dt.to_pydatetime()
.strftime('%Y-%m-%d'))

@mksamanes
Copy link

Hi @Ramoncontabil How did you fix it? I replace lines 1008 to 1016 with what you wrote but I still have the issue, what versions should I have why is this not working I also replace line 893 and the error says is on line 1008

@Sergey80
Copy link

Sergey80 commented Oct 10, 2021

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

  • empyrical==0.5.5
  • pyfolio==0.9.2
  • pandas==1.3.3

@gstulli
Copy link

gstulli commented Nov 20, 2023

Para corrigir o erro, basta alterar a linha nº 893 do código de timeseries.py na def get_max_drawdown_underwater(underwater):
de:
valley = np.argmin(underwater) # end of the period
para:
valley = underwater.index[np.argmin(underwater)] # end of the period

@samlopezruiz
Copy link

samlopezruiz commented Dec 12, 2023

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

* empyrical==0.5.5

* pyfolio==0.9.2

* pandas==1.3.3

x2, but with pandas==2.1.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests