{"record":{"id":"2346f26683cd4119","repo":"pandas-dev/pandas","slug":"cannot-divide-vectors-with-unequal-lengths","errorCode":null,"errorMessage":"Cannot divide vectors with unequal lengths","messagePattern":"Cannot divide vectors with unequal lengths","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/timedeltas.py","lineNumber":660,"sourceCode":"\n            if op in [roperator.rtruediv, roperator.rfloordiv]:\n                raise TypeError(\n                    f\"Cannot divide {type(other).__name__} by {type(self).__name__}\"\n                )\n\n            if lib.is_float(other):\n                # GH#43178: raise instead of silently saturating on overflow\n                self._check_float_div_overflow(other)\n            result = op(self._ndarray, other)\n            return type(self)._simple_new(result, dtype=result.dtype)\n\n    def _cast_divlike_op(self, other):\n        if not hasattr(other, \"dtype\"):\n            # e.g. list, tuple\n            other = np.array(other)\n\n        if len(other) != len(self):\n            raise ValueError(\"Cannot divide vectors with unequal lengths\")\n        return other\n\n    def _vector_divlike_op(self, other, op) -> np.ndarray | Self:\n        \"\"\"\n        Shared logic for __truediv__, __floordiv__, and their reversed versions\n        with timedelta64-dtype ndarray other.\n        \"\"\"\n        other_arr = np.asarray(other)\n        if other_arr.dtype.kind == \"f\" and op in [operator.truediv, operator.floordiv]:\n            # GH#43178: raise instead of silently saturating on overflow\n            self._check_float_div_overflow(other_arr)\n\n        # Let numpy handle it\n        result = op(self._ndarray, other_arr)\n\n        if (is_integer_dtype(other.dtype) or is_float_dtype(other.dtype)) and op in [\n            operator.truediv,\n            operator.floordiv,","sourceCodeStart":642,"sourceCodeEnd":678,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/timedeltas.py#L642-L678","documentation":"Raised by _cast_divlike_op when dividing a timedelta array by another array of differing length. Lengths must match for elementwise division; mismatched vectors are rejected explicitly.","triggerScenarios":"`td_array / np.array([...])` where lengths differ; `td_series / other_series` with different index lengths and no alignment.","commonSituations":"Index misalignment after filter/merge; subsetting one operand but not the other; passing arrays from different sources.","solutions":["Align indices/lengths via reindex before dividing.","Use a scalar divisor if all elements share the same scale.","Verify len(other) == len(self) and document the expectation."],"exampleFix":"// before\nout = td_series / other_series  # mismatched length\n\n// after\nother = other_series.reindex(td_series.index)\nout = td_series / other","handlingStrategy":"validation","validationCode":"assert len(td) == len(other), f'vector length mismatch: {len(td)} vs {len(other)}'","typeGuard":"def div_lengths_match(a, b) -> bool:\n    return len(a) == len(b)","tryCatchPattern":"try:\n    out = td / other\nexcept ValueError as e:\n    if 'unequal lengths' in str(e):\n        other = other.reindex(td.index) if hasattr(other, 'reindex') else other[:len(td)]\n        out = td / other\n    else:\n        raise","preventionTips":["Align operands before dividing.","Use scalar divisors where possible.","Verify lengths at function entry."],"tags":["timedelta","valueerror","shape-mismatch","division"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}