Skip to content

concat along dim with mix of scalar coordinate and array coordinates is not right #6434

Closed
@dcherian

Description

@dcherian

What happened?

Really hard to describe in words =)

concat = xr.concat([da.isel(time=0), da.isel(time=[1])], dim="time")
xr.align(concat, da, dim="time")

fails when concat and da should be identical. This is causing failures in cf-xarray:xarray-contrib/cf-xarray#319

cc @benbovy

What did you expect to happen?

No response

Minimal Complete Verifiable Example

import numpy as np
import xarray as xr

time = xr.DataArray(
    np.array(
        ["2013-01-01T00:00:00.000000000", "2013-01-01T06:00:00.000000000"],
        dtype="datetime64[ns]",
    ),
    dims="time",
    name="time",
)

da = time
concat = xr.concat([da.isel(time=0), da.isel(time=[1])], dim="time")
xr.align(da, concat, join="exact")  #  works

da = xr.DataArray(np.ones(time.shape), dims="time", coords={"time": time})
concat = xr.concat([da.isel(time=0), da.isel(time=[1])], dim="time")
xr.align(da, concat, join="exact")

Relevant log output

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [27], in <module>
     17 da = xr.DataArray(np.ones(time.shape), dims="time", coords={"time": time})
     18 concat = xr.concat([da.isel(time=0), da.isel(time=[1])], dim="time")
---> 19 xr.align(da, concat, join="exact")

File ~/work/python/xarray/xarray/core/alignment.py:761, in align(join, copy, indexes, exclude, fill_value, *objects)
    566 """
    567 Given any number of Dataset and/or DataArray objects, returns new
    568 objects with aligned indexes and dimension sizes.
   (...)
    751 
    752 """
    753 aligner = Aligner(
    754     objects,
    755     join=join,
   (...)
    759     fill_value=fill_value,
    760 )
--> 761 aligner.align()
    762 return aligner.results

File ~/work/python/xarray/xarray/core/alignment.py:549, in Aligner.align(self)
    547 self.find_matching_unindexed_dims()
    548 self.assert_no_index_conflict()
--> 549 self.align_indexes()
    550 self.assert_unindexed_dim_sizes_equal()
    552 if self.join == "override":

File ~/work/python/xarray/xarray/core/alignment.py:395, in Aligner.align_indexes(self)
    393 if need_reindex:
    394     if self.join == "exact":
--> 395         raise ValueError(
    396             "cannot align objects with join='exact' where "
    397             "index/labels/sizes are not equal along "
    398             "these coordinates (dimensions): "
    399             + ", ".join(f"{name!r} {dims!r}" for name, dims in key[0])
    400         )
    401     joiner = self._get_index_joiner(index_cls)
    402     joined_index = joiner(matching_indexes)

ValueError: cannot align objects with join='exact' where index/labels/sizes are not equal along these coordinates (dimensions): 'time' ('time',)

Anything else we need to know?

No response

Environment

xarray main

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions