microsoft/qlib · error · NotImplementedError
Please implement the `__lt__` method
Error message
Please implement the `__lt__` method
What it means
BaseSingleMetric.__lt__ is the abstract less-than operator, designed to yield an element-wise boolean metric. The base class only declares it and raises NotImplementedError; SingleMetric (line 436) implements it. Using < on a raw BaseSingleMetric instance triggers the error.
Source
Thrown at qlib/backtest/high_performance_ds.py:255
raise NotImplementedError(f"Please implement the `__sub__` method")
def __rsub__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:
raise NotImplementedError(f"Please implement the `__rsub__` method")
def __mul__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:
raise NotImplementedError(f"Please implement the `__mul__` method")
def __truediv__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:
raise NotImplementedError(f"Please implement the `__truediv__` method")
def __eq__(self, other: object) -> BaseSingleMetric:
raise NotImplementedError(f"Please implement the `__eq__` method")
def __gt__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:
raise NotImplementedError(f"Please implement the `__gt__` method")
def __lt__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:
raise NotImplementedError(f"Please implement the `__lt__` method")
def __len__(self) -> int:
raise NotImplementedError(f"Please implement the `__len__` method")
def sum(self) -> float:
raise NotImplementedError(f"Please implement the `sum` method")
def mean(self) -> float:
raise NotImplementedError(f"Please implement the `mean` method")
def count(self) -> int:
"""Return the count of the single metric, NaN is not included."""
raise NotImplementedError(f"Please implement the `count` method")
def abs(self) -> BaseSingleMetric:
raise NotImplementedError(f"Please implement the `abs` method")
View on GitHub (pinned to 79633dd950)
Solutions
- Use SingleMetric for less-than comparisons
- Implement __lt__ in your subclass mirroring SingleMetric (element-wise, returns the metric type)
- Extract the underlying Series via .storage for sorting/min operations
Example fix
# before exit_mask = BaseSingleMetric(drawdown) < -0.1 # after from qlib.backtest.high_performance_ds import SingleMetric exit_mask = SingleMetric(drawdown) < -0.1
Defensive patterns
Strategy: type-guard
Validate before calling
from qlib.backtest.high_performance_ds import BaseSingleMetric assert type(m).__lt__ is not BaseSingleMetric.__lt__, "object does not implement __lt__"
Type guard
def supports_lt(m) -> bool:
from qlib.backtest.high_performance_ds import BaseSingleMetric
return isinstance(m, BaseSingleMetric) and type(m).__lt__ is not BaseSingleMetric.__lt__ Prevention
- Use SingleMetric for less-than filters (stop-loss, limits)
- sorted()/min()/max() call __lt__ — apply them to the underlying Series, not metric objects
- Implement __lt__ whenever you implement __gt__
When it happens
Trigger: m < threshold, m1 < m2, or sorted(metrics) where m is a bare BaseSingleMetric; stop-loss checks (price < stop_level); risk filters (exposure < limit); subclasses that implement __gt__ but not __lt__.
Common situations: Building sell/exit masks from metric thresholds; min()/sorted() over metric objects invoking __lt__; partial implementations in custom metric backends.
Related errors
- Please implement the `__gt__` method
- Please implement the `__add__` method
- Please implement the `__sub__` method
- Please implement the `__rsub__` method
- Please implement the `__mul__` method
AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15).
Data as JSON: /api/errors/16a3d016e7647954.
Report an issue: GitHub.