pydata / xarray

N-D labeled arrays and datasets in Python
https://xarray.dev
Apache License 2.0
3.61k stars 1.08k forks source link

Creation of empty DataArrays in a Dataset #9468

Open itcarroll opened 1 month ago

itcarroll commented 1 month ago

Is your feature request related to a problem?

Initializing a DataArray from a scalar along with non-scalar coords and dims, which was implemented in #3159, does not extend to Datasets. Initializing multiple DataArrays with shared dimensions, as a Dataset, is not similarly possible.

Describe the solution you'd like

Dataset creation and assignment could handle scalar values in the same way as DataArray creation.

For example

Dataset({"y": ("x", None)}, coords={"x": [0, 1, 2]})

could yield the same thing as

Dataset({"y": xr.DataArray(None, coords={"x": [0, 1, 2]}, dims="x")})

Only the first case easily extends to multiple variables sharing x.

Describe alternatives you've considered

Defining Coordinates in advance is the currently working alternative, but it's not as elegant and is a rare case of needing to explicitly define a Coordinates instance.

Additional context

If this enhancement is supported, I would attempt a PR.

shoyer commented 1 month ago

This sounds like a reasonable addition to me.