Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A way to stack all dimensions but one? #8278

Closed
carschandler opened this issue Oct 6, 2023 · 3 comments
Closed

A way to stack all dimensions but one? #8278

carschandler opened this issue Oct 6, 2023 · 3 comments

Comments

@carschandler
Copy link
Contributor

carschandler commented Oct 6, 2023

Is your feature request related to a problem?

One thing I find useful with DataArrays is the ability to take all dimensions but one, stack them into one MultiIndex, and then iterate over the entries of that MultiIndex. For example, I have a DataArray that starts with a time dimension/coordinate. I then add several new dimensions/coords that correspond to varying input parameters (i.e. one for initial velocity, initial position, air density, etc.). What I then want to do is iterate over all 1D time vectors (all the combinations of those variable input parameters). The easiest way I've found to do this is to stack all the dimensions except time and then iterate over the resulting MultiIndex. I have been doing this by converting the dims of the DataArray to a list and then removing "time" from that list and using it as the input to stack.

Describe the solution you'd like

It would be awesome if there was a method that could specifically stack all dimensions except for a list of specified dimension names (something like DataArray.stack_except({"name_of_created_stacked_dimension": ["dim_to_leave_unstacked", "another_dim"])), or some kind of syntax that would allow an "all except" behavior to be passed directly into stack (could be something like `DataArray.stack({"name_of_created_stacked_dimension"}: ["dim_to_leave_unstacked"], invert=True).

Describe alternatives you've considered

If there is a better way to achieve what I'm trying to accomplish (take one dimension and iterate over all the combinations of the other dimensions, resulting in 1D vectors that vary along the single desired dimension), then I would welcome any tips!

Additional context

No response

@welcome
Copy link

welcome bot commented Oct 6, 2023

Thanks for opening your first issue here at xarray! Be sure to follow the issue template!
If you have an idea for a solution, we would really welcome a Pull Request with proposed changes.
See the Contributing Guide for more.
It may take us a while to respond here, but we really value your contribution. Contributors like you help make xarray better.
Thank you!

@mathause
Copy link
Collaborator

mathause commented Oct 6, 2023

You can try the Dataset.to_stacked_array method. This will only work for Dataset and stack all data vars on the Dataset. However, if you start from one DataArray (and call to_dataset() on it), this may do what you want.

@carschandler
Copy link
Contributor Author

carschandler commented Oct 6, 2023

@mathause that actually does exactly what I'm looking for (I'm working with a Dataset anyway). There goes my work I started to add a PR for the invert=True kwarg on stack() lol. This is great though... I may add a quick PR to add this method to DataArray since most of them use _to_temp_dataset() anyway.

Thanks for the tip!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants