Skip to content

Raise in combine_by_coords when dataset coordinates overlap - #11957

Open
tushar-mahalya wants to merge 5 commits into
pydata:mainfrom
tushar-mahalya:fix-combine-by-coords-overlap
Open

tushar-mahalya wants to merge 5 commits into
pydata:mainfrom
tushar-mahalya:fix-combine-by-coords-overlap

Conversation

@tushar-mahalya

Copy link
Copy Markdown
Contributor

Description

combine_by_coords orders the datasets by their first coordinate value and then checks that the combined index is monotonic. That check accepts equal neighbouring values, so when two datasets share a boundary label, e.g. x=[0, 1] and x=[1, 2], they are concatenated into x=[0, 1, 1, 2] without any warning. The same happens through open_mfdataset with two files whose time ranges overlap by one step, which is how I'd expect most people to hit it. Overlaps of two or more labels already raise, because the result isn't monotonic, so only the single-label case slipped through.

The fix adds a check after the concat in _combine_single_variable_hypercube: if the combined index has duplicates but every input index along that dimension was unique, it raises a ValueError saying the datasets overlap and suggesting dropping the overlapping labels first (e.g. with preprocess).

On warning versus error: in #7189 a warning first was suggested in case someone relies on this. I went with an error because the result has duplicate labels, which isn't a usable combined index, and it makes the one-label overlap consistent with the larger overlaps that already raise. The case mentioned there as possibly relied upon, inputs that already contain repeated labels, is left alone: the check is skipped whenever any input has repeated labels. The downside is that something like [0, 0, 1] + [1, 2] still isn't caught. Catching it would mean comparing tile boundaries, which felt like more change than this needed. Happy to switch to a warning if you'd prefer that.

Tests cover ascending and descending 1-D overlaps, a 2-D DataArray overlap on a shared timestamp, and that repeated labels within each dataset still combine as before. The three overlap tests fail on main and pass here. I ran test_combine.py, test_concat.py, test_merge.py and test_datatree.py locally (554 passed). In the wider -k "mfdataset or combine_by_coords" run, the only failures are the h5netcdf open_mfdataset tests, which fail on main too because h5py isn't installed in my environment.

Checklist

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR.
      Tools: Claude Code (Claude Sonnet for the fix and tests, Claude Opus for review). I asked it to find an open xarray bug without an existing PR and fix it with a regression test and a whats-new entry; it picked combine_by_coords allows one overlapping coordinate value, but not more than one #7189. I reviewed the diff and the reasoning above.

…ydata#7189)

The final monotonic check accepts repeated values, so datasets with coordinates like [0, 1] and [1, 2] were concatenated into [0, 1, 1, 2]. Raise a ValueError when the combined index has duplicates that were not present in the individual datasets.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

combine_by_coords allows one overlapping coordinate value, but not more than one

1 participant