{"record":{"id":"0e480032a7d2ccf7","repo":"microsoft/qlib","slug":"please-implement-the-count-method","errorCode":null,"errorMessage":"Please implement the `count` method","messagePattern":"Please implement the `count` method","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"qlib/backtest/high_performance_ds.py","lineNumber":269,"sourceCode":"    def __gt__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:\n        raise NotImplementedError(f\"Please implement the `__gt__` method\")\n\n    def __lt__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:\n        raise NotImplementedError(f\"Please implement the `__lt__` method\")\n\n    def __len__(self) -> int:\n        raise NotImplementedError(f\"Please implement the `__len__` method\")\n\n    def sum(self) -> float:\n        raise NotImplementedError(f\"Please implement the `sum` method\")\n\n    def mean(self) -> float:\n        raise NotImplementedError(f\"Please implement the `mean` method\")\n\n    def count(self) -> int:\n        \"\"\"Return the count of the single metric, NaN is not included.\"\"\"\n\n        raise NotImplementedError(f\"Please implement the `count` method\")\n\n    def abs(self) -> BaseSingleMetric:\n        raise NotImplementedError(f\"Please implement the `abs` method\")\n\n    @property\n    def empty(self) -> bool:\n        \"\"\"If metric is empty, return True.\"\"\"\n\n        raise NotImplementedError(f\"Please implement the `empty` method\")\n\n    def add(self, other: BaseSingleMetric, fill_value: float = None) -> BaseSingleMetric:\n        \"\"\"Replace np.nan with fill_value in two metrics and add them.\"\"\"\n\n        raise NotImplementedError(f\"Please implement the `add` method\")\n\n    def replace(self, replace_dict: dict) -> BaseSingleMetric:\n        \"\"\"Replace the value of metric according to replace_dict.\"\"\"\n","sourceCodeStart":251,"sourceCodeEnd":287,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/backtest/high_performance_ds.py#L251-L287","documentation":"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.","triggerScenarios":"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.","commonSituations":"Computing divisor for averages (sum/count); universe coverage diagnostics; report code that assumes pandas-like count semantics.","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"],"exampleFix":"# before\nn_valid = BaseSingleMetric(values).count()\n# after\nfrom qlib.backtest.high_performance_ds import SingleMetric\nn_valid = SingleMetric(values).count()  # NaN excluded","handlingStrategy":"type-guard","validationCode":"from qlib.backtest.high_performance_ds import BaseSingleMetric\nassert type(m).count is not BaseSingleMetric.count, \"object does not implement count()\"","typeGuard":"def supports_count(m) -> bool:\n    from qlib.backtest.high_performance_ds import BaseSingleMetric\n    return isinstance(m, BaseSingleMetric) and type(m).count is not BaseSingleMetric.count","tryCatchPattern":null,"preventionTips":["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"],"tags":["python","qlib","abstract-method","aggregation","nan","metrics"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}