pydata / pydata/xarray

Feature Request: Conversion Utilities for Complex Data – Dtype & Dimension Transformations

Open
#10,213 16 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

Is your feature request related to a problem?

Description:
I’d like to propose adding utility functions to xarray to convert complex-valued DataArrays into a real-valued form with a separate dimension for real and imaginary parts, and back. Since netCDF doesn't support complex numbers yet, this would offer a practical workaround for saving, plotting, and processing complex data using existing xarray tools.

Proposed Approaches:

  1. Using “to_”/“from_” Naming:

    • to_complex_dim: Converts a complex dtype DataArray to a new DataArray with an extra dimension (e.g., "complex") that holds the real and imaginary components. Something in the style of:
      def to_complex_dim(da: xr.DataArray, dim: str = "complex", labels: list = ["Real", "Imag"]) -> xr.DataArray:
          if not np.iscomplexobj(da.data):
              raise ValueError("DataArray must be complex-valued.")
          return xr.DataArray(
              np.stack([da.real, da.imag], axis=-1),
              dims=da.dims + (dim,),
              coords={**da.coords, dim: labels},
              attrs=da.attrs,
          )
      
    • to_complex_dtype: Reconstructs a complex dtype DataArray from a DataArray that contains real and imaginary parts along a dedicated dimension.
      def to_complex_dtype(da: xr.DataArray, dim: str = "complex", labels: list = ["Real", "Imag"]) -> xr.DataArray:
          return da.sel({dim: labels[0]}) + 1j * da.sel({dim: labels[1]})
      
  2. Using “pack”/“unpack” Naming:

    • pack_complex: “Packs” a real/imaginary DataArray (with an extra dimension) back into a complex dtype DataArray.
    • unpack_complex: “Unpacks” the real and imaginary parts from a complex dtype DataArray into separate values along a new dimension.
  3. Accessor-Based Integration:
    Alternatively, these functions could be incorporated as a DataArray accessor (?)

Would love to hear your thoughts on this.

Describe the solution you'd like

No response

Describe alternatives you've considered

No response

Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with issue #10213 and compare the proposed to_complex_dim/to_complex_dtype and pack_complex/unpack_complex APIs, including the possible DataArray accessor. Before implementation, clarify naming, dimension labels, validation, metadata, and the expected complex round trip; done should be a decided API with agreed behavior and tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
data
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.