pandas-dev/pandas · error · ValueError

Cannot convert from {self.dtype} to {dtype}. Supported resol

Error message

Cannot convert from {self.dtype} to {dtype}. Supported resolutions are 's', 'ms', 'us', 'ns'

What it means

Raised by TimedeltaArray.astype when the target timedelta64 dtype is not in the supported resolution set (s/ms/us/ns). pandas only supports those four resolutions for timedelta64 because the conversion is overflow-safe; finer (e.g. timedelta64[ps],[fs],[as]) or unusual units cannot be represented without silent overflow, so a ValueError is raised naming the current and target dtypes.

Source

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

    def astype(self, dtype, copy: bool = True):
        # We handle
        #   --> timedelta64[ns]
        #   --> timedelta64
        # DatetimeLikeArrayMixin super call handles other cases
        dtype = pandas_dtype(dtype)

        if lib.is_np_dtype(dtype, "m"):
            if dtype == self.dtype:
                if copy:
                    return self.copy()
                return self

            if is_supported_dtype(dtype):
                # unit conversion e.g. timedelta64[s]
                res_values = astype_overflowsafe(self._ndarray, dtype, copy=False)
                return type(self)._simple_new(res_values, dtype=res_values.dtype)
            else:
                raise ValueError(
                    f"Cannot convert from {self.dtype} to {dtype}. "
                    "Supported resolutions are 's', 'ms', 'us', 'ns'"
                )

        return dtl.DatetimeLikeArrayMixin.astype(self, dtype, copy=copy)

    def _iter_convert_chunk(self, data: np.ndarray) -> np.ndarray:
        return ints_to_pytimedelta(data, box=True)

    # ----------------------------------------------------------------
    # Reductions

    def sum(
        self,
        *,
        axis: AxisInt | None = None,
        dtype: NpDtype | None = None,
        out=None,

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Cast to a supported resolution first: `td_arr.astype('timedelta64[s]')`, then handle further unit conversion in Python if needed.
  2. For day/hour granularity, compute via `.dt.days` / `.dt.components` rather than astype.
  3. If you need a non-supported numpy unit, go through int64: `td_arr.asi8.astype(...)` with explicit unit math.

Example fix

# before
arr.astype('timedelta64[D]')  # ValueError
# after
arr.astype('timedelta64[s]')  # then .dt.days for day values
Defensive patterns

Strategy: validation

Validate before calling

SUPPORTED = {'s','ms','us','ns'}

def safe_td_astype(arr, target):
    import re
    m = re.match(r'timedelta64\[(\w+)\]', target)
    if m and m.group(1) not in SUPPORTED:
        raise ValueError(f'{target} unsupported; use one of {SUPPORTED}')
    return arr.astype(target)

Type guard

import re
SUPPORTED = {'s','ms','us','ns'}
def is_supported_td_dtype(dtype_str) -> bool:
    m = re.match(r'timedelta64\[(\w+)\]', dtype_str)
    return bool(m) and m.group(1) in SUPPORTED

Try / catch

try:
    return arr.astype(target)
except ValueError as e:
    if 'Cannot convert from' in str(e) and 'Supported resolutions' in str(e):
        return arr.astype('timedelta64[s]')
    raise

Prevention

When it happens

Trigger: Calling `td_arr.astype('timedelta64[ms]')` works, but `td_arr.astype('timedelta64[D]')`, `astype('timedelta64[h]')`, or `astype('timedelta64[ps]')` raises because those are not in is_supported_dtype. The else-branch at timedeltas.py:365 fires.

Common situations: Interop with systems expecting day/hour-resolution arrays; copying dtype strings from numpy docs that list units pandas does not support; converting large-nanosecond values to coarser-than-supported units.

Related errors


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