Skip to content

Option to make DataArray.transpose also transpose coords #1856

Closed
@phausamann

Description

@phausamann

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions