pola-rs/polars · error

only Datetime/Duration inputs supported, got

Error message

only Datetime/Duration inputs supported, got {dtype}

What it means

polars_dtype_to_np_temporal_dtype converts polars Datetime/Duration columns to numpy temporal dtypes (datetime64/timedelta64). It panics on any dtype outside Datetime and Duration because its caller contract (temporal_df_to_numpy_view / temporal_series_to_numpy_view) guarantees only temporal input; receiving another dtype means an internal dispatch bug.

Solutions

  1. Upgrade polars to the latest version where Date/Time routing is fixed.
  2. Cast Date to Datetime before conversion: df.with_columns(pl.col('d').cast(pl.Datetime)).
  3. Exclude non-supported temporal columns from the conversion and convert them separately via to_list().

Example fix

# before
arr = df.select(pl.col('date_col')).to_numpy()  # panics for Date in some versions
# after
arr = df.select(pl.col('date_col').cast(pl.Datetime('us'))).to_numpy()
Defensive patterns

Strategy: validation

Validate before calling

assert all(isinstance(dt, (pl.Datetime, pl.Duration)) for dt in df.schema.values()), \
    'temporal to_numpy path requires Datetime/Duration columns'

Type guard

def is_temporal(dtype):
    return isinstance(dtype, (pl.Datetime, pl.Duration))

Try / catch

try:
    arr = df.to_numpy()
except BaseException as e:
    if 'only Datetime/Duration' in str(e):
        arr = df.with_columns(pl.all().cast(pl.Datetime('us'), strict=False)).to_numpy()
    else:
        raise

Prevention

When it happens

Trigger: to_numpy() on temporal-series paths where a non-temporal dtype (e.g. Date, Time, Utf8) reaches the temporal conversion helper, typically due to a version bug or unexpected dtype in a mixed DataFrame conversion.

Common situations: Calling to_numpy() on DataFrames containing Date or Time columns in polars versions where those dtypes are not routed correctly; after upgrading polars and dtype routing changed.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/e5a114e783aef1cd. Report an issue: GitHub.

Appendix: source

Thrown at crates/polars-python/src/interop/numpy/utils.rs:127

    use numpy::datetime::{Datetime, Timedelta, units};
    match dtype {
        DataType::Datetime(TimeUnit::Milliseconds, _) => {
            Datetime::<units::Milliseconds>::get_dtype(py)
        },
        DataType::Datetime(TimeUnit::Microseconds, _) => {
            Datetime::<units::Microseconds>::get_dtype(py)
        },
        DataType::Datetime(TimeUnit::Nanoseconds, _) => {
            Datetime::<units::Nanoseconds>::get_dtype(py)
        },
        DataType::Duration(TimeUnit::Milliseconds) => {
            Timedelta::<units::Milliseconds>::get_dtype(py)
        },
        DataType::Duration(TimeUnit::Microseconds) => {
            Timedelta::<units::Microseconds>::get_dtype(py)
        },
        DataType::Duration(TimeUnit::Nanoseconds) => Timedelta::<units::Nanoseconds>::get_dtype(py),
        _ => panic!("only Datetime/Duration inputs supported, got {dtype}"),
    }
}

View on GitHub (pinned to fe841f959e)