pandas-dev/pandas · error · TypeError

Can only string multiply by an integer.

Error message

Can only string multiply by an integer.

What it means

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.

Solutions

  1. Ensure the multiplier is integer dtype: cast with .astype('int64[pyarrow]').
  2. Use a Python int scalar for fixed repetition: s * 3.
  3. Round then cast floats: s * df['n'].round().astype('int64[pyarrow]').
  4. Avoid multiplying two string columns; that is not a defined operation.

Example fix

# before
s = pd.Series(['ab'], dtype="string[pyarrow]")
n = pd.array([1.5], dtype="float64[pyarrow]")
s * n  # raises TypeError: Can only string multiply by an integer.

# after
s * n.astype('int64[pyarrow]')
Defensive patterns

Strategy: type-guard

Validate before calling

import pyarrow as pa

def safe_string_mul(arr, multiplier):
    if not pa.types.is_integer(multiplier._pa_array.type if hasattr(multiplier, '_pa_array') else pa.array(multiplier).type):
        multiplier = multiplier.astype('int64[pyarrow]') if hasattr(multiplier, 'astype') else int(multiplier)
    return arr * multiplier

Type guard

import pyarrow as pa

def is_integer_pa(arr) -> bool:
    return hasattr(arr, '_pa_array') and pa.types.is_integer(arr._pa_array.type)

Try / catch

try:
    result = s * n
except TypeError as e:
    if 'Can only string multiply by an integer' in str(e):
        result = s * n.astype('int64[pyarrow]')
    else:
        raise

Prevention

When it happens

Trigger: string_arr * 1.5; string_arr * other_string_arr; string_arr * decimal_array; s * s (string times string). All with pyarrow-backed string arrays.

Common situations: 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.

Related errors


AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11). Data as JSON: /api/errors/91d1366e763fdda9. Report an issue: GitHub.

Appendix: source

Thrown at pandas/core/arrays/arrow/array.py:1219

                sep = pa.scalar("", type=self_array.type)
                if isinstance(other, pa.Scalar) and pc.is_null(other).as_py():
                    other = other.cast(self_array.type)
                try:
                    if op is operator.add:
                        result = pc.binary_join_element_wise(self_array, other, sep)
                    elif op is roperator.radd:
                        result = pc.binary_join_element_wise(other, self_array, sep)
                except pa.ArrowNotImplementedError as err:
                    raise TypeError(
                        self._op_method_error_message(other_original, op)
                    ) from err
                return self._from_pyarrow_array(result)
            elif op in [operator.mul, roperator.rmul]:
                binary = self._pa_array
                integral = other
                if not pa.types.is_integer(integral.type):
                    raise TypeError("Can only string multiply by an integer.")
                pa_integral = pc.if_else(pc.less(integral, 0), 0, integral)
                result = pc.binary_repeat(binary, pa_integral)
                return self._from_pyarrow_array(result)
        elif (
            pa.types.is_string(other.type)
            or pa.types.is_binary(other.type)
            or pa.types.is_large_string(other.type)
        ) and op in [operator.mul, roperator.rmul]:
            binary = other
            integral = self._pa_array
            if not pa.types.is_integer(integral.type):
                raise TypeError("Can only string multiply by an integer.")
            pa_integral = pc.if_else(pc.less(integral, 0), 0, integral)
            result = pc.binary_repeat(binary, pa_integral)
            return self._from_pyarrow_array(result)
        if (
            isinstance(other, pa.Scalar)
            and pc.is_null(other).as_py()

View on GitHub (pinned to 3b7651241d)