{"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":1219,"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":1201,"sourceCodeEnd":1237,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/arrow/array.py#L1201-L1237","documentation":"Raised by _evaluate_op_method when the self array is string/binary/large_string type, the operator is mul/rmul, and the OTHER operand (the multiplier) is a pyarrow array whose type is not integer. String repetition via pc.binary_repeat requires an integer count; a non-integer multiplier (float, string, decimal) is rejected.","triggerScenarios":"string_arr * 1.5; string_arr * other_string_arr; string_arr * decimal_array; s * s (string times string). All with pyarrow-backed string arrays.","commonSituations":"User multiplies a string column by a float column expecting broadcasting; pandas object dtype used to coerce via __rmul__ but pyarrow path is strict; decimal/decimal128 multiplication where one side is string.","solutions":["Ensure the multiplier is integer dtype: cast with .astype('int64[pyarrow]').","Use a Python int scalar for fixed repetition: s * 3.","Round then cast floats: s * df['n'].round().astype('int64[pyarrow]').","Avoid multiplying two string columns; that is not a defined operation."],"exampleFix":"# before\ns = pd.Series(['ab'], dtype=\"string[pyarrow]\")\nn = pd.array([1.5], dtype=\"float64[pyarrow]\")\ns * n  # raises TypeError: Can only string multiply by an integer.\n\n# after\ns * n.astype('int64[pyarrow]')","handlingStrategy":"type-guard","validationCode":"import pyarrow as pa\n\ndef safe_string_mul(arr, multiplier):\n    if not pa.types.is_integer(multiplier._pa_array.type if hasattr(multiplier, '_pa_array') else pa.array(multiplier).type):\n        multiplier = multiplier.astype('int64[pyarrow]') if hasattr(multiplier, 'astype') else int(multiplier)\n    return arr * multiplier","typeGuard":"import pyarrow as pa\n\ndef is_integer_pa(arr) -> bool:\n    return hasattr(arr, '_pa_array') and pa.types.is_integer(arr._pa_array.type)","tryCatchPattern":"try:\n    result = s * n\nexcept TypeError as e:\n    if 'Can only string multiply by an integer' in str(e):\n        result = s * n.astype('int64[pyarrow]')\n    else:\n        raise","preventionTips":["Ensure multiplier columns are integer pyarrow dtype before string multiplication.","Use scalar ints for fixed repetitions.","Round and cast float multipliers explicitly."],"tags":["arithmetic","string","multiply","type-validation","pyarrow"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}