Skip to content

raise Validatation Error on numpy.datetime64 type checking #46

@a2nalea

Description

@a2nalea

Hello,

I would like to use Numpydantic for basic validation checks for xarray.DataArray. I am checking the validity of the coordinates of the array. Some of my datasets have a coordinate of datetime and here it fails.

A simple example:

from pydantic import BaseModel
from numpydantic import NDArray, Shape
import xarray
import numpy

arr = xarray.DataArray(
  numpy.ones((3, 2, 1)),
  coords={"x": [1, 2, 3], "y": [0.1, numpy.NaN], "t": [numpy.datetime64("2025-02-25"), ]},
  dims=["x", "y", "t"]
)

class ArrSchema(BaseModel):
  x: NDArray[Shape["3"], int]
  y: NDArray[Shape["*"], float]
  t: NDArray[Shape["1"], numpy.datetime64]

ArrSchema(**arr.coords) # Raises a ValidationError
ValidationError: 1 validation error for TestingSchema
t
  Value error, Invalid dtype! expected <class 'numpy.datetime64'>, got datetime64[D] [type=value_error, input_value=array(['2025-02-25'], dtype='datetime64[D]'), input_type=ndarray]
    For further information visit https://errors.pydantic.dev/2.7/v/value_error

I get the same error, if I use ArrSchema directly with a numpy array.

version: v1.6.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdtype

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions