stefan-jansen / pyfolio-reloaded

Portfolio and risk analytics in Python
https://pyfolio.ml4trading.io/
Apache License 2.0
348 stars 109 forks source link

ConversionError when running plot_drawdown_periods #35

Open ktan636 opened 1 year ago

ktan636 commented 1 year ago

Problem Description

I hit a conversion error when running pyfolio.plotting.plot_drawdown_periods(). First, I ran a backtest in Zipline-reloaded with a start date of 2022-01-02 and an end date of 2023-05-31 and the backtest completed successfully. Then, I extracted the returns from the backtest result using pyfolio.utils.extract_rets_pos_txn_from_zipline(result). Finally, I ran pyfolio.plotting.plot_drawdown_periods(returns) and that threw a ConversionError.

Please provide a minimal, self-contained, and reproducible example:

import pyfolio as pf
returns, positions, transactions = pf.utils.extract_rets_pos_txn_from_zipline(perf)  ## perf is output from zipline algorithm
pf.plot_drawdown_periods(returns)

Please provide the full traceback:

TypeError                                 Traceback (most recent call last)
File [c:\Users\ktan636\Anaconda3\envs\zipline310\lib\site-packages\matplotlib\axis.py:1732](file:///C:/Users/ktan636/Anaconda3/envs/zipline310/lib/site-packages/matplotlib/axis.py:1732), in Axis.convert_units(self, x)
   1731 try:
-> 1732     ret = self.converter.convert(x, self.units, self)
   1733 except Exception as e:

File [c:\Users\ktan636\Anaconda3\envs\zipline310\lib\site-packages\pandas\plotting\_matplotlib\converter.py:287](file:///C:/Users/ktan636/Anaconda3/envs/zipline310/lib/site-packages/pandas/plotting/_matplotlib/converter.py:287), in DatetimeConverter.convert(values, unit, axis)
    286 else:
--> 287     values = DatetimeConverter._convert_1d(values, unit, axis)
    288 return values

File [c:\Users\ktan636\Anaconda3\envs\zipline310\lib\site-packages\pandas\plotting\_matplotlib\converter.py:322](file:///C:/Users/ktan636/Anaconda3/envs/zipline310/lib/site-packages/pandas/plotting/_matplotlib/converter.py:322), in DatetimeConverter._convert_1d(values, unit, axis)
    320         pass
--> 322     values = dates.date2num(values)
    324 return values

File [c:\Users\ktan636\Anaconda3\envs\zipline310\lib\site-packages\matplotlib\dates.py:462](file:///C:/Users/ktan636/Anaconda3/envs/zipline310/lib/site-packages/matplotlib/dates.py:462), in date2num(d)
    461         d = np.asarray(d)
--> 462     d = d.astype('datetime64[us]')
    464 d = np.ma.masked_array(d, mask=mask) if masked else d

TypeError: 'float' object cannot be interpreted as an integer

The above exception was the direct cause of the following exception:
...
-> 1734     raise munits.ConversionError('Failed to convert value(s) to axis '
   1735                                  f'units: {x!r}') from e
   1736 return ret

ConversionError: Failed to convert value(s) to axis units: (NaT, Timestamp('2023-05-31 00:00:00+0000', tz='UTC'))

Please provide any additional information below:

returns.tail()

2023-05-24 00:00:00+00:00 -0.001341 2023-05-25 00:00:00+00:00 0.002400 2023-05-26 00:00:00+00:00 0.003129 2023-05-30 00:00:00+00:00 0.000319 2023-05-31 00:00:00+00:00 -0.001153 Name: returns, dtype: float64

Versions