Skip to content
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

assign_coords downgrades GeometryIndex to PandasIndex #13

Closed
martinfleis opened this issue Dec 1, 2022 · 5 comments
Closed

assign_coords downgrades GeometryIndex to PandasIndex #13

martinfleis opened this issue Dec 1, 2022 · 5 comments

Comments

@martinfleis
Copy link
Member

If you call assign_coords on a DataArray with one or more GeometryIndex, they all get downgraded to PandasIndex.

This also needs to be fixed before I can move on with #11 as it currently breaks transformation of multiple coordinates at the same time.

In [2]: geom_dataset = xarray.Dataset(
   ...:     coords={"geom": numpy.array([shapely.Point(1, 2), shapely.Point(3, 4)]), "value": [1, 2]}
   ...:     ).drop_indexes("geom").set_xindex("geom", GeometryIndex, crs=26915)
   ...: geom_dataset.xindexes
Out[2]: 
Indexes:
    value    PandasIndex
    geom     GeometryIndex (crs=EPSG:26915)

In [3]: geom_dataset.assign_coords(new=['a', 'b']).xindexes
Out[3]: 
Indexes:
    value    PandasIndex
    geom     PandasIndex
    new      PandasIndex
@martinfleis
Copy link
Member Author

@benbovy I am wondering if this is actually not an xarray bug. Any ideas if I should look for a fix on our side or report it there?

@benbovy
Copy link
Member

benbovy commented Dec 2, 2022

It is indeed a bug in Xarray :-). Fix is here: pydata/xarray#7347

In the meantime, I think the easiest workaround is to re-assign a GeometryIndex with set_xindex for each of the impacted coordinates. This is cheap as GeometryIndex reuses any pre-existing pandas index wrapped in the input coordinate variable.

@benbovy
Copy link
Member

benbovy commented Dec 2, 2022

Or you can wait for the next, imminent Xarray release 2022.12.0.

@martinfleis
Copy link
Member Author

I already have a solution locally with a todo to change it once it comes. Thanks for the quick fix!

@martinfleis
Copy link
Member Author

Closed by an xarray fix.

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

No branches or pull requests

2 participants