From c4cf5f63673d3f59bff140860ea0528a851a99b3 Mon Sep 17 00:00:00 2001 From: EtienneCmb Date: Thu, 9 Apr 2020 10:04:04 +0200 Subject: [PATCH 1/8] new coords to existing dim (doc) --- xarray/core/common.py | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/xarray/core/common.py b/xarray/core/common.py index a003642076f..2486c49c574 100644 --- a/xarray/core/common.py +++ b/xarray/core/common.py @@ -398,11 +398,15 @@ def assign_coords(self, coords=None, **coords_kwargs): Parameters ---------- - coords : dict, optional - A dict with keys which are variables names. If the values are - callable, they are computed on this object and assigned to new - coordinate variables. If the values are not callable, - (e.g. a ``DataArray``, scalar, or array), they are simply assigned. + coords : (dict, tuple, optional) + If a dict is provided, the keys are the names of the coordinates + with the new values to assign. If the values are callable, they are + computed on this object and assigned to new coordinate variables. + If the values are not callable, (e.g. a ``DataArray``, scalar, or + array), they are simply assigned. A new coordinate can also be + defined and attached to an existing dimension using a tuple with + the first element the dimension name and the second element the + values for this new coordinate. **coords_kwargs : keyword, value pairs, optional The keyword arguments form of ``coords``. @@ -440,10 +444,22 @@ def assign_coords(self, coords=None, **coords_kwargs): Coordinates: * lon (lon) int64 -2 -1 0 1 + New coordinate can also be attached to an existing dimension : + + >>> lon_2 = np.array([300, 289, 0, 1]) + >>> da.assign_coords(lon_2=('lon', lon_2)) + + array([0.28298 , 0.667347, 0.657938, 0.177683]) + Coordinates: + * lon (lon) int64 358 359 0 1 + lon_2 (lon) int64 300 289 0 1 + >>> # Note that the same result can also be obtained with a dict e.g. + >>> # `da.assign_coords(dict(lon_2=('lon', lon_2)))` + Notes ----- - Since ``coords_kwargs`` is a dictionary, the order of your arguments may - not be preserved, and so the order of the new variables is not well + Since ``coords_kwargs`` is a dictionary, the order of your arguments + may not be preserved, and so the order of the new variables is not well defined. Assigning multiple variables within the same ``assign_coords`` is possible, but you cannot reference other variables created within the same ``assign_coords`` call. From 73aff230590fe8fdfb34b4d1818ddbdfc9e4500b Mon Sep 17 00:00:00 2001 From: Etienne Combrisson Date: Fri, 10 Apr 2020 09:38:58 +0200 Subject: [PATCH 2/8] Update xarray/core/common.py Co-Authored-By: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> --- xarray/core/common.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/xarray/core/common.py b/xarray/core/common.py index 2486c49c574..76a72e80225 100644 --- a/xarray/core/common.py +++ b/xarray/core/common.py @@ -453,8 +453,9 @@ def assign_coords(self, coords=None, **coords_kwargs): Coordinates: * lon (lon) int64 358 359 0 1 lon_2 (lon) int64 300 289 0 1 - >>> # Note that the same result can also be obtained with a dict e.g. - >>> # `da.assign_coords(dict(lon_2=('lon', lon_2)))` + + Note that the same result can also be obtained with a dict e.g. + >>> _ = da.assign_coords(dict(lon_2=('lon', lon_2))) Notes ----- From 747e0e53431020a012146e033865a2d5900b14e3 Mon Sep 17 00:00:00 2001 From: EtienneCmb Date: Fri, 10 Apr 2020 09:47:10 +0200 Subject: [PATCH 3/8] rm spaces --- xarray/core/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/common.py b/xarray/core/common.py index 76a72e80225..4cfa1d2078e 100644 --- a/xarray/core/common.py +++ b/xarray/core/common.py @@ -453,7 +453,7 @@ def assign_coords(self, coords=None, **coords_kwargs): Coordinates: * lon (lon) int64 358 359 0 1 lon_2 (lon) int64 300 289 0 1 - + Note that the same result can also be obtained with a dict e.g. >>> _ = da.assign_coords(dict(lon_2=('lon', lon_2))) From 58034e8e142cff065af9c09a8202eb60e18323d1 Mon Sep 17 00:00:00 2001 From: EtienneCmb Date: Fri, 10 Apr 2020 09:52:16 +0200 Subject: [PATCH 4/8] update whatsnew --- doc/whats-new.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 1d00cb369d1..40e225266b9 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -76,6 +76,10 @@ Bug fixes Documentation ~~~~~~~~~~~~~ +- update the docstring of :py:meth:`DataArray.assign_coords` : clarify how to + add a new coordinate to an existing dimension and illustrative example + (:issue:`3958`, :pull:`3952`) By + `Etienne Combrisson `_. - update the docstring of :py:meth:`Dataset.diff` and :py:meth:`DataArray.diff` so it does document the ``dim`` parameter as required. (:issue:`1040`, :pull:`3909`) From 858cd964f52543da0934e3e4f344a15b2ada75f9 Mon Sep 17 00:00:00 2001 From: EtienneCmb Date: Fri, 10 Apr 2020 09:54:35 +0200 Subject: [PATCH 5/8] swap issue and PR --- doc/whats-new.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 40e225266b9..9ee350e084d 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -78,7 +78,7 @@ Documentation ~~~~~~~~~~~~~ - update the docstring of :py:meth:`DataArray.assign_coords` : clarify how to add a new coordinate to an existing dimension and illustrative example - (:issue:`3958`, :pull:`3952`) By + (:issue:`3952`, :pull:`3958`) By `Etienne Combrisson `_. - update the docstring of :py:meth:`Dataset.diff` and :py:meth:`DataArray.diff` so it does document the ``dim`` From 4f3f23efc04a1a2d4d06564ebd30f413526e22b6 Mon Sep 17 00:00:00 2001 From: Etienne Combrisson Date: Fri, 10 Apr 2020 14:14:16 +0200 Subject: [PATCH 6/8] Update xarray/core/common.py add space Co-Authored-By: keewis --- xarray/core/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/common.py b/xarray/core/common.py index 4cfa1d2078e..280dbf095ab 100644 --- a/xarray/core/common.py +++ b/xarray/core/common.py @@ -444,7 +444,7 @@ def assign_coords(self, coords=None, **coords_kwargs): Coordinates: * lon (lon) int64 -2 -1 0 1 - New coordinate can also be attached to an existing dimension : + New coordinate can also be attached to an existing dimension: >>> lon_2 = np.array([300, 289, 0, 1]) >>> da.assign_coords(lon_2=('lon', lon_2)) From c57b0ef6319bcabc5c0712966cadbfdad489df4b Mon Sep 17 00:00:00 2001 From: Etienne Combrisson Date: Fri, 10 Apr 2020 14:15:25 +0200 Subject: [PATCH 7/8] Update xarray/core/common.py Co-Authored-By: keewis --- xarray/core/common.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/core/common.py b/xarray/core/common.py index 280dbf095ab..f0a62e55076 100644 --- a/xarray/core/common.py +++ b/xarray/core/common.py @@ -398,8 +398,8 @@ def assign_coords(self, coords=None, **coords_kwargs): Parameters ---------- - coords : (dict, tuple, optional) - If a dict is provided, the keys are the names of the coordinates + coords : dict, optional + A dict where the keys are the names of the coordinates with the new values to assign. If the values are callable, they are computed on this object and assigned to new coordinate variables. If the values are not callable, (e.g. a ``DataArray``, scalar, or From a0915159a7946c4be181e21b2665531e4fba7bc6 Mon Sep 17 00:00:00 2001 From: Etienne Combrisson Date: Fri, 10 Apr 2020 14:15:36 +0200 Subject: [PATCH 8/8] Update xarray/core/common.py Co-Authored-By: keewis --- xarray/core/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/common.py b/xarray/core/common.py index f0a62e55076..e8c43e964aa 100644 --- a/xarray/core/common.py +++ b/xarray/core/common.py @@ -455,7 +455,7 @@ def assign_coords(self, coords=None, **coords_kwargs): lon_2 (lon) int64 300 289 0 1 Note that the same result can also be obtained with a dict e.g. - >>> _ = da.assign_coords(dict(lon_2=('lon', lon_2))) + >>> _ = da.assign_coords({"lon_2": ('lon', lon_2)}) Notes -----