diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 20e829d293e..f9afd465f58 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -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 ---------- @@ -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 + + 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 + + array([10, 20, 30]) + Coordinates: + * t (t) int64 0 1 2 + Attributes: + title: air temperature """ coords = None if "coords" in d: diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index fb30cf22e04..1c08d4f3d00 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -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 ---------- @@ -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 + + Dimensions: (t: 3) + Coordinates: + * t (t) int64 0 1 2 + Data variables: + a (t) >> 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 + + Dimensions: (t: 3) + Coordinates: + * t (t) int64 0 1 2 + Data variables: + a (t) int64 10 20 30 + b (t)