Skip to content

Transpose some but not all dimensions #1081

Closed
@Yefee

Description

@Yefee

Hi, all

Sorry to bother. Maybe it is a kind of stupid question for others, but I cannot figure it out at this moment.

I want to swap dims in xarray, like swapaxes in numpy. I found both dataarray and dataset has method swap_dims, but I don't understand its arguments: dims_dict : dict-like Dictionary whose keys are current dimension names and whose values are new names. Each value must already be a coordinate on this array.

Here is my example:

data = np.random.rand(4,3)
lon = [1,2,3]
lat = [4,3,2,1]
foo = xr.DataArray(data,coords=[lat,lon])
foo
foo = xr.DataArray(data,coords=[lat,lon],dims=['lat','lon'])
foo
foo.swap_dims({'lat':'lon'})

The error message:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-47-c8aa4311b27e> in <module>()
----> 1 foo.swap_dims({'lat':'lon'})

/glade/u/home/che43/miniconda2/lib/python2.7/site-packages/xarray/core/dataarray.pyc in swap_dims(self, dims_dict)
    794         Dataset.swap_dims
    795         """
--> 796         ds = self._to_temp_dataset().swap_dims(dims_dict)
    797         return self._from_temp_dataset(ds)
    798

/glade/u/home/che43/miniconda2/lib/python2.7/site-packages/xarray/core/dataset.pyc in swap_dims(self, dims_dict, inplace)
   1293                 raise ValueError('replacement dimension %r is not a 1D '
   1294                                  'variable along the old dimension %r'
-> 1295                                  % (v, k))
   1296
   1297         result_dims = set(dims_dict.get(dim, dim) for dim in self.dims)

ValueError: replacement dimension 'lon' is not a 1D variable along the old dimension 'lat'

Sorry to bother.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions