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

Option to make DataArray.transpose also transpose coords #1856

Closed
phausamann opened this issue Jan 25, 2018 · 3 comments · Fixed by #2556
Closed

Option to make DataArray.transpose also transpose coords #1856

phausamann opened this issue Jan 25, 2018 · 3 comments · Fixed by #2556

Comments

@phausamann
Copy link
Contributor

I've noticed that Dataset.transpose transposes the coordinates while DataArray.transpose does not.

Example:

import xarray as xr
import numpy as np

X_da = xr.DataArray(
    np.random.random((100, 10)),
    coords={'coord_1': (['sample', 'feature'], np.ones((100, 10)))},
    dims=('sample', 'feature')
)

In []: X_da.transpose().coord_1.dims
Out[]: ('sample', 'feature')

X_ds = xr.Dataset(
    {'var_1': (['sample', 'feature'], np.random.random((100, 10)))},
    coords={'coord_1': (['sample', 'feature'], np.ones((100, 10)))},
)

In []: X_ds.transpose().coord_1.dims
Out[]: ('feature', 'sample')

This behaviour is probably intentional, but there are cases where I'd like DataArray.transpose to also transpose the coordinates. As a workaround, I have to convert to a Dataset, transpose and convert back.

I was thinking that DataArray.transpose could accept a keyword argument transpose_coords which would be False by default. I could work on a PR implementing this behavior if it's desired.

@shoyer
Copy link
Member

shoyer commented Jan 29, 2018

To be honest, I'm not sure skipping coordinates was actually intentional. In most cases, I suspect transposing coordinates simply doesn't matter because either (1) there are no multi-dimensional coordinates or (2) xarray usually doesn't care much about dimension order (so it doesn't matter).

I would support adding a transpose_coords argument, but we should also consider switching the default behavior in the future to transpose_coords=True.

Anyone else have opinions on this API?

@NicWayand
Copy link

Also hitting this issue. (Use case: formatting netcdf files for some R code that does not have labeled indexing... ugh). Thanks @phausamann for the work around. Default transposing of coods makes sense to me.

@shoyer
Copy link
Member

shoyer commented Jun 19, 2018

Honestly, we could probably consider the DataArray behavior to be a bug. In general DataArray methods should work exactly like Dataset methods on a Dataset with a single data variable. (In fact, this is how we implement many DataArray methods.)

phausamann added a commit to phausamann/xarray that referenced this issue Nov 16, 2018
shoyer pushed a commit that referenced this issue May 21, 2019
* Add transpose_coords option to DataArray.transpose
Fixes #1856

* Fix typo

* Fix bug in transpose
Fix python 2 compatibility

* Set default for transpose_coords to None
Update documentation

* Fix bug in coordinate tranpose
Update documentation

* Suppress FutureWarning in tests

* Add restore_coord_dims parameter to DataArrayGroupBy.apply

* Move restore_coord_dims parameter to GroupBy class

* Remove restore_coord_dims parameter from DataArrayResample.apply

* Update whats-new

* Update whats-new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants