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 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.

Source

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

                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 71959b8cb9)

Solutions

  1. Cast the multiplier to integer: s * n.astype('int64[pyarrow]').
  2. Use an integer scalar: s * 3.
  3. If repeat count is float, round/truncate first: s * n.round().astype('int64[pyarrow]').
  4. Validate the multiplier is integral before the op.

Example fix

# before
out = prefixes * (widths / 2)   # float -> TypeError
# after
out = prefixes * (widths // 2).astype('int64[pyarrow]')
Defensive patterns

Strategy: validation

Validate before calling

def repeat_strings(arr, n):
    import numbers
    if hasattr(n, 'astype'):
        n = n.astype('int64[pyarrow]')
    elif not isinstance(n, numbers.Integral):
        raise TypeError(f'string repeat requires integer, got {type(n)}')
    return arr * n

out = repeat_strings(prefixes, counts)

Type guard

import numbers
import numpy as np

def is_integer_repeat_count(n) -> bool:
    if isinstance(n, numbers.Integral):
        return True
    if isinstance(n, np.ndarray):
        return n.dtype.kind in 'iu'
    if hasattr(n, 'dtype'):
        return n.dtype.kind in 'iu'
    return False

Try / catch

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

Prevention

When it happens

Trigger: `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.

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

Related errors


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