merging non-dimension coordinates with the Dataset constructor
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
When adding two DataArray objects with different coordinates to a Dataset, a MergeError is raised even though one of the conflicting coords is a subset of the other. Merging dimension coordinates works so I'd expect associated non-dimension coordinates to work, too.
This fails:
In [1]: import xarray as xr
...: import numpy as np
In [2]: a = np.linspace(0, 1, 10)
...: b = np.linspace(-1, 0, 12)
...:
...: x_a = np.arange(10)
...: x_b = np.arange(12)
...:
...: y_a = x_a * 1000
...: y_b = x_b * 1000
...:
...: arr1 = xr.DataArray(data=a, coords={"x": x_a, "y": ("x", y_a)}, dims="x")
...: arr2 = xr.DataArray(data=b, coords={"x": x_b, "y": ("x", y_b)}, dims="x")
...:
...: xr.Dataset({"a": arr1, "b": arr2})
...
MergeError: conflicting values for variable 'y' on objects to be combined. You can skip this check by specifying compat='override'.
While this works:
In [3]: a = np.linspace(0, 1, 10)
...: b = np.linspace(-1, 0, 12)
...:
...: x_a = np.arange(10)
...: x_b = np.arange(12)
...:
...: y_a = x_a * 1000
...: y_b = x_b * 1000
...:
...: xr.Dataset({
...: "a": xr.DataArray(data=a, coords={"x": x_a}, dims="x"),
...: "b": xr.DataArray(data=b, coords={"x": x_b}, dims="x"),
...: })
Out[3]:
<xarray.Dataset>
Dimensions: (x: 12)
Coordinates:
* x (x) int64 0 1 2 3 4 5 6 7 8 9 10 11
Data variables:
a (x) float64 0.0 0.1111 0.2222 0.3333 0.4444 ... 0.8889 1.0 nan nan
b (x) float64 -1.0 -0.9091 -0.8182 -0.7273 ... -0.1818 -0.09091 0.0
I can work around this by calling:
In [4]: xr.merge([arr1.rename("a").to_dataset(), arr2.rename("b").to_dataset()])
Out[4]:
<xarray.Dataset>
Dimensions: (x: 12)
Coordinates:
* x (x) int64 0 1 2 3 4 5 6 7 8 9 10 11
y (x) float64 0.0 1e+03 2e+03 3e+03 ... 8e+03 9e+03 1e+04 1.1e+04
Data variables:
a (x) float64 0.0 0.1111 0.2222 0.3333 0.4444 ... 0.8889 1.0 nan nan
b (x) float64 -1.0 -0.9091 -0.8182 -0.7273 ... -0.1818 -0.09091 0.0
but I think the Dataset constructor should be capable of that, too.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the Dataset constructor reproducer with the two DataArray objects and compare it with xr.merge on renamed datasets. Trace how the constructor handles non-dimension coordinate conflicts versus dimension coordinates; done means the constructor accepts the subset coordinate case and preserves the merged y coordinate without raising MergeError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100