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.

to_array to create a dimension as last axis

See original GitHub issue

Is 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:closed
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
max-sixtycommented, Jul 6, 2022

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.

0reactions
max-sixtycommented, Jul 6, 2022

I would say very little. I’m a lazy programmer.

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!

Read more comments on GitHub >

github_iconTop 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 >

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