{"record":{"id":"e5a114e783aef1cd","repo":"pola-rs/polars","slug":"only-datetime-duration-inputs-supported-got-dtyp","errorCode":null,"errorMessage":"only Datetime/Duration inputs supported, got {dtype}","messagePattern":"only Datetime/Duration inputs supported, got (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-python/src/interop/numpy/utils.rs","lineNumber":127,"sourceCode":"    use numpy::datetime::{Datetime, Timedelta, units};\n    match dtype {\n        DataType::Datetime(TimeUnit::Milliseconds, _) => {\n            Datetime::<units::Milliseconds>::get_dtype(py)\n        },\n        DataType::Datetime(TimeUnit::Microseconds, _) => {\n            Datetime::<units::Microseconds>::get_dtype(py)\n        },\n        DataType::Datetime(TimeUnit::Nanoseconds, _) => {\n            Datetime::<units::Nanoseconds>::get_dtype(py)\n        },\n        DataType::Duration(TimeUnit::Milliseconds) => {\n            Timedelta::<units::Milliseconds>::get_dtype(py)\n        },\n        DataType::Duration(TimeUnit::Microseconds) => {\n            Timedelta::<units::Microseconds>::get_dtype(py)\n        },\n        DataType::Duration(TimeUnit::Nanoseconds) => Timedelta::<units::Nanoseconds>::get_dtype(py),\n        _ => panic!(\"only Datetime/Duration inputs supported, got {dtype}\"),\n    }\n}\n","sourceCodeStart":109,"sourceCodeEnd":130,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-python/src/interop/numpy/utils.rs#L109-L130","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Upgrade polars to the latest version where Date/Time routing is fixed.","Cast Date to Datetime before conversion: df.with_columns(pl.col('d').cast(pl.Datetime)).","Exclude non-supported temporal columns from the conversion and convert them separately via to_list()."],"exampleFix":"# before\narr = df.select(pl.col('date_col')).to_numpy()  # panics for Date in some versions\n# after\narr = df.select(pl.col('date_col').cast(pl.Datetime('us'))).to_numpy()","handlingStrategy":"validation","validationCode":"assert all(isinstance(dt, (pl.Datetime, pl.Duration)) for dt in df.schema.values()), \\\n    'temporal to_numpy path requires Datetime/Duration columns'","typeGuard":"def is_temporal(dtype):\n    return isinstance(dtype, (pl.Datetime, pl.Duration))","tryCatchPattern":"try:\n    arr = df.to_numpy()\nexcept BaseException as e:\n    if 'only Datetime/Duration' in str(e):\n        arr = df.with_columns(pl.all().cast(pl.Datetime('us'), strict=False)).to_numpy()\n    else:\n        raise","preventionTips":["Cast Date columns to Datetime before numeric/temporal numpy conversion.","Inspect df.schema to confirm dtypes before zero-copy conversion.","Keep polars updated for improved temporal dtype routing."],"tags":["numpy","datetime","dtype","panic","interop"],"backgroundTag":"unsupported-dtype","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}