# How can I merge two YAXarrays datasets

**URL:** https://discourse.julialang.org/t/how-can-i-merge-two-yaxarrays-datasets/133326
**Category:** New to Julia
**Created:** [October 21, 2025, 9:34am UTC](https://discourse.julialang.org/t/how-can-i-merge-two-yaxarrays-datasets/133326 "2025-10-21T09:34:22Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Kamil\_Mroz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kamil_mroz/32/218009_2.png) [@Kamil\_Mroz](https://discourse.julialang.org/u/Kamil_Mroz)
#### Post date: [October 21, 2025, 9:34am UTC](https://discourse.julialang.org/t/how-can-i-merge-two-yaxarrays-datasets/133326/1 "2025-10-21T09:34:22Z")

</div>

I’ve created two YAXArrays from data stored in two separate groups of an HDF5 file.  
They share the same dimensions, and I’d like to merge them into a single Dataset.

Is there a recommended way to do this?  
I couldn’t find any documentation on combining multiple datasets in YAXArrays.jl.

I tried creating a dictionary to combine the two datasets and then converting it back into a Dataset, but I wasn’t successful — mainly because I’m not sure how to iterate over the variables in a Dataset.

```julia-auto
function merge_datasets(ds_list::Vector{Dataset})
    datasets = Dict{Symbol, YAXArray}()

    for ds in ds_list
        for name in Variables(ds)
            datasets[name] = ds[name]
        end
    end

    return Dataset(; properties = Dict{String, Any}(), datasets...)
end

```

---

<div class="post-metadata">

### Author: ![fabiangans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fabiangans/32/2624_2.png) [@fabiangans](https://discourse.julialang.org/u/fabiangans)
#### Post date: [October 21, 2025, 1:24pm UTC](https://discourse.julialang.org/t/how-can-i-merge-two-yaxarrays-datasets/133326/2 "2025-10-21T13:24:25Z")

</div>

You are right, there is currently no good documented way to loop over the elements of a Dataset. My quick solution would be this one:

```julia
using YAXArrays
dataset1 = Dataset(a = YAXArray((lon(1:10),lat(1:5)),zeros(10,5)))
dataset2 = Dataset(b = YAXArray((lon(1:10),lat(1:5)),zeros(10,5)))

Dataset(;dataset1.cubes...,dataset2.cubes...)

```

---

<div class="post-metadata">

### Author: ![Kamil\_Mroz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kamil_mroz/32/218009_2.png) [@Kamil\_Mroz](https://discourse.julialang.org/u/Kamil_Mroz)
#### Post date: [October 21, 2025, 5:43pm UTC](https://discourse.julialang.org/t/how-can-i-merge-two-yaxarrays-datasets/133326/3 "2025-10-21T17:43:38Z")

</div>

Thank you for your help. I have another question. In Python’s xarray, you can create a new variable inside a Dataset using:

```julia-auto
dataset["c"] = dataset["a"] ** 2

```

How can I do the equivalent in Julia using YAXArrays.jl ? or I need to do:  
`ds = Dataset(; ds.cubes..., c =ds["a"].^2)`? It doesn’t feel natural.

---

<div class="post-metadata">

### Author: ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)
#### Post date: [October 23, 2025, 1:43pm UTC](https://discourse.julialang.org/t/how-can-i-merge-two-yaxarrays-datasets/133326/4 "2025-10-23T13:43:45Z")

</div>

Not sure if there’s a more elegant way, but

```julia
dataset.cubes[:c] = map(t -> t^2, dataset["a"])

```

seems to work.
