Skip to content

auto_complex appears to be broken for DataTrees #10631

@EmDash00

Description

@EmDash00

What happened?

Running the example code in the documentation with a DataTree appears to be broken. The data is read back in encoded form rather than turned automatically into a complex number.

What did you expect to happen?

The datatree's datasets should respect auto_complex and read back the encoded data as complex numbers.

Minimal Complete Verifiable Example

import xarray as xr
import numpy as np

data = np.array([[1 + 2j, 3 + 4j], [5 + 6j, 7 + 8j]])
dt2 = xr.DataTree(
    xr.Dataset({'data': xr.DataArray(
        data,
        dims=["x", "y"],
        coords={"x": ["a", "b"], "y": [1, 2]},
        name="complex_nums",
    )})
)

# write the data to disk
dt2.to_netcdf("complex_nums_nc4.nc", engine="netcdf4", auto_complex=True)
# read the file back into memory
with xr.open_datatree(
    "complex_nums_nc4.nc", engine="netcdf4", auto_complex=True
) as dt_nc4:

    print(dt_nc4['data'].dtype)
   # prints: {'names': ['r', 'i'], 'formats': ['<f8', '<f8'], 'offsets': [0, 8], 'itemsize': 16, 'aligned': True}

MVCE confirmation

  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.
  • Recent environment — the issue occurs with the latest version of xarray and its dependencies.

Relevant log output

Anything else we need to know?

No response

Environment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions