question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Transpose some but not all dimensions

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:17 (12 by maintainers)

github_iconTop GitHub Comments

4reactions
crusaderkycommented, Oct 10, 2019

From personal experience I find that 99% of the time, I want to push some known dimensions either to the front or to the back of the array while I don’t care about the order of the others. I’d love to have this syntax:

transpose(..., "x", "y")

or

transpose("x", "y", ...)

where the ellipsis expands to all dimensions not explicitly listed, in their original order. There can be at most one ellipsis.

1reaction
shoyercommented, Oct 21, 2019

There’s one edge case that might be worth thinking carefully about here: Consider a dataset with two variables with dimensions ('w', 'x', 'y', 'z') and ('x', 'w', 'y', 'z'). Now we write .transpose(..., 'z', 'y'). What should the dimensions of variables on the resulting dataset be?

  1. Both ('w', 'x', 'z', 'y'), with ... filled in based on the order of dimensions in the overall dataset.
  2. ('w', 'x', 'y', 'z') and ('x', 'w', 'y', 'z'), with ... filled in for each variable separately.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Transposing dimensions after a reshape: when it is required?
The problem I see is when X_SIZE == T_SIZE reshape does not have any way to know how to arrange B data in...
Read more >
Transpose - Wikipedia
In linear algebra, the transpose of a matrix is an operator which flips a matrix over its diagonal; that is, it switches the...
Read more >
How to Quickly Transpose Data in Excel (Step-by-Step Guide)
In this tutorial, you'll learn the techniques to transpose data in Excel. You can use the Paste Special option, Transpose Function or a...
Read more >
Transpose R matrix object: Tips and Tricks - Data Analytics
Two dimensional R objects include data.frame, matrix and table objects. You can transpose the rows and columns using the t() command.
Read more >
Transpose of a matrix (video) - Khan Academy
What is that going to be equal to? Well to go from C to C transpose, we switched all the rows and the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found