pyfolio
pyfolio copied to clipboard
'NaTType' object has no attribute 'ordinal'
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)
File "/opt/anaconda3/envs/rlf/lib/python3.7/site-packages/matplotlib/axis.py", line 1573, in convert_units
ret = self.converter.convert(x, self.units, self)
File "/opt/anaconda3/envs/rlf/lib/python3.7/site-packages/pandas/plotting/_matplotlib/converter.py", line 231, in convert
values = PeriodConverter._convert_1d(values, units, axis)
File "/opt/anaconda3/envs/rlf/lib/python3.7/site-packages/pandas/plotting/_matplotlib/converter.py", line 250, in _convert_1d
return [get_datevalue(x, axis.freq) for x in values]
File "/opt/anaconda3/envs/rlf/lib/python3.7/site-packages/pandas/plotting/_matplotlib/converter.py", line 250, in <listcomp>
return [get_datevalue(x, axis.freq) for x in values]
File "/opt/anaconda3/envs/rlf/lib/python3.7/site-packages/pandas/plotting/_matplotlib/converter.py", line 258, in get_datevalue
return Period(date, freq).ordinal
AttributeError: 'NaTType' object has no attribute 'ordinal'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<ipython-input-126-d7bb60dfd7ac>", line 1, in <module>
pyfolio.tears.create_full_tear_sheet(returns1, positions1, transactions1, round_trips=True)
File "/opt/anaconda3/envs/rlf/lib/python3.7/site-packages/pyfolio/tears.py", line 190, in create_full_tear_sheet
set_context=set_context)
File "/opt/anaconda3/envs/rlf/lib/python3.7/site-packages/pyfolio/plotting.py", line 52, in call_w_context
return func(*args, **kwargs)
File "/opt/anaconda3/envs/rlf/lib/python3.7/site-packages/pyfolio/tears.py", line 570, in create_returns_tear_sheet
returns, top=5, ax=ax_drawdown)
File "/opt/anaconda3/envs/rlf/lib/python3.7/site-packages/pyfolio/plotting.py", line 446, in plot_drawdown_periods
color=colors[i])
File "/opt/anaconda3/envs/rlf/lib/python3.7/site-packages/matplotlib/__init__.py", line 1565, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File "/opt/anaconda3/envs/rlf/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 5139, in fill_between
x = ma.masked_invalid(self.convert_xunits(x))
File "/opt/anaconda3/envs/rlf/lib/python3.7/site-packages/matplotlib/artist.py", line 178, in convert_xunits
return ax.xaxis.convert_units(x)
File "/opt/anaconda3/envs/rlf/lib/python3.7/site-packages/matplotlib/axis.py", line 1576, in convert_units
f'units: {x!r}') from e
ConversionError: Failed to convert value(s) to axis units: (NaT, Timestamp('2021-08-03 00:00:00+0000', tz='UTC'))
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.
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])