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
Often the xarray API supports either a
dictorkwargs, and it seems like this should be consistent as often as possible. Withassign_coords, this works as expected:Since
assign_coordsworks with keyword arguments, I would expect it to also work with adict, similar tosel,drop, etc. But it does not: