Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Merging series of 2D DataFrames to 3D xarray

I have a series of 2D DataFrames that should be merged into a 3D xarray. The structure of DataFrames looks like this:

5 6 4 8 -1 3 angle
5 105.87 459.62 0.1
10 211.74 919.24 0.1
5 6 4 8 -1 3 angle
5 125.87 439.62 0.2
10 241.74 949.24 0.2

My goal is to have a 3D xarray that will have such structure:

Dimensions:  (xyz: 2, thickness: 2, angle: 2)
Coordinates:
  * xyz       (xyz) object '5 6 4' '8 -1 3'
  * thickness (thickness) int 5 10
  * angle     (angle) float64 0.1 0.2
Data variables:
    I don't know how the variables should be sorted

For now I changed DataFrames into xarrays in such a manner:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

xa = xarray.Dataset.from_dataframe(df).set_coords("angle")

The 2D xarrays look like this:

Dimensions:    (thickness: 2)
Coordinates:
  * thickness  (thickness) int 5 10
    angles     (thickness) float64 0.1 0.1
Data variables:
    5 6 4      (thickness) float64 105.87 211.74
    8 -1 3     (thickness) float64 459.62 919.24

Then when I try to merge the xarrays with .merge, I got an error MergeError: conflicting values for variable '0 0 0 ' on objects to be combined. You can skip this check by specifying compat='override'.

I wanted to know:

  1. How to turn angles into a dimension? Seems that it’s something different than coordinates.
  2. How to make this list of xyz coordinates (‘5 6 4’, ‘8 -1 3’) into another dimension called ‘xyz’?

>Solution :

Pandas MultiIndex levels become xarray dimensions and coordinates. DataFrame columns become Dataset variables; series are converted to DataArrays.

So the key is to arrange your data properly in pandas first. You could do this with a number of the pandas reshaping methods, but here’s one:

da = (
    df.rename_axis("thickness")
    .set_index("angle", append =True)
    .rename_axis("xyz", axis=1)
    .stack("xyz")
    .to_xarray()
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading