[Python] What is the difference between an xarray Dataset and a DataArray? [closed]

Discussão em 'Python' iniciado por Stack, Abril 2, 2025.

  1. Stack

    Stack Membro Participativo

    According to the xarray documentation:


    xarray.DataArray is xarray’s implementation of a labeled, multi-dimensional array.
    ​

    To me, this means a DataArray is just numpy with labels. However, xarray provides a second data structure, called Dataset, whose documentation is


    xarray.Dataset is xarray’s multi-dimensional equivalent of a DataFrame.
    ​

    Since a Pandas DataFrame is just a 2D numpy array, with labels, can't I still use a DataArray instead? In fact, two DataArray like

    import numpy as np
    import xarray as xr

    foo = xr.DataArray(
    data=np.zeros((4, 3)),
    coords={
    "x": np.arange(4),
    "y": np.arange(3),
    })

    bar = xr.DataArray(
    data=np.zeros((4, 3)),
    coords={
    "x": np.arange(4),
    "y": np.arange(3),
    })


    can be grouped together using a string coordinate

    da = xr.DataArray(
    data=np.zeros((4, 3, 2)),
    coords={
    "x": np.arange(4),
    "y": np.arange(3),
    "key": ["foo", "bar"],
    })


    The second part of the Dataset documentation is a little clearer


    [a Dataset] is a dict-like container of labeled arrays (DataArray objects) with aligned dimensions.
    ​

    However, from the examples in the documentation it appears that some of the Dataset usage can be perfectly covered by a DataArray.

    Is there any other difference that I am missing?

    Continue reading...

Compartilhe esta Página