Skip to content

Dict-like argument support for assign_coords() #3231

@gwgundersen

Description

@gwgundersen

Often the xarray API supports either a dict or kwargs, and it seems like this should be consistent as often as possible. With assign_coords, this works as expected:

>>> dims = ["x", "y"]
>>> coords = {"fruit": ("x", ["apple", "banana"])}
>>> arr = xr.DataArray([[1,2,3],[4,5,6]], dims=dims, coords=coords)
>>> arr.assign_coords(color=("x", ["red", "yellow"]))
<xarray.DataArray (x: 2, y: 3)>
array([[1, 2, 3],
       [4, 5, 6]])
Coordinates:
    fruit    (x) <U6 'apple' 'banana'
    color    (x) <U6 'red' 'yellow'
Dimensions without coordinates: x, y

Since assign_coords works with keyword arguments, I would expect it to also work with a dict, similar to sel, drop, etc. But it does not:

>>> arr.assign_coords({"color": ("x", ["red", "yellow"])})
*** TypeError: assign_coords() takes 1 positional argument but 2 were given

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions