Skip to content

concat and align flips coordinates order #9777

Open
@alippai

Description

@alippai

What happened?

xr.concat() and xr.align() behavior is based on the values of coordinates, the result is surprising

What did you expect to happen?

Consistent behavior, stable coordinates.

Minimal Complete Verifiable Example

This gives the expected (date, c, d):

import pandas as pd
import xarray as xr

ds1 = pd.DataFrame({"date": [1, 1], "c": [1,2], "d": [3,4], "v":[5,6]}).set_index(['date', 'c', 'd']).to_xarray()
ds2 = pd.DataFrame({"date": [2, 2], "c": [1,2], "d": [3,4], "v":[5,6]}).set_index(['date', 'c', 'd']).to_xarray()
xr.concat([ds1, ds2], dim='date')

But changing one value of c results in (c,d,date) coordinates.

import pandas as pd
import xarray as xr

ds1 = pd.DataFrame({"date": [1, 1], "c": [1,2], "d": [3,4], "v":[5,6]}).set_index(['date', 'c', 'd']).to_xarray()
ds2 = pd.DataFrame({"date": [2, 2], "c": [1,3], "d": [3,4], "v":[5,6]}).set_index(['date', 'c', 'd']).to_xarray()
xr.concat([ds1, ds2], dim='date')

I tried to use xr.align instead, but that's similar.
(date, c, d) is coordinates are created:

import pandas as pd
import xarray as xr

ds1 = pd.DataFrame({"date": [1, 1], "c": [1,2], "d": [3,4], "v":[5,6]}).set_index(['date', 'c', 'd']).to_xarray()
ds2 = pd.DataFrame({"date": [2, 2], "c": [1,2], "d": [3,4], "v":[5,6]}).set_index(['date', 'c', 'd']).to_xarray()
xr.align(ds1, ds2, join='outer', exclude='date')

Changing a coordinate results in coordinate order (c,d,date):

import pandas as pd
import xarray as xr

ds1 = pd.DataFrame({"date": [1, 1], "c": [1,2], "d": [3,4], "v":[5,6]}).set_index(['date', 'c', 'd']).to_xarray()
ds2 = pd.DataFrame({"date": [2, 2], "c": [1,3], "d": [3,4], "v":[5,6]}).set_index(['date', 'c', 'd']).to_xarray()
xr.align(ds1, ds2, join='outer', exclude='date')

MVCE confirmation

  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.
  • Recent environment — the issue occurs with the latest version of xarray and its dependencies.

Relevant log output

No response

Anything else we need to know?

Maybe a good workaround or description in the docs is the solution if fixing this behavior is not straightforward.
Right now I have no idea how to append two disjoint Datasets along an axis. transpose changes DataArray coordinates only.

xr.merge() works, but it's really slow (10x slower than concat or align) for appending un-aligned datasets along an axis.

Environment

python: 3.10.14 | packaged by conda-forge OS: Linux xarray: 2024.9.0 pandas: 2.2.2 numpy: 1.26.4

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions