pola-rs/polars · error · ValueError
incorrect NumPy datetime resolution 'D' (datetime only)…
Error message
incorrect NumPy datetime resolution
'D' (datetime only), 'ms', 'us', and 'ns' resolutions are supported when converting from numpy.{datetime64,timedelta64}. Please cast to the closest supported unit before converting. What it means
When converting a numpy datetime64/timedelta64 array to a Series, _normalise_numpy_dtype only supports 'D', 'ms', 'us', and 'ns' resolutions. Any other unit ('Y', 'M', 'W', 'h', 'm', 's', or sub-ns like 'fs', 'as') raises this ValueError because polars' internal time representation cannot hold them losslessly.
Solutions
- Cast the numpy array first: arr.astype('datetime64[us]') (or 'ns'/'ms'/'D').
- For timedelta: arr.astype('timedelta64[us]').
- Convert via Python objects: pl.Series(arr.astype('datetime64[us]')) or pass a list of datetimes.
Example fix
// before
pl.Series(np.array(['2024-01-01'], dtype='datetime64[s]'))
// after
arr = np.array(['2024-01-01'], dtype='datetime64[s]').astype('datetime64[us]')
pl.Series(arr) Defensive patterns
Strategy: validation
Validate before calling
SUPPORTED = {'D', 'ms', 'us', 'ns'}
def ensure_supported_dt64(arr: np.ndarray) -> np.ndarray:
if arr.dtype.kind in 'mM':
unit = arr.dtype.name.split('[')[1].rstrip(']') if '[' in arr.dtype.str else 'us'
if unit not in SUPPORTED:
arr = arr.astype('datetime64[us]' if arr.dtype.kind == 'M' else 'timedelta64[us]')
return arr Try / catch
try:
s = pl.Series(arr)
except ValueError as e:
if "incorrect NumPy datetime resolution" in str(e):
s = pl.Series(arr.astype('datetime64[us]'))
else:
raise Prevention
- Standardize numpy datetime64 arrays to [us] or [ns] at ingestion boundaries
- Check arr.dtype.str before passing datetime64/timedelta64 to polars
- Avoid 's', 'h', 'm' unit arrays from upstream sources; cast immediately after load
When it happens
Trigger: pl.Series(np.array(['2024-01-01'], dtype='datetime64[s]')) or dtype='datetime64[h]', timedelta64 with 's'/'m'/'h' units, or numpy 2.x attosecond/femtosecond dtypes passed to pl.DataFrame/from_numpy/Series.
Common situations: Reading coarse-grained dates stored as datetime64[s] or [h] from other pipelines; pandas-to-polars conversions of second-resolution timestamps; numpy arrays produced with default units like 's'.
Related errors
- cannot convert DataFrame to
- cannot convert List column
- cannot parse numpy data type
- arr.dot query vector must be one-dimensional
- cannot compare datetime.datetime to Series of type
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/f441b7d0ad6d994c.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/datatypes/constructor.py:120
@functools.lru_cache(maxsize=32)
def _normalise_numpy_dtype(dtype: Any) -> tuple[Any, Any]:
normalised_dtype = (
np.dtype(dtype.base.name) if dtype.kind in ("i", "u", "f") else dtype
).type
if normalised_dtype in (np.datetime64, np.timedelta64):
time_unit = np.datetime_data(dtype)[0]
if time_unit in dt.DTYPE_TEMPORAL_UNITS or (
time_unit == "D" and normalised_dtype == np.datetime64
):
return normalised_dtype, np.int64
else:
msg = (
"incorrect NumPy datetime resolution"
"\n\n'D' (datetime only), 'ms', 'us', and 'ns' resolutions are supported when converting from numpy.{datetime64,timedelta64}."
" Please cast to the closest supported unit before converting."
)
raise ValueError(msg)
return normalised_dtype, None
def numpy_values_and_dtype(
values: np.ndarray[Any, Any],
) -> tuple[np.ndarray[Any, Any], type]:
"""Return numpy values and their associated dtype, adjusting if required."""
# Create new dtype object from dtype base name so architecture specific
# dtypes (np.longlong np.ulonglong np.intc np.uintc np.longdouble, ...)
# get converted to their normalized dtype (np.int*, np.uint*, np.float*).
dtype, cast_as = _normalise_numpy_dtype(values.dtype)
if cast_as:
values = values.astype(cast_as)
return values, dtype
def numpy_type_to_constructor(
values: np.ndarray[Any, Any], dtype: type[np.dtype[Any]]View on GitHub (pinned to fe841f959e)