Skip to content

Commit

Permalink
Fix multiindex error on upstream xr (#1450)
Browse files Browse the repository at this point in the history
<!--Please ensure the PR fulfills the following requirements! -->
<!-- If this is your first PR, make sure to add your details to the
AUTHORS.rst! -->
### Pull Request Checklist:
- [x] This PR addresses an already opened issue (for bug fixes /
features)
    - This PR fixes #1417
- [ ] Tests for the changes have been added (for bug fixes / features)
- [ ] (If applicable) Documentation has been added / updated (for bug
fixes / features)
- [x] CHANGES.rst has been updated (with summary of main changes)
- [x] Link to issue (:issue:`number`) and pull request (:pull:`number`)
has been added

### What kind of change does this PR introduce?

Overriding a coordinate with a pandas MultiIndex was not working
properly before, but this didn't result in any errors up to a recent
change in xarray's master. When calling `assign_coords` with a
multiindex on a coordinate that already existed, the sub-indexes would
not appear in the DataArray repr. In the most recent xarray master, that
now triggers an error when trying to unstack the multiindex, but only if
dask is used. Before, the unstacking would proceed as normal and the
sub-indexes would magically appear on the result. Dropping the previous
coordinate solves this problem.

The next xarray will implement a new public `xr.Coordinates` class that
should be used to assign coords, instead of a raw `pd.MultiIndex`.
Nonetheless, overriding is still not possible and dropping the previous
coordinate is still needed.

### Does this PR introduce a breaking change?
No.


### Other information:
See pydata/xarray#8039.
  • Loading branch information
aulemahal authored Aug 3, 2023
2 parents 1052d59 + db78225 commit f126728
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Internal changes
* Increased the guess of number of quantiles needed in ExtremeValues. (:pull:`1413`).
* Tolerance thresholds for error in ``test_processing::test_adapt_freq`` have been relaxed to allow for more variation in the results. (:issue:`1417`, :pull:`1418`).
* Added 'streamflow' to the list of known variables (:pull:`1431`).
* Fix and adapt ``percentile_doy`` for an error raised by xarray > 2023.7.0 (:issue:`1417`, :pull:`1450`).

v0.44.0 (2023-06-23)
--------------------
Expand Down
9 changes: 8 additions & 1 deletion xclim/core/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,14 @@ def percentile_doy(
(rr.time.dt.year.values, rr.time.dt.dayofyear.values),
names=("year", "dayofyear"),
)
rrr = rr.assign_coords(time=ind).unstack("time").stack(stack_dim=("year", "window"))
if hasattr(xr, "Coordinates"):
# xarray > 2023.7.0 will deprecate passing a Pandas MultiIndex directly.
# TODO: Remove this condition when pinning xarray above 2023.7.0
ind = xr.Coordinates.from_pandas_multiindex(ind, "time")
rr = rr.drop_vars("time").assign_coords(ind)
else:
rr = rr.drop_vars("time").assign_coords(time=ind)
rrr = rr.unstack("time").stack(stack_dim=("year", "window"))

if rrr.chunks is not None and len(rrr.chunks[rrr.get_axis_num("stack_dim")]) > 1:
# Preserve chunk size
Expand Down

0 comments on commit f126728

Please sign in to comment.