Skip to content

Commit

Permalink
from_dict: doctest (#6302)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathause authored Feb 28, 2022
1 parent 17acbb0 commit 4292bde
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 44 deletions.
47 changes: 28 additions & 19 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2873,25 +2873,7 @@ def to_dict(self, data: bool = True) -> dict:

@classmethod
def from_dict(cls, d: dict) -> DataArray:
"""
Convert a dictionary into an xarray.DataArray
Input dict can take several forms:
.. code:: python
d = {"dims": "t", "data": x}
d = {
"coords": {"t": {"dims": "t", "data": t, "attrs": {"units": "s"}}},
"attrs": {"title": "air temperature"},
"dims": "t",
"data": x,
"name": "a",
}
where "t" is the name of the dimension, "a" is the name of the array,
and x and t are lists, numpy.arrays, or pandas objects.
"""Convert a dictionary into an xarray.DataArray
Parameters
----------
Expand All @@ -2906,6 +2888,33 @@ def from_dict(cls, d: dict) -> DataArray:
--------
DataArray.to_dict
Dataset.from_dict
Examples
--------
>>> d = {"dims": "t", "data": [1, 2, 3]}
>>> da = xr.DataArray.from_dict(d)
>>> da
<xarray.DataArray (t: 3)>
array([1, 2, 3])
Dimensions without coordinates: t
>>> d = {
... "coords": {
... "t": {"dims": "t", "data": [0, 1, 2], "attrs": {"units": "s"}}
... },
... "attrs": {"title": "air temperature"},
... "dims": "t",
... "data": [10, 20, 30],
... "name": "a",
... }
>>> da = xr.DataArray.from_dict(d)
>>> da
<xarray.DataArray 'a' (t: 3)>
array([10, 20, 30])
Coordinates:
* t (t) int64 0 1 2
Attributes:
title: air temperature
"""
coords = None
if "coords" in d:
Expand Down
67 changes: 42 additions & 25 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5641,31 +5641,7 @@ def to_dict(self, data=True):

@classmethod
def from_dict(cls, d):
"""
Convert a dictionary into an xarray.Dataset.
Input dict can take several forms:
.. code:: python
d = {
"t": {"dims": ("t"), "data": t},
"a": {"dims": ("t"), "data": x},
"b": {"dims": ("t"), "data": y},
}
d = {
"coords": {"t": {"dims": "t", "data": t, "attrs": {"units": "s"}}},
"attrs": {"title": "air temperature"},
"dims": "t",
"data_vars": {
"a": {"dims": "t", "data": x},
"b": {"dims": "t", "data": y},
},
}
where "t" is the name of the dimesion, "a" and "b" are names of data
variables and t, x, and y are lists, numpy.arrays or pandas objects.
"""Convert a dictionary into an xarray.Dataset.
Parameters
----------
Expand All @@ -5682,6 +5658,47 @@ def from_dict(cls, d):
--------
Dataset.to_dict
DataArray.from_dict
Examples
--------
>>> d = {
... "t": {"dims": ("t"), "data": [0, 1, 2]},
... "a": {"dims": ("t"), "data": ["a", "b", "c"]},
... "b": {"dims": ("t"), "data": [10, 20, 30]},
... }
>>> ds = xr.Dataset.from_dict(d)
>>> ds
<xarray.Dataset>
Dimensions: (t: 3)
Coordinates:
* t (t) int64 0 1 2
Data variables:
a (t) <U1 'a' 'b' 'c'
b (t) int64 10 20 30
>>> d = {
... "coords": {
... "t": {"dims": "t", "data": [0, 1, 2], "attrs": {"units": "s"}}
... },
... "attrs": {"title": "air temperature"},
... "dims": "t",
... "data_vars": {
... "a": {"dims": "t", "data": [10, 20, 30]},
... "b": {"dims": "t", "data": ["a", "b", "c"]},
... },
... }
>>> ds = xr.Dataset.from_dict(d)
>>> ds
<xarray.Dataset>
Dimensions: (t: 3)
Coordinates:
* t (t) int64 0 1 2
Data variables:
a (t) int64 10 20 30
b (t) <U1 'a' 'b' 'c'
Attributes:
title: air temperature
"""

if not {"coords", "data_vars"}.issubset(set(d)):
Expand Down

0 comments on commit 4292bde

Please sign in to comment.