Skip to content

Merging two datasets using xarray.combine_nested goes wrong #8553

@HJA24

Description

@HJA24

What is your issue?

Hi, I want to merge two xarray.Dataset-objects. I am using version 2023.5.0.

My code looks as follows:

import numpy as np
import xarray as xr

N_CHAINS = 4
N_DRAWS = 1000
N_PLAYERS = 5

player_idx = [1, 1, 2, 3, 4, 4, 0, 0, 2, 2]
opponent_idx = [0, 3, 1, 4, 1, 1, 1, 4, 3, 3]

h2h_idx = pd.MultiIndex.from_tuples(
    tuple(zip(player_idx, opponent_idx)), names=('player_id', 'opponent_id')
)

obs = xr.Dataset(
    data_vars=dict(
        n_points_won=(['h2h_id'], np.array([11, 11, 8, 9, 4, 11, 7, 11, 11, 11])),
        n_points_lost=(['h2h_id'], np.array([9, 9, 11, 11, 11, 1, 11, 2, 3, 6])),
    ),
    coords=dict(
        h2h_id=(['h2h_id'], h2h_idx),
    )
)

alpha = np.random.rand(N_CHAINS, N_DRAWS, N_PLAYERS, N_PLAYERS) * 100
beta = np.random.rand(N_CHAINS, N_DRAWS, N_PLAYERS, N_PLAYERS) * 100

pos = xr.Dataset(
    data_vars=dict(
        alpha=(['chain', 'draw', 'player_id', 'opponent_id'], alpha),
        beta=(['chain', 'draw', 'player_id', 'opponent_id'], beta),
    ),
    coords=dict(
        chain=(['chain'], list(range(N_CHAINS))),
        draw=(['draw'], list(range(N_DRAWS))),
        player_id=(['player_id'], list(range(N_PLAYERS))),
        oppponent_id=(['opponent_id'], list(range(N_PLAYERS))),
    ),
)
 

combined = xr.combine_nested([obs, pos], concat_dim=['player_id', 'opponent_id']) # this results in an error

I am getting the following error:

ValueError: concat_dims has length 2 but the datasets passed are nested in a 1-dimensional structure

How can I merge the two datasets?
The goal is to create a xarray.Dataset with coordinates h2h_id, player_id, opponent_id, chain and draw. Basically for each data variable in obs I want to concat the corresponding alpha- and beta values from pos of that particular observation (through player_id and opponent_id)

Thanks in advance

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageIssue that has not been reviewed by xarray team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions