pandas-dev/pandas · error · TypeError

Converting from to is not supported. Do…

Error message

Converting from {self.dtype} to {dtype} is not supported. Do obj.astype('int64').astype(dtype) instead

What it means

Raised in DatetimeLikeArray.astype when converting to an integer dtype other than int64. Datetimelike values are stored as int64 nanosecond (or unit-specific) ticks; converting to int32/int16/int8 would silently overflow or lose precision for typical timestamps. pandas deliberately refuses and tells the user to round-trip through int64 explicitly so the truncation is intentional.

Solutions

  1. Two-step cast as the message suggests: obj.astype('int64').astype('int32').
  2. For epoch seconds: (dti.view('int64') // 10**9).astype('int32') — explicit truncation.
  3. Use int64 if precision/range matters; int32 overflows for dates beyond 2038-01-19.
  4. Consider .view(np.int32) on the underlying array if you genuinely want the raw 32-bit reinterpretation (semantics differ).

Example fix

# before
dti.astype('int32')  # TypeError

# after
dti.astype('int64').astype('int32')
Defensive patterns

Strategy: validation

Validate before calling

target = np.int32
out = dti.astype('int64')
if target != np.int64:
    out = out.astype(target)

Type guard

def needs_two_step_int_cast(target) -> bool:
    import numpy as np
    return np.issubdtype(target, np.integer) and target != np.int64

Try / catch

try:
    dti.astype('int32')
except TypeError as e:
    if 'not supported' in str(e):
        dti.astype('int64').astype('int32')
    else:
        raise

Prevention

When it happens

Trigger: dti.astype('int32'), tdi.astype(np.int16'), period_index.astype('int8'). df['ts'].astype('int32'). Converting a datetime column for a fixed-width binary format that expects int32.

Common situations: User wants compact storage of epoch seconds in int32 and calls astype directly. Interop with systems expecting 32-bit Unix timestamps. Building features for ML with reduced precision integers.

Related errors


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

Appendix: source

Thrown at pandas/core/arrays/datetimelike.py:449

            return self._box_values(self.asi8.ravel()).reshape(self.shape)

        elif is_string_dtype(dtype):
            if isinstance(dtype, ExtensionDtype):
                arr_object = self._format_native_types(na_rep=dtype.na_value)  # type: ignore[arg-type]
                cls = dtype.construct_array_type()
                return cls._from_sequence(arr_object, dtype=dtype, copy=False)
            else:
                return self._format_native_types()

        elif isinstance(dtype, ExtensionDtype):
            return super().astype(dtype, copy=copy)
        elif dtype.kind in "iu":
            # we deliberately ignore int32 vs. int64 here.
            # See https://github.com/pandas-dev/pandas/issues/24381 for more.
            values = self.asi8
            if dtype != np.int64:
                raise TypeError(
                    f"Converting from {self.dtype} to {dtype} is not supported. "
                    "Do obj.astype('int64').astype(dtype) instead"
                )

            if copy:
                values = values.copy()
            return values
        elif (dtype.kind in "mM" and self.dtype != dtype) or dtype.kind == "f":
            # disallow conversion between datetime/timedelta,
            # and conversions for any datetimelike to float
            msg = f"Cannot cast {type(self).__name__} to dtype {dtype}"
            raise TypeError(msg)
        else:
            return np.asarray(self, dtype=dtype)

    @overload  # type: ignore[override]
    def view(self) -> Self: ...

View on GitHub (pinned to 3b7651241d)