Skip to content

Replace hacks that worked on old versions of xarray with proper xarray calls #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog_unreleased/new-xarray-compat.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* The latest (not-yet-released) version of xarray contains a rework of indexing
and some small changes to our I/O functions are necessary to support both old
and new xarray.
2 changes: 1 addition & 1 deletion primap2/_setters.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def set(
# to broadcast value only to sel, but would need more careful handling
# later.
if new == "extend":
value, expanded = xr.broadcast(value, self._da)
expanded, value = xr.broadcast(self._da, value)
else:
expanded = self._da
value = value.broadcast_like(expanded)
Expand Down
9 changes: 6 additions & 3 deletions primap2/pm2io/_interchange_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,12 @@ def from_interchange_format(
for entity, dims in dimensions.items():
da_entity = da.loc[{entity_col: entity}]
# we still have a full MultiIndex, so trim it to the relevant dimensions
da_entity["index"] = da_entity.indexes["index"].droplevel(
list(index_cols - set(dims))
)
da_entity = da_entity.reset_index(list(index_cols - set(dims)), drop=True)
# depending on the version of xarray, an atomic, 0-dimensional coord
# for the entity remains. we have to remove it to be able to combine the
# dataset afterwards.
if entity_col in da_entity.coords:
da_entity = da_entity.drop(entity_col)
# now we can safely unstack the index
data_vars[entity] = da_entity.unstack("index").astype(dtypes[entity])

Expand Down