microsoft/qlib · error · NotImplementedError
Please implement the `mean` method
Error message
Please implement the `mean` method
What it means
BaseSingleMetric.mean() is the abstract per-stock average reduction, returning a float. The base class raises NotImplementedError; SingleMetric implements the NaN-aware mean. Calling .mean() on a bare BaseSingleMetric instance raises this error.
Source
Thrown at qlib/backtest/high_performance_ds.py:264
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")
@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."""
View on GitHub (pinned to 79633dd950)
Solutions
- Use SingleMetric.mean()
- Implement mean(self) in your subclass (NaN-aware, e.g. float(self.storage.mean()) if pandas-backed)
- Reuse the base-class pattern: often mean can be implemented as self.sum() / self.count() once both are concrete
Example fix
# before avg = BaseSingleMetric(values).mean() # after from qlib.backtest.high_performance_ds import SingleMetric avg = SingleMetric(values).mean()
Defensive patterns
Strategy: type-guard
Validate before calling
from qlib.backtest.high_performance_ds import BaseSingleMetric assert type(m).mean is not BaseSingleMetric.mean, "object does not implement mean()"
Type guard
def supports_mean(m) -> bool:
from qlib.backtest.high_performance_ds import BaseSingleMetric
return isinstance(m, BaseSingleMetric) and type(m).mean is not BaseSingleMetric.mean Prevention
- Use SingleMetric.mean() for cross-sectional averages
- Remember mean is NaN-aware: pair it with count() to know the denominator
- Implement all abstract reductions when subclassing, not just the one you need now
When it happens
Trigger: m.mean() where m is a raw BaseSingleMetric; cross-sectional statistics (average score, average weight) in evaluators; subclasses implementing sum but not mean.
Common situations: Reporting average per-stock metrics; factor evaluation code computing cross-sectional means; custom metric classes with partial reduction implementations.
Related errors
- Please implement the `sum` method
- Please implement the `count` method
- Please implement the `add` method
- Please implement the `__init__` method
- Please implement the `__add__` method
AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15).
Data as JSON: /api/errors/24470d140d44a933.
Report an issue: GitHub.