to_array to create a dimension as last axis
See original GitHub issueIs your feature request related to a problem?
I do ds.to_array(dim="variable").transpose("latitude", "longitude", "variable")
. I would like to avoid the extra transpose call.
Describe the solution you’d like
ds.to_array(dim="variable", new_axis="last")
where new_axis
is Literal["first", "last"] = "first"
or
ds.to_array(dim="variable", new_axis=0)
where new_axis
is Literal[0, -1] = 0
code to change: https://github.com/pydata/xarray/blob/main/xarray/core/dataset.py#L5770
Describe alternatives you’ve considered
No response
Additional context
I imagine new_axis
could be of type int to place the new axis where you would like but the proposal above may be a good first step.
For reference, i’m doing deep learning and want to shape the data as width (latitude), height (longitude), channel (feature)
Issue Analytics
- State:
- Created a year ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Numpy array with elements of different last axis dimensions
One way would be to first create an empty array of objects and then fill in the values. z = np.empty(2, dtype=object) z[0]...
Read more >Add new dimensions to ndarray (np.newaxis, np.expand_dims)
You can add new dimensions to a NumPy array ndarray (= unsqueeze a NumPy array) with np.newaxis , np.expand_dims() and np.reshape() (or reshape ......
Read more >numpy.expand_dims — NumPy v1.24 Manual
Insert a new axis that will appear at the axis position in the expanded array shape. Parameters: aarray_like. Input array. axisint or tuple...
Read more >Numpy Axes, Explained - Sharp Sight
An image that shows that NumPy arrays have axes. In a 2-dimensional NumPy array, the axes are the directions along the rows and...
Read more >Change the dimension of a NumPy array - GeeksforGeeks
Let's discuss how to change the dimensions of an array. In NumPy, this can be achieved in many ways. Let's discuss each of...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
What’s the advantage of this over the transpose call?
The transpose approach is more modular, since it takes advantage of the orthogonality of the methods — i.e. we have one method for all usages, rather than kwargs in lots of methods.
We’ve also managed to have
axis
as a hidden implentation detail, and this would start exposing it.Haha, us both Ray!
I do think that part of laziness is not having to remember too much — so hopefully I can trade you the costs of an extra method call for more consistency!