Skip to content

Order of Dimensions when converting DataArray to DataFrame back to DataArray. #6915

Answered by rhkleijn
benHeid asked this question in Q&A
Discussion options

You must be logged in to vote

It is easily overlooked, but the documentation of to_pandas() mentions: The DataArray constructor performs the inverse transformation.

With that approach roundtripping indeed seems to work correctly for your example:

import xarray as xr
da = xr.DataArray([[1, 3], [4, 7], [0.5, 2], [-3, -3 / 7]], dims=["index", "columns"], coords={"index": [0,1,2,3], "columns":["a", "b"]})
df = da.to_pandas()
xr.DataArray(df).identical(da)
Out[1]: True

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@benHeid
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by benHeid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
3 participants