I encountered a TypeError when trying to generate a report using the quantstats.reports.html function. The error message indicates an issue with the pivot method in the quantstats library.
Steps to Reproduce
Set up the environment with the following packages:
Python 3.9.19
pandas 2.2.2
numpy 1.26.4
scipy 1.12.0
matplotlib 3.8.4
seaborn 0.13.2
tabulate 0.9.0
yfinance 0.2.40
plotly 5.20.0
quantstats 0.0.50
Run the following script:
python
Copy code
import pandas as pd
import quantstats as qs
import yfinance as yf
Fetch the daily returns for a stock
stock = qs.utils.download_returns('META')
Ensure datetime index of stock is timezone-naive
stock.index = stock.index.tz_localize(None)
Fetch the benchmark data
benchmark = qs.utils.download_returns('SPY')
Ensure datetime index of benchmark is timezone-naive
I encountered a TypeError when trying to generate a report using the quantstats.reports.html function. The error message indicates an issue with the pivot method in the quantstats library.
Steps to Reproduce Set up the environment with the following packages:
Python 3.9.19 pandas 2.2.2 numpy 1.26.4 scipy 1.12.0 matplotlib 3.8.4 seaborn 0.13.2 tabulate 0.9.0 yfinance 0.2.40 plotly 5.20.0 quantstats 0.0.50 Run the following script:
python Copy code import pandas as pd import quantstats as qs import yfinance as yf
Fetch the daily returns for a stock
stock = qs.utils.download_returns('META')
Ensure datetime index of stock is timezone-naive
stock.index = stock.index.tz_localize(None)
Fetch the benchmark data
benchmark = qs.utils.download_returns('SPY')
Ensure datetime index of benchmark is timezone-naive
benchmark.index = benchmark.index.tz_localize(None)
Ensure the data is aligned by reindexing the benchmark to the stock index
benchmark = benchmark.reindex(stock.index).dropna()
Show sharpe ratio
print(qs.stats.sharpe(stock))
Or using extend_pandas() :)
print(stock.sharpe())
Generate the quantstats report
qs.reports.html(stock, benchmark, output='report.html')
TypeError Traceback (most recent call last) File ~\anaconda3\envs\myenv\lib\site-packages\quantstats\reports.py:185, in html(returns, benchmark, rf, grayscale, title, output, compounded) 182 tpl = tpl.replace('{{title}}', title) 184 figfile = _utils._file_stream() 185 _plots.monthly_heatmap(returns, grayscale=grayscale, 186 figsize=(8, 4), cbar=False, 187 savefig={'fname': figfile, 'format': 'svg'}, 188 show=False, ylabel=False, compounded=compounded) 189 tpl = tpl.replace('{{monthly_heatmap}}', figfile.getvalue().decode()) 191 figfile = _utils._file_stream()
File ~\anaconda3\envs\myenv\lib\site-packages\quantstats_plotting\wrappers.py:568, in monthly_heatmap(returns, annot_size, figsize, cbar, square, compounded, eoy, grayscale, fontname, ylabel, savefig, show) 559 def monthly_heatmap(returns, annot_size=10, figsize=(10, 5), 560 cbar=True, square=False, 561 compounded=True, eoy=False, ... 564 565 # colors, ls, alpha = _core._get_colors(grayscale) 566 cmap = 'gray' if grayscale else 'RdYlGn' 568 returns = _stats.monthly_returns(returns, eoy=eoy, 569 compounded=compounded) * 100 571 fig_height = len(returns) / 3 573 if figsize is None:
File ~\anaconda3\envs\myenv\lib\site-packages\quantstats\stats.py:697, in monthly_returns(returns, eoy, compounded) 694 returns['Month'] = returns.index.strftime('%b') 696 # make pivot table 697 returns = returns.pivot('Year', 'Month', 'Returns').fillna(0) TypeError: pivot() takes 1 positional argument but 4 were given