0

I need to turn a 3-dimensional xarray DataArray into a 1-dimension dataset/dataarray in order to create a cumulative distribution function. I attempted to use this question and answer for reference, but I get an error that states: 'DataArray' object has no attribute 'data_vars.'

This makes me think that the answer is out of date and no longer useable. What would be my best option to collapse this 3D array into a 1D array?

Here's a picture of the data. Unfortunately, I'm unsure of how to upload a netCDF file, so the data is not readily accessible.

dataset in question

Thank you for the help!

Michael Delgado
  • 13,789
  • 3
  • 29
  • 54
SAVen
  • 67
  • 8
  • does [`stack`](https://docs.xarray.dev/en/stable/generated/xarray.DataArray.stack.html) do the trick? and your intuition about the referenced answer isn't right. That answer is just working with a dataset - you need to adapt the answer to work with a DataArray rather than a dataset. – Michael Delgado Aug 18 '22 at 00:04
  • also, [please don't upload images of data, code, or errors](//meta.stackoverflow.com/q/285551/). you could post the result of `print(ds)` as a [formatted code block](//help/formatting). – Michael Delgado Aug 18 '22 at 00:07

1 Answers1

1

You can stack the dimensions of an xarray.DataArray with xarray.DataArray.stack. Passing an ellipsis (...) in a list as the dimensions argument will stack all dimensions to create a 1-D array:

stacked = da.stack(stacked=[...])

if at this point you'd like a numpy 1-D array containing all values, you can access the .values attribute

stacked_values = stacked.values
Michael Delgado
  • 13,789
  • 3
  • 29
  • 54