microsoft/qlib · error · NotImplementedError

Please implement the `abs` method

Error message

Please implement the `abs` method

What it means

BaseSingleMetric.abs() is the abstract element-wise absolute value, returning a new metric of the same type. The base class raises NotImplementedError; SingleMetric implements it. Calling .abs() on a bare BaseSingleMetric raises this error.

Source

Thrown at qlib/backtest/high_performance_ds.py:272

    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")

    @property
    def empty(self) -> bool:
        """If metric is empty, return True."""

        raise NotImplementedError(f"Please implement the `empty` method")

    def add(self, other: BaseSingleMetric, fill_value: float = None) -> BaseSingleMetric:
        """Replace np.nan with fill_value in two metrics and add them."""

        raise NotImplementedError(f"Please implement the `add` method")

    def replace(self, replace_dict: dict) -> BaseSingleMetric:
        """Replace the value of metric according to replace_dict."""

        raise NotImplementedError(f"Please implement the `replace` method")

    def apply(self, func: Callable) -> BaseSingleMetric:

View on GitHub (pinned to 79633dd950)

Solutions

  1. Use SingleMetric.abs()
  2. Implement abs(self) in your subclass returning type(self)(self.storage.abs())
  3. For quick checks without a subclass, take absolute values on the underlying Series and re-wrap in SingleMetric

Example fix

# before
exposure = BaseSingleMetric(weights).abs().sum()
# after
from qlib.backtest.high_performance_ds import SingleMetric
exposure = SingleMetric(weights).abs().sum()
Defensive patterns

Strategy: type-guard

Validate before calling

from qlib.backtest.high_performance_ds import BaseSingleMetric
assert type(m).abs is not BaseSingleMetric.abs, "object does not implement abs()"

Type guard

def supports_abs(m) -> bool:
    from qlib.backtest.high_performance_ds import BaseSingleMetric
    return isinstance(m, BaseSingleMetric) and type(m).abs is not BaseSingleMetric.abs

Prevention

When it happens

Trigger: m.abs() where m is a raw BaseSingleMetric; risk metrics like total absolute exposure or sum(|weight|); subclasses implementing arithmetic operators but not abs.

Common situations: L1-norm style exposure checks; converting signed PnL to magnitude metrics for reports; custom backends with partial method coverage.

Related errors


AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15). Data as JSON: /api/errors/361ed540c89cd1b9. Report an issue: GitHub.