Open Rajmehta123 opened 3 years ago
I did a lot of research and the source of the problem is from the function 'plot_drawdown_periods', Line number 403 in plotting.py in pyfolio. Specifically the line that is calculating the drawdown table 'df_drawdowns = timeseries.gen_drawdown_table(returns, top=top)'. Line number 432 in plotting.py in pyfolio.
While calculating the drawdowns, it has several NaT and nan values that are not able to convert into datetime and hence the error.
The quick fix is to use df_drawdowns.dropna() after calculating but that will create a problem in setting colors[i].
Appreciate the fix. I am trying to understand the function.
Found the fix. Don't process if the peak or recovery are NaT values. Change the for loop to the following. This is not a fix but just to suppress drawdowns for which we don't have peak/recovery values.
for i, (peak, recovery) in df_drawdowns[['Peak date', 'Recovery date']].iterrows():
if not pd.isna(peak) and not pd.isna(recovery):
if pd.isnull(recovery):
recovery = returns.index[-1]
ax.fill_between((peak, recovery),
lim[0],
lim[1],
alpha=.4,
color=colors[i])
I have the returns, positions, and transactions for 30 trading days. It throws an error. But when I run the same for 60 trading days, it works fine.
pyfolio.tears.create_full_tear_sheet(returns1, positions1, transactions1, round_trips=True)
When I run the same code for 60 trading days, it works fine as expected. Looking at the error, it is trying to convert NaT to datetime, but the dataframe has no NaT value.