Skip to main content

Schema validation for Xarray objects

Project description

xarray-schema

Schema validation for Xarray

CI codecov MIT License

installation

Install xarray-schema from PyPI:

pip install xarray-schema

Conda:

conda install -c conda-forge xarray-schema

Or install it from source:

pip install git+https://github.com/carbonplan/xarray-schema

usage

Xarray-schema's API is modeled after Pandera. The DataArraySchema and DatasetSchema objects both have .validate() methods.

The basic usage is as follows:

import numpy as np
import xarray as xr
from xarray_schema import DataArraySchema, DatasetSchema, CoordsSchema

da = xr.DataArray(np.ones(4, dtype='i4'), dims=['x'], name='foo')

schema = DataArraySchema(dtype=np.integer, name='foo', shape=(4, ), dims=['x'])

schema.validate(da)

You can also use it to validate a Dataset like so:

schema_ds = DatasetSchema({'foo': schema})

schema_ds.validate(da.to_dataset())

Each component of the Xarray data model is implemented as a stand alone class:

from xarray_schema.components import (
    DTypeSchema,
    DimsSchema,
    ShapeSchema,
    NameSchema,
    ChunksSchema,
    ArrayTypeSchema,
    AttrSchema,
    AttrsSchema
)

# example constructions
dtype_schema = DTypeSchema('i4')
dims_schema = DimsSchema(('x', 'y', None))  # None is used as a wildcard
shape_schema = ShapeSchema((5, 10, None))  # None is used as a wildcard
name_schema = NameSchema('foo')
chunk_schema = ChunkSchema({'x': None, 'y': -1})  # None is used as a wildcard, -1 is used as
ArrayTypeSchema = ArrayTypeSchema(np.ndarray)

# Example usage
dtype_schama.validate(da.dtype)

# Each object schema can be exported to JSON format
dtype_json = dtype_schama.to_json()

roadmap

This is a very early prototype of a library. Some key things are missing:

  1. Validation of coords and attrs. These are not implemented yet.
  2. Exceptions: Pandera accumulates schema exceptions and reports them all at once. Currently, we are a eagerly raising SchemaErrors when the are found.
  3. Roundtrip schemas to/from JSON and/or YAML format.

license

All the code in this repository is MIT licensed, but we request that you please provide attribution if reusing any of our digital content (graphics, logo, articles, etc.).

about us

CarbonPlan is a non-profit organization that uses data and science for climate action. We aim to improve the transparency and scientific integrity of climate solutions through open data and tools. Find out more at carbonplan.org or get in touch by opening an issue or sending us an email.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xarray-schema-0.0.3.tar.gz (15.0 kB view details)

Uploaded Source

Built Distribution

xarray_schema-0.0.3-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file xarray-schema-0.0.3.tar.gz.

File metadata

  • Download URL: xarray-schema-0.0.3.tar.gz
  • Upload date:
  • Size: 15.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for xarray-schema-0.0.3.tar.gz
Algorithm Hash digest
SHA256 9c6c760489c0690a70394b2ad1368b32f8fa1333911c361b4adf249384212920
MD5 0c6c5fd6d3148e65ee27e2eed9a6956a
BLAKE2b-256 643de0652fad05696e34897f085d4c3115dc4bedb1fb8c1827d6c79ce74095a6

See more details on using hashes here.

File details

Details for the file xarray_schema-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for xarray_schema-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 aa6f856626b2e100213ba290407797464608b2555bb8e0b26093a97fe1ba38ce
MD5 ccfab9893c83e45a0d984b415dce7e48
BLAKE2b-256 a96df585a27b380ee987619b5617c0ca672a71a4345b67cfedbb6299750ce845

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page