Skip to content

Commit 449c31a

Browse files
max-sixtybenbovy
andauthored
Fix type of .assign_coords (#8495)
* Fix type of `.assign_coords` As discussed in #8455 * Update xarray/core/common.py Co-authored-by: Benoit Bovy <[email protected]> * Generally improve docstring * . --------- Co-authored-by: Benoit Bovy <[email protected]>
1 parent 50bd8f9 commit 449c31a

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

xarray/core/common.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ def _calc_assign_results(
476476

477477
def assign_coords(
478478
self,
479-
coords: Mapping[Any, Any] | None = None,
479+
coords: Mapping | None = None,
480480
**coords_kwargs: Any,
481481
) -> Self:
482482
"""Assign new coordinates to this object.
@@ -486,15 +486,21 @@ def assign_coords(
486486
487487
Parameters
488488
----------
489-
coords : dict-like or None, optional
490-
A dict where the keys are the names of the coordinates
491-
with the new values to assign. If the values are callable, they are
492-
computed on this object and assigned to new coordinate variables.
493-
If the values are not callable, (e.g. a ``DataArray``, scalar, or
494-
array), they are simply assigned. A new coordinate can also be
495-
defined and attached to an existing dimension using a tuple with
496-
the first element the dimension name and the second element the
497-
values for this new coordinate.
489+
coords : mapping of dim to coord, optional
490+
A mapping whose keys are the names of the coordinates and values are the
491+
coordinates to assign. The mapping will generally be a dict or
492+
:class:`Coordinates`.
493+
494+
* If a value is a standard data value — for example, a ``DataArray``,
495+
scalar, or array — the data is simply assigned as a coordinate.
496+
497+
* If a value is callable, it is called with this object as the only
498+
parameter, and the return value is used as new coordinate variables.
499+
500+
* A coordinate can also be defined and attached to an existing dimension
501+
using a tuple with the first element the dimension name and the second
502+
element the values for this new coordinate.
503+
498504
**coords_kwargs : optional
499505
The keyword arguments form of ``coords``.
500506
One of ``coords`` or ``coords_kwargs`` must be provided.
@@ -595,14 +601,6 @@ def assign_coords(
595601
Attributes:
596602
description: Weather-related data
597603
598-
Notes
599-
-----
600-
Since ``coords_kwargs`` is a dictionary, the order of your arguments
601-
may not be preserved, and so the order of the new variables is not well
602-
defined. Assigning multiple variables within the same ``assign_coords``
603-
is possible, but you cannot reference other variables created within
604-
the same ``assign_coords`` call.
605-
606604
See Also
607605
--------
608606
Dataset.assign

xarray/core/coordinates.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -571,11 +571,18 @@ def assign(self, coords: Mapping | None = None, **coords_kwargs: Any) -> Self:
571571
572572
Parameters
573573
----------
574-
coords : :class:`Coordinates` or mapping of hashable to Any
575-
Mapping from coordinate names to the new values. If a ``Coordinates``
576-
object is passed, its indexes are assigned in the returned object.
577-
Otherwise, a default (pandas) index is created for each dimension
578-
coordinate found in the mapping.
574+
coords : mapping of dim to coord, optional
575+
A mapping whose keys are the names of the coordinates and values are the
576+
coordinates to assign. The mapping will generally be a dict or
577+
:class:`Coordinates`.
578+
579+
* If a value is a standard data value — for example, a ``DataArray``,
580+
scalar, or array — the data is simply assigned as a coordinate.
581+
582+
* A coordinate can also be defined and attached to an existing dimension
583+
using a tuple with the first element the dimension name and the second
584+
element the values for this new coordinate.
585+
579586
**coords_kwargs
580587
The keyword arguments form of ``coords``.
581588
One of ``coords`` or ``coords_kwargs`` must be provided.
@@ -605,6 +612,7 @@ def assign(self, coords: Mapping | None = None, **coords_kwargs: Any) -> Self:
605612
* y_level_1 (y) int64 0 1 0 1
606613
607614
"""
615+
# TODO: this doesn't support a callable, which is inconsistent with `DataArray.assign_coords`
608616
coords = either_dict_or_kwargs(coords, coords_kwargs, "assign")
609617
new_coords = self.copy()
610618
new_coords.update(coords)

0 commit comments

Comments
 (0)