Skip to content

Commit 4292bde

Browse files
authored
from_dict: doctest (#6302)
1 parent 17acbb0 commit 4292bde

File tree

2 files changed

+70
-44
lines changed

2 files changed

+70
-44
lines changed

xarray/core/dataarray.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2873,25 +2873,7 @@ def to_dict(self, data: bool = True) -> dict:
28732873

28742874
@classmethod
28752875
def from_dict(cls, d: dict) -> DataArray:
2876-
"""
2877-
Convert a dictionary into an xarray.DataArray
2878-
2879-
Input dict can take several forms:
2880-
2881-
.. code:: python
2882-
2883-
d = {"dims": "t", "data": x}
2884-
2885-
d = {
2886-
"coords": {"t": {"dims": "t", "data": t, "attrs": {"units": "s"}}},
2887-
"attrs": {"title": "air temperature"},
2888-
"dims": "t",
2889-
"data": x,
2890-
"name": "a",
2891-
}
2892-
2893-
where "t" is the name of the dimension, "a" is the name of the array,
2894-
and x and t are lists, numpy.arrays, or pandas objects.
2876+
"""Convert a dictionary into an xarray.DataArray
28952877
28962878
Parameters
28972879
----------
@@ -2906,6 +2888,33 @@ def from_dict(cls, d: dict) -> DataArray:
29062888
--------
29072889
DataArray.to_dict
29082890
Dataset.from_dict
2891+
2892+
Examples
2893+
--------
2894+
>>> d = {"dims": "t", "data": [1, 2, 3]}
2895+
>>> da = xr.DataArray.from_dict(d)
2896+
>>> da
2897+
<xarray.DataArray (t: 3)>
2898+
array([1, 2, 3])
2899+
Dimensions without coordinates: t
2900+
2901+
>>> d = {
2902+
... "coords": {
2903+
... "t": {"dims": "t", "data": [0, 1, 2], "attrs": {"units": "s"}}
2904+
... },
2905+
... "attrs": {"title": "air temperature"},
2906+
... "dims": "t",
2907+
... "data": [10, 20, 30],
2908+
... "name": "a",
2909+
... }
2910+
>>> da = xr.DataArray.from_dict(d)
2911+
>>> da
2912+
<xarray.DataArray 'a' (t: 3)>
2913+
array([10, 20, 30])
2914+
Coordinates:
2915+
* t (t) int64 0 1 2
2916+
Attributes:
2917+
title: air temperature
29092918
"""
29102919
coords = None
29112920
if "coords" in d:

xarray/core/dataset.py

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5641,31 +5641,7 @@ def to_dict(self, data=True):
56415641

56425642
@classmethod
56435643
def from_dict(cls, d):
5644-
"""
5645-
Convert a dictionary into an xarray.Dataset.
5646-
5647-
Input dict can take several forms:
5648-
5649-
.. code:: python
5650-
5651-
d = {
5652-
"t": {"dims": ("t"), "data": t},
5653-
"a": {"dims": ("t"), "data": x},
5654-
"b": {"dims": ("t"), "data": y},
5655-
}
5656-
5657-
d = {
5658-
"coords": {"t": {"dims": "t", "data": t, "attrs": {"units": "s"}}},
5659-
"attrs": {"title": "air temperature"},
5660-
"dims": "t",
5661-
"data_vars": {
5662-
"a": {"dims": "t", "data": x},
5663-
"b": {"dims": "t", "data": y},
5664-
},
5665-
}
5666-
5667-
where "t" is the name of the dimesion, "a" and "b" are names of data
5668-
variables and t, x, and y are lists, numpy.arrays or pandas objects.
5644+
"""Convert a dictionary into an xarray.Dataset.
56695645
56705646
Parameters
56715647
----------
@@ -5682,6 +5658,47 @@ def from_dict(cls, d):
56825658
--------
56835659
Dataset.to_dict
56845660
DataArray.from_dict
5661+
5662+
Examples
5663+
--------
5664+
>>> d = {
5665+
... "t": {"dims": ("t"), "data": [0, 1, 2]},
5666+
... "a": {"dims": ("t"), "data": ["a", "b", "c"]},
5667+
... "b": {"dims": ("t"), "data": [10, 20, 30]},
5668+
... }
5669+
>>> ds = xr.Dataset.from_dict(d)
5670+
>>> ds
5671+
<xarray.Dataset>
5672+
Dimensions: (t: 3)
5673+
Coordinates:
5674+
* t (t) int64 0 1 2
5675+
Data variables:
5676+
a (t) <U1 'a' 'b' 'c'
5677+
b (t) int64 10 20 30
5678+
5679+
>>> d = {
5680+
... "coords": {
5681+
... "t": {"dims": "t", "data": [0, 1, 2], "attrs": {"units": "s"}}
5682+
... },
5683+
... "attrs": {"title": "air temperature"},
5684+
... "dims": "t",
5685+
... "data_vars": {
5686+
... "a": {"dims": "t", "data": [10, 20, 30]},
5687+
... "b": {"dims": "t", "data": ["a", "b", "c"]},
5688+
... },
5689+
... }
5690+
>>> ds = xr.Dataset.from_dict(d)
5691+
>>> ds
5692+
<xarray.Dataset>
5693+
Dimensions: (t: 3)
5694+
Coordinates:
5695+
* t (t) int64 0 1 2
5696+
Data variables:
5697+
a (t) int64 10 20 30
5698+
b (t) <U1 'a' 'b' 'c'
5699+
Attributes:
5700+
title: air temperature
5701+
56855702
"""
56865703

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

0 commit comments

Comments
 (0)