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

Unstacking produces flipped array when using decreasing coordinate values #980

Closed
shoyer opened this issue Aug 22, 2016 · 1 comment
Closed

Comments

@shoyer
Copy link
Member

shoyer commented Aug 22, 2016

As reported by @robintw on the mailing list:

If we create a DataArray with the y co-ordinates decreasing:

da = xr.DataArray(np.random.rand(3, 4), dims = ('y', 'x'),
                  coords={'x': np.arange(4),
                          'y': np.arange(3, 0, -1)})

and then stack it and unstack it again:

stacked = da.stack(allpoints=['y', 'x'])
result = stacked.unstack('allpoints')

We find that the top-left value of the resulting array is not the same as the top-left value of the original array:

da.isel(x=0, y=0) # 0.1143
result.isel(x=0, y=0) # 0.696

However, the value at the 'bottom left' of the array matches the top left of the original array:

result.isel(x=0, y=2) # 0.1143

Replacing the y co-ordinates of da with increasing values gives the correct results:

da.coords['y'] = np.arange(da.coords.y)
da.isel(x=0, y=0) # 0.1143
result.isel(x=0, y=0) # 0.1143

I'm assuming this is unintentional behaviour - at least, I couldn't find anything in the docs about it. Is this meant to happen?

@shoyer
Copy link
Member Author

shoyer commented Aug 22, 2016

The issue here is that pandas.MultiIndex.from_product (used in xarray's stack) sorts levels, which means that when we unstack we get back sorted levels. We should probably fix this by using a custom version of from_product that doesn't sort levels.

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 a pull request may close this issue.

1 participant