{"record":{"id":"7cc3da00a1d96f78","repo":"pandas-dev/pandas","slug":"cannot-multiply-with-unequal-lengths","errorCode":null,"errorMessage":"Cannot multiply with unequal lengths","messagePattern":"Cannot multiply with unequal lengths","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/timedeltas.py","lineNumber":575,"sourceCode":"\n        if not hasattr(other, \"dtype\"):\n            # list, tuple\n            other = np.array(other)\n\n        if other.dtype.kind == \"b\":\n            # GH#58054\n            raise TypeError(\n                f\"Cannot multiply '{self.dtype}' by bool, explicitly cast to \"\n                \"integers instead\"\n            )\n        if isinstance(other.dtype, (ArrowDtype, BaseMaskedDtype)):\n            # GH#58054\n            return NotImplemented\n\n        if len(other) != len(self) and not lib.is_np_dtype(other.dtype, \"m\"):\n            # Exclude timedelta64 here so we correctly raise TypeError\n            #  for that instead of ValueError\n            raise ValueError(\"Cannot multiply with unequal lengths\")\n\n        if is_object_dtype(other.dtype):\n            # this multiplication will succeed only if all elements of other\n            #  are int or float scalars, so we will end up with\n            #  timedelta64[ns]-dtyped result\n            arr = self._ndarray\n            obj_result = np.array([arr[n] * other[n] for n in range(len(self))])\n            return type(self)._simple_new(obj_result, dtype=obj_result.dtype)\n\n        if other.dtype.kind in \"iu\":\n            # GH#43178: detect int64 overflow rather than silently wrapping.\n            #  Cast to int64 first: an unsigned multiplier above int64.max wraps\n            #  to negative, which we detect by sign. We check the sign rather\n            #  than ``other > i8max`` because comparing a broadcast unsigned\n            #  array to a Python int segfaults on numpy < 2.2 (hit via the\n            #  DataFrame blockwise path).\n            i8_other = other.astype(\"i8\", copy=False)\n            if other.dtype.kind == \"u\" and (i8_other < 0).any():","sourceCodeStart":557,"sourceCodeEnd":593,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/timedeltas.py#L557-L593","documentation":"Raised in TimedeltaArray.__mul__ when the array operand's length differs from the timedelta array's length, except when the operand is itself timedelta64 (which is dispatched to a type-error path instead). This is the standard broadcast-length guard for elementwise multiplication.","triggerScenarios":"`pd.to_timedelta([1, 2, 3], unit='D') * np.array([1, 2])`, or multiplying two Series of different lengths without alignment.","commonSituations":"Forgetting that raw numpy arrays/lists do not align by index; mismatched filter or weight arrays.","solutions":["Ensure both operands have equal length.","Use pandas Series/Index objects so index alignment applies automatically, then drop or fill NaNs as needed.","Broadcast a scalar instead of a length-mismatched array if a uniform scale 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 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        # align via Series or trim to common length\n        raise\n    raise","preventionTips":["Use pandas Series for automatic index alignment, or assert equal lengths up front.","Broadcast scalars when uniform scaling is intended."],"tags":["timedelta","multiplication","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"}