Skip to content

plot_timeseries does not handle types properly in Python 3.12 #61

@SergeyHein

Description

@SergeyHein

Setup

  • quantstats_lumi 0.3.3
  • pandas 2.2.3
  • python 3.12
  • OS WSL / Ubuntu-24.04

Error

I'm receiving error when calling qs.reports.full

/******************-s8VZZPBo-py3.12/lib/python3.12/site-packages/quantstats_lumi/_plotting/core.py in ?(returns, benchmark, title, compound, cumulative, fill, returns_label, hline, hlw, hlcolor, hllabel, percent, match_volatility, log_scale, resample, lw, figsize, ylabel, grayscale, fontname, subtitle, savefig, show)
    357     # Set y-axis limits to avoid blank space at the bottom and top
    358     min_val = returns.min()
    359     max_val = returns.max()
    360     if benchmark is not None:
--> 361         min_val = min(min_val, benchmark.min())
    362         max_val = max(max_val, benchmark.max())
    363     ax.set_ylim(bottom=min_val, top=max_val)
    364 

/******************-s8VZZPBo-py3.12/lib/python3.12/site-packages/pandas/core/generic.py in ?(self)
   1575     @final
   1576     def __nonzero__(self) -> NoReturn:
-> 1577         raise ValueError(
   1578             f"The truth value of a {type(self).__name__} is ambiguous. "
   1579             "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
   1580         )

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Root cause

returns.min() is a Series whereas benchmark.min() is a float

Proposed solution

    # Set y-axis limits to avoid blank space at the bottom and top
    min_val = returns.min()
    max_val = returns.max()
    if isinstance(returns, _pd.DataFrame):
        min_val = min_val.iloc[0]
        max_val = max_val.iloc[0]
    if benchmark is not None:
        min_val = min(min_val, benchmark.min())
        max_val = max(max_val, benchmark.max())

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions