{"record":{"id":"3d5606285c346f06","repo":"microsoft/qlib","slug":"please-implement-the-add-method-3d5606","errorCode":null,"errorMessage":"Please implement the `add` method","messagePattern":"Please implement the `add` method","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"qlib/backtest/high_performance_ds.py","lineNumber":283,"sourceCode":"\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\n        raise NotImplementedError(f\"Please implement the `replace` method\")\n\n    def apply(self, func: Callable) -> BaseSingleMetric:\n        \"\"\"Replace the value of metric with func (metric).\n        Currently, the func is only qlib/backtest/order/Order.parse_dir.\n        \"\"\"\n\n        raise NotImplementedError(f\"Please implement the 'apply' method\")\n\n\nclass BaseOrderIndicator:\n    \"\"\"\n    The data structure of order indicator.\n    !!!NOTE: There are two ways to organize the data structure. Please choose a better way.","sourceCodeStart":265,"sourceCodeEnd":301,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/backtest/high_performance_ds.py#L265-L301","documentation":"BaseSingleMetric.add(other, fill_value=None) is abstract. Per its docstring it adds two metrics after replacing NaN with fill_value in both — the NaN-handling counterpart to __add__. The base class raises NotImplementedError; SingleMetric implements it. Calling .add(...) on a bare BaseSingleMetric raises this error.","triggerScenarios":"m1.add(m2, fill_value=0.0) where m1 is a raw BaseSingleMetric; combining two per-stock metrics whose stock universes differ (e.g. today's positions + yesterday's cash-flow series with missing instruments); subclasses implementing __add__ but not add().","commonSituations":"Time-series accumulation of daily metrics where coverage changes day to day; aligning metrics with disjoint stock ids; porting pandas Series.add(fill_value=...) idioms onto metric objects.","solutions":["Use SingleMetric.add(other, fill_value=...), which mirrors pandas' fill_value semantics","Implement add(self, other, fill_value) in your subclass: fill NaN on both sides, then add element-wise","Alternatively pre-fill both underlying Series with .fillna(fill_value) and use +"],"exampleFix":"# before\ntotal = BaseSingleMetric(today).add(BaseSingleMetric(yesterday), fill_value=0.0)\n# after\nfrom qlib.backtest.high_performance_ds import SingleMetric\ntotal = SingleMetric(today).add(SingleMetric(yesterday), fill_value=0.0)","handlingStrategy":"type-guard","validationCode":"from qlib.backtest.high_performance_ds import BaseSingleMetric\nassert type(m).add is not BaseSingleMetric.add, \"object does not implement add(fill_value=...)\"","typeGuard":"def supports_add_method(m) -> bool:\n    from qlib.backtest.high_performance_ds import BaseSingleMetric\n    return isinstance(m, BaseSingleMetric) and type(m).add is not BaseSingleMetric.add","tryCatchPattern":null,"preventionTips":["Use SingleMetric.add(other, fill_value=0.0) when stock universes differ","Prefer .add(fill_value=...) over + when accumulating metrics across days with changing coverage","Implement add() in subclasses: fillna both sides before element-wise addition"],"tags":["python","qlib","abstract-method","nan","aggregation","metrics"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}