Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type of .assign_coords #8495

Merged
merged 6 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions xarray/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ def _calc_assign_results(

def assign_coords(
self,
coords: Mapping[Any, Any] | None = None,
coords: Mapping | None = None,
**coords_kwargs: Any,
) -> Self:
"""Assign new coordinates to this object.
Expand All @@ -484,15 +484,17 @@ def assign_coords(

Parameters
----------
coords : dict-like or None, 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
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 : mapping of dim to coord, optional
A mapping whose keys are the names of the coordinates and values are the
coordinates to assign. The mapping will generally be a dict or
:class:`Coordinates`.
- If a value is a standard data value — for example, a ``DataArray``,
scalar, or array — the data is simply assigned as a coordinate.
- If a value is callable, it is called with this object as the only
parameter, and the return value use as new coordinate variables.
- A 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 : optional
The keyword arguments form of ``coords``.
One of ``coords`` or ``coords_kwargs`` must be provided.
Expand Down Expand Up @@ -593,14 +595,6 @@ def assign_coords(
Attributes:
description: Weather-related data

Notes
-----
Since ``coords_kwargs`` is a dictionary, the order of your arguments
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is no longer the case in modern python...

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.

See Also
--------
Dataset.assign
Expand Down
15 changes: 10 additions & 5 deletions xarray/core/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,11 +571,15 @@ def assign(self, coords: Mapping | None = None, **coords_kwargs: Any) -> Self:

Parameters
----------
coords : :class:`Coordinates` or mapping of hashable to Any
Mapping from coordinate names to the new values. If a ``Coordinates``
object is passed, its indexes are assigned in the returned object.
Otherwise, a default (pandas) index is created for each dimension
coordinate found in the mapping.
coords : mapping of dim to coord, optional
A mapping whose keys are the names of the coordinates and values are the
coordinates to assign. The mapping will generally be a dict or
:class:`Coordinates`.
- If a value is a standard data value — for example, a ``DataArray``,
scalar, or array — the data is simply assigned as a coordinate.
- A 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
The keyword arguments form of ``coords``.
One of ``coords`` or ``coords_kwargs`` must be provided.
Expand Down Expand Up @@ -605,6 +609,7 @@ def assign(self, coords: Mapping | None = None, **coords_kwargs: Any) -> Self:
* y_level_1 (y) int64 0 1 0 1

"""
# TODO: this doesn't support a callable, which is inconsistent with `DataArray.assign_coords`
coords = either_dict_or_kwargs(coords, coords_kwargs, "assign")
new_coords = self.copy()
new_coords.update(coords)
Expand Down
Loading