Skip to content

swap_dims drops coordinate units #361

Description

@simonkeys

Using swap_dims on a DataArray with quantified units drops the units on the new dimension. Dequantifying and then quantifying preserves the units:

import xarray as xr
import pint_xarray

da = xr.DataArray(
    [1, 2, 3], 
    dims=['foo'], 
    coords={
        "foo": ('foo', [1,2,3], {"units": "s"}), 
        "bar": ('foo', [4,5,6], {"units": "kg"})
    },
    attrs={"units": 'm'}
).pint.quantify()

[
    da.bar.pint.units,
    da.swap_dims(foo='bar').bar.pint.units,
    da.pint.dequantify().swap_dims(foo='bar').pint.quantify().bar.pint.units
]

Returns:

[<Unit('kilogram')>, None, <Unit('kilogram')>]

xarray 2026.1.0, pint-xarray 0.6.0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions