pandas-dev/pandas · error · TypeError

Cannot multiply with {type(other).__name__}

Error message

Cannot multiply with {type(other).__name__}

What it means

Raised by TimedeltaArray.__mul__ when 'other' is a scalar that numpy accepted but produced a non-timedelta result dtype (the multiply did not yield timedelta64[ns]). This is the fallback TypeError after the int and float scalar branches are exhausted, guarding against nonsensical scalar multipliers. It exists because numpy >= 2.1 stopped raising TypeError in some cases and instead dispatched to other.__rmul__, so pandas re-asserts the result must stay timedelta-typed.

Source

Thrown at pandas/core/arrays/timedeltas.py:536

                    # The extreme elements bound all products, so checking them
                    #  with exact Python-int arithmetic lets the common
                    #  no-overflow case use a vectorized multiply. NaT
                    #  (int64.min) always trips the bound, falling through to
                    #  the NaT-aware cython loop.
                    low_prod = int(i8_vals.min()) * other
                    high_prod = int(i8_vals.max()) * other
                    if max(abs(low_prod), abs(high_prod)) <= lib.i8max:
                        result = (i8_vals * other).view(self._ndarray.dtype)
                        return type(self)._simple_new(result, dtype=result.dtype)
                return self._mul_int_overflowsafe(np.asarray(other, dtype="i8"))
            if lib.is_float(other):
                return self._mul_float_overflowsafe(other)
            # numpy will raise TypeError for non-numeric scalar
            result = self._ndarray * other
            if result.dtype.kind != "m":
                # numpy >= 2.1 may not raise a TypeError
                # and seems to dispatch to others.__rmul__?
                raise TypeError(f"Cannot multiply with {type(other).__name__}")
            return type(self)._simple_new(result, dtype=result.dtype)

        if not hasattr(other, "dtype"):
            # list, tuple
            other = np.array(other)

        if other.dtype.kind == "b":
            # GH#58054
            raise TypeError(
                f"Cannot multiply '{self.dtype}' by bool, explicitly cast to "
                "integers instead"
            )
        if isinstance(other.dtype, (ArrowDtype, BaseMaskedDtype)):
            # GH#58054
            return NotImplemented

        if len(other) != len(self) and not lib.is_np_dtype(other.dtype, "m"):
            # Exclude timedelta64 here so we correctly raise TypeError

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Inspect type(other); only multiply timedeltas by int or float scalars.
  2. If you intended scaling time, convert other to int/float first (e.g. float(other)).
  3. If other is actually a timedelta and you wanted a ratio, swap to division (timedelta / timedelta).
  4. If other is a datetime, rethink the operation: you likely want addition, not multiplication.

Example fix

// before
import pandas as pd
td = pd.to_timedelta(['1d','2d'])
out = td * pd.Timestamp('2020-01-01')

// after
out = td * 2  # scale by integer days
Defensive patterns

Strategy: type-guard

Validate before calling

import numbers
if not isinstance(other, (numbers.Integral, numbers.Real, pd.Timedelta)):
    raise TypeError(f'unsupported multiplier type: {type(other).__name__}')

Type guard

def is_supported_td_multiplier(x) -> bool:
    import numbers
    return isinstance(x, (numbers.Integral, numbers.Real))

Try / catch

try:
    out = td * other
except TypeError as e:
    if 'Cannot multiply with' in str(e):
        raise TypeError(f'cast {type(other).__name__} to int/float first') from e
    raise

Prevention

When it happens

Trigger: Multiplying a Timedelta/Index of dtype timedelta64[ns] by an unsupported scalar type, e.g. `pd.Timedelta('1d') * pd.Timestamp('2020-01-01')` or a timedelta array times a string/decimal/object scalar. Hit only when other is a scalar that is neither Python int/float nor a recognized timedelta scalar, and numpy's `self._ndarray * other` returns a non-'m' dtype.

Common situations: Mixing timedelta with datetime objects, strings, Decimal, or custom numeric-like objects in vectorized ops; refactors that pass through untyped user input to arithmetic; version upgrades to numpy >= 2.1 where dispatch behavior changed.

Related errors


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