{"record":{"id":"e754e02723bb95fb","repo":"pandas-dev/pandas","slug":"cannot-add-indices-of-unequal-length","errorCode":null,"errorMessage":"cannot add indices of unequal length","messagePattern":"cannot add indices of unequal length","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/datetimelike.py","lineNumber":1112,"sourceCode":"        self = cast(\"DatetimeArray\", self)\n        # subtract a datetime from myself, yielding an ndarray[timedelta64[ns]]\n\n        if isna(other):\n            # i.e. np.datetime64(\"NaT\")\n            return self - NaT\n\n        ts = Timestamp(other)\n\n        self, ts = self._ensure_matching_resos(ts)\n        return self._sub_datetimelike(ts)\n\n    @final\n    def _sub_datetime_arraylike(self, other: DatetimeArray) -> TimedeltaArray:\n        if self.dtype.kind != \"M\":\n            raise TypeError(f\"cannot subtract a datelike from a {type(self).__name__}\")\n\n        if len(self) != len(other):\n            raise ValueError(\"cannot add indices of unequal length\")\n\n        self = cast(\"DatetimeArray\", self)\n\n        self, other = self._ensure_matching_resos(other)\n        return self._sub_datetimelike(other)\n\n    @final\n    def _sub_datetimelike(self, other: Timestamp | DatetimeArray) -> TimedeltaArray:\n        self = cast(\"DatetimeArray\", self)\n\n        from pandas.core.arrays import TimedeltaArray\n\n        try:\n            self._assert_tzawareness_compat(other)\n        except TypeError as err:\n            new_message = str(err).replace(\"compare\", \"subtract\")\n            raise type(err)(new_message) from err\n","sourceCodeStart":1094,"sourceCodeEnd":1130,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/datetimelike.py#L1094-L1130","documentation":"Raised by _sub_datetime_arraylike when len(self) != len(other). DatetimeArray subtraction is elementwise (no broadcasting between two arrays), so unequal lengths are rejected with ValueError. Despite the word 'add' in the message, this guards a subtraction.","triggerScenarios":"Subtracting two DatetimeIndex/DatetimeArray objects of different lengths: idx_a - idx_b where len(idx_a) != len(idx_b). Reached via __sub__ line 1412 then _sub_datetime_arraylike line 1111.","commonSituations":"Joining or aligning series whose indexes disagree; slicing one series but not the other before a datetime subtraction; misconfigured fixtures in tests.","solutions":["Align both arrays to a common index first: idx_a, idx_b = idx_a.align(idx_b) (alignment broadcasts NaN/NaT as needed).","Reindex one operand to the other's index: idx_b = idx_b.reindex(idx_a.index).","Slice the longer operand so lengths match, or broadcast a scalar instead of an array.","Confirm len(a) == len(b) with an assertion before the arithmetic."],"exampleFix":"// before\nout = datetime_idx_a - datetime_idx_b  # ValueError when lengths differ\n// after\na, b = datetime_idx_a.align(datetime_idx_b)\nout = a - b","handlingStrategy":"validation","validationCode":"if len(a) != len(b):\n    a, b = a.align(b)\nassert len(a) == len(b)\nout = a - b","typeGuard":null,"tryCatchPattern":"try:\n    out = a - b\nexcept ValueError as e:\n    if 'unequal length' in str(e):\n        a, b = a.align(b)\n        out = a - b\n    else:\n        raise","preventionTips":["Align DatetimeIndex operands before subtraction.","Assert equal lengths before elementwise datetime arithmetic.","Use reindex to enforce a common index."],"tags":["length-mismatch","subtraction","alignment","datetime-array"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}