{"record":{"id":"6ea6d5715cb462e5","repo":"microsoft/qlib","slug":"please-implement-align-indices-func","errorCode":null,"errorMessage":"please implement _align_indices func","messagePattern":"please implement _align_indices func","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"qlib/utils/index_data.py","lineNumber":435,"sourceCode":"        # NOTE: this tries to behave like a numpy array to be compatible with numpy aggregating function like nansum and nanmean\n        return self.iloc[args]\n\n    def _align_indices(self, other: \"IndexData\") -> \"IndexData\":\n        \"\"\"\n        Align all indices of `other` to `self` before performing the arithmetic operations.\n        This function will return a new IndexData rather than changing data in `other` inplace\n\n        Parameters\n        ----------\n        other : \"IndexData\"\n            the index in `other` is to be changed\n\n        Returns\n        -------\n        IndexData:\n            the data in `other` with index aligned to `self`\n        \"\"\"\n        raise NotImplementedError(f\"please implement _align_indices func\")\n\n    def sort_index(self, axis=0, inplace=True):\n        assert inplace, \"Only support sorting inplace now\"\n        self.indices[axis], sorted_idx = self.indices[axis].sort()\n        self.data = np.take(self.data, sorted_idx, axis=axis)\n\n    # The code below could be simpler like methods in __getattribute__\n    def __invert__(self):\n        return self.__class__(~self.data.astype(bool), *self.indices)\n\n    def abs(self):\n        \"\"\"get the abs of data except np.nan.\"\"\"\n        tmp_data = np.absolute(self.data)\n        return self.__class__(tmp_data, *self.indices)\n\n    def replace(self, to_replace: Dict[np.number, np.number]):\n        assert isinstance(to_replace, dict)\n        tmp_data = self.data.copy()","sourceCodeStart":417,"sourceCodeEnd":453,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/utils/index_data.py#L417-L453","documentation":"IndexData is a base class; binary operations (+, -, *, / via BinaryOps) call _align_indices to bring the other operand's index to match self. The base implementation is deliberately a NotImplementedError stub — only SingleData and MultiData provide real alignment logic. Hitting it means an IndexData subclass (or the base class itself) was used in arithmetic without an alignment strategy.","triggerScenarios":"Instantiating IndexData directly or subclassing it (e.g. for a custom 1-D/2-D variant) and then applying +, -, *, / between two instances; also any code path that calls obj._align_indices(other) on a bare IndexData.","commonSituations":"Extending qlib's index-data containers for a new data shape; importing IndexData instead of SingleData/MultiData by mistake; upgrading qlib versions where the alignment contract moved into subclass methods.","solutions":["Use SingleData (1-D) or MultiData (2-D) instead of the raw IndexData base class.","If you subclass IndexData, implement _align_indices(self, other) -> IndexData that reindexes `other` onto self.indices and returns it.","Alternatively pre-align manually with reindex()/reindexall() before doing arithmetic, which bypasses _align_indices."],"exampleFix":"// before\nc = IndexData(a.data, *a.indices) + b  # NotImplementedError\n\n// after\nc = SingleData(a.data, a.index) + b.reindex(a.index)  # concrete classes align fine","handlingStrategy":"type-guard","validationCode":"from qlib.utils.index_data import SingleData, MultiData, IndexData\nassert type(a) is not IndexData and type(b) is not IndexData, 'use SingleData/MultiData for arithmetic'","typeGuard":"def supports_alignment(obj) -> bool:\n    return type(obj).__name__ in ('SingleData', 'MultiData') or type(obj)._align_indices is not IndexData._align_indices","tryCatchPattern":"try:\n    c = a + b\nexcept NotImplementedError:\n    b = b.reindex(a.index)\n    c = SingleData(a.data, a.index) + b","preventionTips":["Never instantiate the IndexData base class for values used in arithmetic.","When subclassing IndexData, implement _align_indices (and reindex) before writing any binary-op tests."],"tags":["qlib","index-data","notimplementederror","abstract-method","arithmetic"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}