{"record":{"id":"35c3d998a403e08a","repo":"microsoft/qlib","slug":"the-indexes-of-self-and-other-do-not-meet-the-requ","errorCode":null,"errorMessage":"The indexes of self and other do not meet the requirements of the four arithmetic operations","messagePattern":"The indexes of self and other do not meet the requirements of the four arithmetic operations","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"qlib/utils/index_data.py","lineNumber":565,"sourceCode":"            if len(data) > 0:\n                index, data = zip(*data.items())\n            else:\n                index, data = [], []\n        elif isinstance(data, pd.Series):\n            assert len(index) == 0\n            index, data = data.index, data.values\n        elif isinstance(data, (int, float, np.number)):\n            data = [data]\n        super().__init__(data, index)\n        assert self.ndim == 1\n\n    def _align_indices(self, other):\n        if self.index == other.index:\n            return other\n        elif set(self.index) == set(other.index):\n            return other.reindex(self.index)\n        else:\n            raise ValueError(\n                f\"The indexes of self and other do not meet the requirements of the four arithmetic operations\"\n            )\n\n    def reindex(self, index: Index, fill_value=np.nan) -> SingleData:\n        \"\"\"reindex data and fill the missing value with np.nan.\n\n        Parameters\n        ----------\n        new_index : list\n            new index\n        fill_value:\n            what value to fill if index is missing\n\n        Returns\n        -------\n        SingleData\n            reindex data\n        \"\"\"","sourceCodeStart":547,"sourceCodeEnd":583,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/utils/index_data.py#L547-L583","documentation":"Before arithmetic on two SingleData objects, qlib aligns their indexes: identical (or same-set, different-order) indexes are fine — the latter triggers an implicit other.reindex(self.index) — but if the index SETS differ at all, alignment is impossible and ValueError is raised. It is qlib's strict version of pandas' automatic union alignment.","triggerScenarios":"a + b where SingleData a and b have any non-shared index element, e.g. a covers dates 2020-01-01..2020-01-10 and b covers 2020-01-05..2020-01-15; also arithmetic against another SingleData built from a different instrument/calendar.","commonSituations":"Arithmetic between series fetched from different data handlers, calendars (QLIB vs custom exchange) or instruments; operations on data spanning different date ranges after filtering; forgetting that qlib (unlike pandas) does not align on index union with NaN fill.","solutions":["Explicitly align first: b = b.reindex(a.index) (missing entries become NaN), then a + b.","Intersect the indexes before operating: common = a.index & b.index; a = a.fetch(common); b = b.fetch(common).","When one operand is a scalar, pass the plain number (int/float) instead of wrapping it in a SingleData with an unrelated index."],"exampleFix":"// before\nc = a + b  # ValueError: different index sets\n\n// after\nb = b.reindex(a.index)  # fills missing dates with NaN\nc = a + b","handlingStrategy":"validation","validationCode":"if set(a.index.idx_list) != set(b.index.idx_list):\n    b = b.reindex(a.index)  # explicit align, NaN-filled\nc = a + b","typeGuard":"def indexes_compatible(a, b) -> bool:\n    return a.index == b.index or set(a.index.idx_list) == set(b.index.idx_list)","tryCatchPattern":"try:\n    c = a + b\nexcept ValueError:\n    b = b.reindex(a.index)\n    c = a + b","preventionTips":["Reindex operands to a common index before any SingleData arithmetic — qlib does not union-align like pandas.","Pass plain scalars (not SingleData-wrapped) for scalar arithmetic."],"tags":["qlib","index-data","alignment","arithmetic","valueerror"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}