microsoft/qlib · error · NotImplementedError
Please implement the `count` method
Error message
Please implement the `count` method
What it means
BaseSingleMetric.count() is abstract. Per its docstring it must return the number of stocks excluding NaN values — unlike len(), which counts all entries. The base class only raises NotImplementedError; SingleMetric implements it. Calling .count() on a bare BaseSingleMetric raises this error.
Source
Thrown at qlib/backtest/high_performance_ds.py:269
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."""
raise NotImplementedError(f"Please implement the `add` method")
def replace(self, replace_dict: dict) -> BaseSingleMetric:
"""Replace the value of metric according to replace_dict."""
View on GitHub (pinned to 79633dd950)
Solutions
- Use SingleMetric.count() for the non-NaN stock count
- Implement count(self) in your subclass, excluding NaN (e.g. int(self.storage.count()) if pandas-backed)
- Distinguish from len(): use count() for statistics denominators, len() for vector size
Example fix
# before n_valid = BaseSingleMetric(values).count() # after from qlib.backtest.high_performance_ds import SingleMetric n_valid = SingleMetric(values).count() # NaN excluded
Defensive patterns
Strategy: type-guard
Validate before calling
from qlib.backtest.high_performance_ds import BaseSingleMetric assert type(m).count is not BaseSingleMetric.count, "object does not implement count()"
Type guard
def supports_count(m) -> bool:
from qlib.backtest.high_performance_ds import BaseSingleMetric
return isinstance(m, BaseSingleMetric) and type(m).count is not BaseSingleMetric.count Prevention
- Use SingleMetric.count() for the non-NaN stock count
- Distinguish count() (non-NaN) from len() (all entries) — mixing them skews averages
- Implement count() alongside sum()/mean() in custom subclasses
When it happens
Trigger: m.count() where m is a raw BaseSingleMetric; coverage statistics (how many stocks hold non-NaN values, e.g. number of active positions); subclasses implementing sum/mean but not count.
Common situations: Computing divisor for averages (sum/count); universe coverage diagnostics; report code that assumes pandas-like count semantics.
Related errors
- Please implement the `add` method
- Please implement the `sum` method
- Please implement the `mean` 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/0e480032a7d2ccf7.
Report an issue: GitHub.