[Python] xarray create variables attributes

Discussão em 'Python' iniciado por Stack, Junho 13, 2025.

  1. Stack

    Stack Membro Participativo

    I want to create a dataset with xarray and want to add attributes to variables while creating the dataset. The xarray documentation provides a way of adding global attribute. For example, as below:

    ds = xr.Dataset(
    data_vars=dict(
    'temperature'=(["x", "y", "time"], temperature),
    'precipitation'=(["x", "y", "time"], precipitation),
    ),
    coords=dict(
    lon=(["x", "y"], lon),
    lat=(["x", "y"], lat),
    time=time,
    reference_time=reference_time,
    ),
    attrs=dict(description="Weather related data."),)


    One way to add variable attribute would be some like this:

    ds['temperature'].attrs = {"units": K, '_FillValue': -999}


    But, in my opinion it is more like updating the attribute. Is there a way to directly assign attributes while creating the dataset directly using xr.Dataset ?

    Continue reading...

Compartilhe esta Página