{"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":680,"sourceCode":"            if op in [roperator.rtruediv, roperator.rfloordiv]:\n                raise TypeError(\n                    f\"Cannot divide {type(other).__name__} by {type(self).__name__}\"\n                )\n\n            other = _exact_if_integral(other)\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":662,"sourceCodeEnd":698,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/timedeltas.py#L662-L698","documentation":"Raised in TimedeltaArray._cast_divlike_op when the array operand's length differs from the timedelta array's length. This guard runs after list/tuple operands are converted to arrays and before any division dispatch, ensuring elementwise division operands are aligned in length.","triggerScenarios":"`pd.to_timedelta([1, 2, 3], unit='D') / np.array([1, 2])`, or dividing two unequal-length arrays/lists.","commonSituations":"Using raw numpy arrays/lists that bypass index alignment; mismatched divisor vectors.","solutions":["Ensure both operands have equal length.","Use pandas Series objects so index alignment applies, then handle resulting NaN as needed.","Broadcast a scalar divisor if uniform scaling was intended."],"exampleFix":"# before\npd.to_timedelta([1, 2, 3], unit='D') / np.array([1, 2])  # ValueError\n\n# after\npd.to_timedelta([1, 2, 3], unit='D') / np.array([1, 2, 3])","handlingStrategy":"validation","validationCode":"def div_lengths_match(a, b) -> bool:\n    return len(a) == len(b)","typeGuard":"null","tryCatchPattern":"try:\n    result = td / other\nexcept ValueError as e:\n    if 'unequal lengths' in str(e):\n        raise\n    raise","preventionTips":["Assert equal lengths up front, or use pandas Series for index alignment.","Broadcast a scalar divisor when uniform scaling is intended."],"tags":["timedelta","division","length-mismatch","valueerror"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}