{"record":{"id":"91d1366e763fdda9","repo":"pandas-dev/pandas","slug":"can-only-string-multiply-by-an-integer","errorCode":null,"errorMessage":"Can only string multiply by an integer.","messagePattern":"Can only string multiply by an integer\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/arrow/array.py","lineNumber":1194,"sourceCode":"\n                sep = pa.scalar(\"\", type=self_array.type)\n                if isinstance(other, pa.Scalar) and pc.is_null(other).as_py():\n                    other = other.cast(self_array.type)\n                try:\n                    if op is operator.add:\n                        result = pc.binary_join_element_wise(self_array, other, sep)\n                    elif op is roperator.radd:\n                        result = pc.binary_join_element_wise(other, self_array, sep)\n                except pa.ArrowNotImplementedError as err:\n                    raise TypeError(\n                        self._op_method_error_message(other_original, op)\n                    ) from err\n                return self._from_pyarrow_array(result)\n            elif op in [operator.mul, roperator.rmul]:\n                binary = self._pa_array\n                integral = other\n                if not pa.types.is_integer(integral.type):\n                    raise TypeError(\"Can only string multiply by an integer.\")\n                pa_integral = pc.if_else(pc.less(integral, 0), 0, integral)\n                result = pc.binary_repeat(binary, pa_integral)\n                return self._from_pyarrow_array(result)\n        elif (\n            pa.types.is_string(other.type)\n            or pa.types.is_binary(other.type)\n            or pa.types.is_large_string(other.type)\n        ) and op in [operator.mul, roperator.rmul]:\n            binary = other\n            integral = self._pa_array\n            if not pa.types.is_integer(integral.type):\n                raise TypeError(\"Can only string multiply by an integer.\")\n            pa_integral = pc.if_else(pc.less(integral, 0), 0, integral)\n            result = pc.binary_repeat(binary, pa_integral)\n            return self._from_pyarrow_array(result)\n        if (\n            isinstance(other, pa.Scalar)\n            and pc.is_null(other).as_py()","sourceCodeStart":1176,"sourceCodeEnd":1212,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/arrow/array.py#L1176-L1212","documentation":"Raised by _evaluate_op_method during string multiplication (operator.mul/rmul) when the operand paired with the string array is not an integer type. PyArrow's pc.binary_repeat requires an integer repeat count, so pandas validates the integral side explicitly and raises TypeError with a fixed message before calling pc.","triggerScenarios":"`s * n` where s is string[pyarrow] and n (the other operand) is not an integer pyarrow type — e.g. `s * 2.5`, `s * other_str`, `s * float_column`. The check at line 1193 fires when the boxed `other` type is not pa integer.","commonSituations":"Repeating strings by a float repeat count (e.g. from division), multiplying a string column by another string column, or by a decimal/duration. Migration from object dtype where Python coerced implicitly.","solutions":["Cast the multiplier to integer: s * n.astype('int64[pyarrow]').","Use an integer scalar: s * 3.","If repeat count is float, round/truncate first: s * n.round().astype('int64[pyarrow]').","Validate the multiplier is integral before the op."],"exampleFix":"# before\nout = prefixes * (widths / 2)   # float -> TypeError\n# after\nout = prefixes * (widths // 2).astype('int64[pyarrow]')","handlingStrategy":"validation","validationCode":"def repeat_strings(arr, n):\n    import numbers\n    if hasattr(n, 'astype'):\n        n = n.astype('int64[pyarrow]')\n    elif not isinstance(n, numbers.Integral):\n        raise TypeError(f'string repeat requires integer, got {type(n)}')\n    return arr * n\n\nout = repeat_strings(prefixes, counts)","typeGuard":"import numbers\nimport numpy as np\n\ndef is_integer_repeat_count(n) -> bool:\n    if isinstance(n, numbers.Integral):\n        return True\n    if isinstance(n, np.ndarray):\n        return n.dtype.kind in 'iu'\n    if hasattr(n, 'dtype'):\n        return n.dtype.kind in 'iu'\n    return False","tryCatchPattern":"try:\n    out = s * n\nexcept TypeError as e:\n    if 'Can only string multiply by an integer' in str(e):\n        out = s * n.astype('int64[pyarrow]') if hasattr(n, 'astype') else s * int(n)\n    else:\n        raise","preventionTips":["Cast float repeat counts to int before multiplying strings.","Use integer scalars/columns for string repetition.","Validate multiplier dtype kind is 'i' or 'u'."],"tags":["pyarrow","arithmetic","string-repeat","type-error"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}