{"id":29796437,"url":"https://github.com/xarray-contrib/xarray-dataclass","last_synced_at":"2025-07-28T05:09:59.584Z","repository":{"id":305649414,"uuid":"945912243","full_name":"xarray-contrib/xarray-dataclass","owner":"xarray-contrib","description":":zap: xarray data creation by data classes","archived":false,"fork":false,"pushed_at":"2025-07-24T17:55:31.000Z","size":3801,"stargazers_count":2,"open_issues_count":5,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-24T22:34:25.130Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://astropenguin.github.io/xarray-dataclasses/v2.0.0","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"astropenguin/xarray-dataclasses","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xarray-contrib.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":"CITATION.cff","codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-03-10T10:18:40.000Z","updated_at":"2025-07-24T19:21:13.000Z","dependencies_parsed_at":"2025-07-24T22:34:28.832Z","dependency_job_id":"0156be38-8105-49f0-8f2a-7a87fe6f1ee0","html_url":"https://github.com/xarray-contrib/xarray-dataclass","commit_stats":null,"previous_names":["melonora/xarray-dataclasses","melonora/xarray-dataclass"],"tags_count":28,"template":false,"template_full_name":null,"purl":"pkg:github/xarray-contrib/xarray-dataclass","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xarray-contrib%2Fxarray-dataclass","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xarray-contrib%2Fxarray-dataclass/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xarray-contrib%2Fxarray-dataclass/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xarray-contrib%2Fxarray-dataclass/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xarray-contrib","download_url":"https://codeload.github.com/xarray-contrib/xarray-dataclass/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xarray-contrib%2Fxarray-dataclass/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267464522,"owners_count":24091505,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-28T02:00:09.689Z","response_time":68,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-07-28T05:09:58.903Z","updated_at":"2025-07-28T05:09:59.569Z","avatar_url":"https://github.com/xarray-contrib.png","language":"Python","readme":"# Important Note\nThis repository was a fork from [here](https://github.com/astropenguin/xarray-dataclasses). We are grateful for the \nwork of the developer on this repo. That being said, sadly the state on main has been the case that the code was \ndeleted and there has been no development for a while. Therefore, we intially decided to fork the repository and \ncontinue development here, where the community is better able to contribute to and maintain the project. We now changed\nit into a standalone repository.\n\nNote, the repository is not yet released. We are making some additional changes before release.\n\n# xarray-dataclasses\n\n[![Release](https://img.shields.io/pypi/v/xarray-dataclasses?label=Release\u0026color=cornflowerblue\u0026style=flat-square)](https://pypi.org/project/xarray-dataclasses/)\n[![Python](https://img.shields.io/pypi/pyversions/xarray-dataclasses?label=Python\u0026color=cornflowerblue\u0026style=flat-square)](https://pypi.org/project/xarray-dataclasses/)\n[![Downloads](https://img.shields.io/pypi/dm/xarray-dataclasses?label=Downloads\u0026color=cornflowerblue\u0026style=flat-square)](https://pepy.tech/project/xarray-dataclasses)\n[![DOI](https://img.shields.io/badge/DOI-10.5281/zenodo.4624819-cornflowerblue?style=flat-square)](https://doi.org/10.5281/zenodo.4624819)\n[![Tests](https://img.shields.io/github/actions/workflow/status/astropenguin/xarray-dataclasses/tests.yml?label=Tests\u0026style=flat-square)](https://github.com/astropenguin/xarray-dataclasses/actions/workflows/tests.yaml)\n[![Release](https://github.com/melonora/xarray-dataclasses/actions/workflows/pypi.yaml/badge.svg?event=release)](https://github.com/melonora/xarray-dataclasses/actions/workflows/pypi.yaml)\n\nxarray data creation by data classes\n\n## Overview\n\nxarray-dataclasses is a Python package that makes it easy to create [xarray]'s DataArray and Dataset objects that are \"typed\" (i.e. fixed dimensions, data type, coordinates, attributes, and name) using [the Python's dataclass]:\n\n```python\nfrom dataclasses import dataclass\nfrom typing import Literal\nfrom xarray_dataclasses import AsDataArray, Coord, Data\n\n\nX = Literal[\"x\"]\nY = Literal[\"y\"]\n\n\n@dataclass\nclass Image(AsDataArray):\n    \"\"\"2D image as DataArray.\"\"\"\n\n    data: Data[tuple[X, Y], float]\n    x: Coord[X, int] = 0\n    y: Coord[Y, int] = 0\n```\n\n### Features\n\n- Typed DataArray or Dataset objects can easily be created:\n  ```python\n  image = Image.new([[0, 1], [2, 3]], [0, 1], [0, 1])\n  ```\n- NumPy-like filled-data creation is also available:\n  ```python\n  image = Image.zeros([2, 2], x=[0, 1], y=[0, 1])\n  ```\n- Support for features by [the Python's dataclass] (`field`, `__post_init__`, ...).\n- Support for static type check by [Pyright].\n\n### Installation\n\nThere are multiple ways you can install xarray-dataclasses, dependent on what kind of dependency manager you use.\n\n```shell\npip install xarray-dataclasses\nconda install -c conda-forge xarray-dataclasses\npixi add xarray-dataclasses\npixi add --pypi xarray-dataclasses\n```\n\n## Basic usage\n\nxarray-dataclasses uses [the Python's dataclass].\nData (or data variables), coordinates, attributes, and a name of DataArray or Dataset objects will be defined as dataclass fields by special type hints (`Data`, `Coord`, `Attr`, `Name`), respectively.\nNote that the following code is supposed in the examples below.\n\n```python\nfrom dataclasses import dataclass\nfrom typing import Literal\nfrom xarray_dataclasses import AsDataArray, AsDataset\nfrom xarray_dataclasses import Attr, Coord, Data, Name\n\n\nX = Literal[\"x\"]\nY = Literal[\"y\"]\n```\n\n### Data field\n\nData field is a field whose value will become the data of a DataArray object or a data variable of a Dataset object.\nThe type hint `Data[TDims, TDtype]` fixes the dimensions and the data type of the object.\nHere are some examples of how to specify them.\n\nType hint | Inferred dimensions\n--- | ---\n`Data[tuple[()], ...]` | `()`\n`Data[Literal[\"x\"], ...]` | `(\"x\",)`\n`Data[tuple[Literal[\"x\"]], ...]` | `(\"x\",)`\n`Data[tuple[Literal[\"x\"], Literal[\"y\"]], ...]` | `(\"x\", \"y\")`\n\nType hint | Inferred data type\n--- | ---\n`Data[..., Any]` | `None`\n`Data[..., None]` | `None`\n`Data[..., float]` | `numpy.dtype(\"float64\")`\n`Data[..., numpy.float128]` | `numpy.dtype(\"float128\")`\n`Data[..., Literal[\"datetime64[ns]\"]]` | `numpy.dtype(\"\u003cM8[ns]\")`\n\n### Coordinate field\n\nCoordinate field is a field whose value will become a coordinate of a DataArray or a Dataset object.\nThe type hint `Coord[TDims, TDtype]` fixes the dimensions and the data type of the object.\n\n### Attribute field\n\nAttribute field is a field whose value will become an attribute of a DataArray or a Dataset object.\nThe type hint `Attr[TAttr]` specifies the type of the value, which is used only for static type check.\n\n### Name field\n\nName field is a field whose value will become the name of a DataArray object.\nThe type hint `Name[TName]` specifies the type of the value, which is used only for static type check.\n\n### DataArray class\n\nDataArray class is a dataclass that defines typed DataArray specifications.\nExactly one data field is allowed in a DataArray class.\nThe second and subsequent data fields are just ignored in DataArray creation.\n\n```python\n@dataclass\nclass Image(AsDataArray):\n    \"\"\"2D image as DataArray.\"\"\"\n\n    data: Data[tuple[X, Y], float]\n    x: Coord[X, int] = 0\n    y: Coord[Y, int] = 0\n    units: Attr[str] = \"cd / m^2\"\n    name: Name[str] = \"luminance\"\n```\n\nA DataArray object will be created by a class method `new()`:\n\n```python\nImage.new([[0, 1], [2, 3]], x=[0, 1], y=[0, 1])\n\n\u003cxarray.DataArray \"luminance\" (x: 2, y: 2)\u003e\narray([[0., 1.],\n       [2., 3.]])\nCoordinates:\n  * x        (x) int64 0 1\n  * y        (y) int64 0 1\nAttributes:\n    units:    cd / m^2\n```\n\nNumPy-like class methods (`zeros()`, `ones()`, ...) are also available:\n\n```python\nImage.ones((3, 3))\n\n\u003cxarray.DataArray \"luminance\" (x: 3, y: 3)\u003e\narray([[1., 1., 1.],\n       [1., 1., 1.],\n       [1., 1., 1.]])\nCoordinates:\n  * x        (x) int64 0 0 0\n  * y        (y) int64 0 0 0\nAttributes:\n    units:    cd / m^2\n```\n\n### Dataset class\n\nDataset class is a dataclass that defines typed Dataset specifications.\nMultiple data fields are allowed to define the data variables of the object.\n\n```python\n@dataclass\nclass ColorImage(AsDataset):\n    \"\"\"2D color image as Dataset.\"\"\"\n\n    red: Data[tuple[X, Y], float]\n    green: Data[tuple[X, Y], float]\n    blue: Data[tuple[X, Y], float]\n    x: Coord[X, int] = 0\n    y: Coord[Y, int] = 0\n    units: Attr[str] = \"cd / m^2\"\n```\n\nA Dataset object will be created by a class method `new()`:\n\n```python\nColorImage.new(\n    [[0, 0], [0, 0]],  # red\n    [[1, 1], [1, 1]],  # green\n    [[2, 2], [2, 2]],  # blue\n)\n\n\u003cxarray.Dataset\u003e\nDimensions:  (x: 2, y: 2)\nCoordinates:\n  * x        (x) int64 0 0\n  * y        (y) int64 0 0\nData variables:\n    red      (x, y) float64 0.0 0.0 0.0 0.0\n    green    (x, y) float64 1.0 1.0 1.0 1.0\n    blue     (x, y) float64 2.0 2.0 2.0 2.0\nAttributes:\n    units:    cd / m^2\n```\n\n## Advanced usage\n\n### Coordof and Dataof type hints\n\nxarray-dataclasses provides advanced type hints, `Coordof` and `Dataof`.\nUnlike `Data` and `Coord`, they specify a dataclass that defines a DataArray class.\nThis is useful when users want to add metadata to dimensions for [plotting].\nFor example:\n\n```python\nfrom xarray_dataclasses import Coordof\n\n\n@dataclass\nclass XAxis:\n    data: Data[X, int]\n    long_name: Attr[str] = \"x axis\"\n    units: Attr[str] = \"pixel\"\n\n\n@dataclass\nclass YAxis:\n    data: Data[Y, int]\n    long_name: Attr[str] = \"y axis\"\n    units: Attr[str] = \"pixel\"\n\n\n@dataclass\nclass Image(AsDataArray):\n    \"\"\"2D image as DataArray.\"\"\"\n\n    data: Data[tuple[X, Y], float]\n    x: Coordof[XAxis] = 0\n    y: Coordof[YAxis] = 0\n```\n\n### General data variable names in Dataset creation\n\nDue to the limitation of Python's parameter names, it is not possible to define data variable names that contain white spaces, for example.\nIn such cases, please define DataArray classes of each data variable so that they have name fields and specify them by `Dataof` in a Dataset class.\nThen the values of the name fields will be used as data variable names.\nFor example:\n\n```python\n@dataclass\nclass Red:\n    data: Data[tuple[X, Y], float]\n    name: Name[str] = \"Red image\"\n\n\n@dataclass\nclass Green:\n    data: Data[tuple[X, Y], float]\n    name: Name[str] = \"Green image\"\n\n\n@dataclass\nclass Blue:\n    data: Data[tuple[X, Y], float]\n    name: Name[str] = \"Blue image\"\n\n\n@dataclass\nclass ColorImage(AsDataset):\n    \"\"\"2D color image as Dataset.\"\"\"\n\n    red: Dataof[Red]\n    green: Dataof[Green]\n    blue: Dataof[Blue]\n```\n\n```python\nColorImage.new(\n    [[0, 0], [0, 0]],\n    [[1, 1], [1, 1]],\n    [[2, 2], [2, 2]],\n)\n\n\u003cxarray.Dataset\u003e\nDimensions:      (x: 2, y: 2)\nDimensions without coordinates: x, y\nData variables:\n    Red image    (x, y) float64 0.0 0.0 0.0 0.0\n    Green image  (x, y) float64 1.0 1.0 1.0 1.0\n    Blue image   (x, y) float64 2.0 2.0 2.0 2.0\n```\n\n### Customization of DataArray or Dataset creation\n\nFor customization, users can add a special class attribute, `__dataoptions__`, to a DataArray or Dataset class.\nA custom factory for DataArray or Dataset creation is only supported in the current implementation.\n\n\n```python\nimport xarray as xr\nfrom xarray_dataclasses import DataOptions\n\n\nclass Custom(xr.DataArray):\n    \"\"\"Custom DataArray.\"\"\"\n\n    __slots__ = ()\n\n    def custom_method(self) -\u003e bool:\n        \"\"\"Custom method.\"\"\"\n        return True\n\n\n@dataclass\nclass Image(AsDataArray):\n    \"\"\"2D image as DataArray.\"\"\"\n\n    data: Data[tuple[X, Y], float]\n    x: Coord[X, int] = 0\n    y: Coord[Y, int] = 0\n\n    __dataoptions__ = DataOptions(Custom)\n\n\nimage = Image.ones([3, 3])\nisinstance(image, Custom)  # True\nimage.custom_method()  # True\n```\n\n### DataArray and Dataset creation without shorthands\n\nxarray-dataclasses provides functions, `asdataarray` and `asdataset`.\nThis is useful when users do not want to inherit the mix-in class (`AsDataArray` or `AsDataset`) in a DataArray or Dataset dataclass.\nFor example:\n\n```python\nfrom xarray_dataclasses import asdataarray\n\n\n@dataclass\nclass Image:\n    \"\"\"2D image as DataArray.\"\"\"\n\n    data: Data[tuple[X, Y], float]\n    x: Coord[X, int] = 0\n    y: Coord[Y, int] = 0\n\n\nimage = asdataarray(Image([[0, 1], [2, 3]], [0, 1], [0, 1]))\n```\n\n## How to contribute\n\nThank you for being willing to contribute! If you have some ideas to propose, please open an [issue](https://github.com/xarray-contrib/xarray-dataclasses/issues).\nWe use [GitHub flow](https://docs.github.com/en/get-started/using-github/github-flow) for developing and managing the project.\nThe first section describes how to contribute with it.\nThe second and third sections explain how to prepare a local development environment and our automated workflows in GitHub Actions, respectively.\n\n\n### Get the source code\n\n```shell\ngit clone https://github.com/xarray-contrib/xarray-dataclasses\ncd xarray-dataclasses\n```\n\n### Install dependencies\n\nFirst install [pixi](https://pixi.sh/latest/installation/). Then, install project dependencies:\n\n```shell\npixi install -a\npixi run pre-commit-install\n```\n\n### Testing, linting, and formatting\nWe have [a test workflow][test-workflow] for testing, static type checking, linting, and formatting the code.\nIt is used for status checks when a pull request is created.\nIf you would like to check them in local, the following commands are almost equivalent (the difference is that the workflow is run under multiple Python versions).\nFurthermore, these tasks are defined only in the `dev` environment. Pixi does not require you to specify the environment\nin that case.\n\n```shell\npixi run tests\npixi run flake8\npixi run black\npixi run pyright\n```\n\n### Creating documentation\nWe also have a [documentation workflow] (Add link). However, if you want to locally create the documentation \nrun the following:\n\n```shell\npixi run doc_build\n```\n\n### Create a release\n\nThis section is relevant only for maintainers.\n\n1. Pull `git`'s `main` branch.\n2. `pixi install -a`\n3. `pixi run pre-commit-install`\n4. `pixi run -e test test`\n5. `pixi shell`\n6. `hatch version \u003cnew-version\u003e`\n7. `git add .`\n8. `git commit -m \"ENH: Bump version to \u003cversion\u003e\"`\n9. `hatch build`\n10. `hatch publish`\n11. `git push upstream main`\n12. Create a new tag and Release via the GitHub UI. Auto-generate release notes\n    and add additional notes as needed.\n\n\u003c!-- References --\u003e\n[Pyright]: https://github.com/microsoft/pyright\n[the Python's dataclass]: https://docs.python.org/3/library/dataclasses.html\n[xarray]: https://xarray.pydata.org/en/stable/index.html\n[plotting]: https://xarray.pydata.org/en/stable/user-guide/plotting.html#simple-example\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxarray-contrib%2Fxarray-dataclass","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxarray-contrib%2Fxarray-dataclass","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxarray-contrib%2Fxarray-dataclass/lists"}