Skip to content

Commit ccc8f99

Browse files
authored
add .imag and .real properties to NamedArray (#8365)
1 parent eb74944 commit ccc8f99

File tree

3 files changed

+35
-33
lines changed

3 files changed

+35
-33
lines changed

xarray/core/variable.py

-22
Original file line numberDiff line numberDiff line change
@@ -2365,28 +2365,6 @@ def notnull(self, keep_attrs: bool | None = None):
23652365
keep_attrs=keep_attrs,
23662366
)
23672367

2368-
@property
2369-
def real(self):
2370-
"""
2371-
The real part of the variable.
2372-
2373-
See Also
2374-
--------
2375-
numpy.ndarray.real
2376-
"""
2377-
return self._replace(data=self.data.real)
2378-
2379-
@property
2380-
def imag(self):
2381-
"""
2382-
The imaginary part of the variable.
2383-
2384-
See Also
2385-
--------
2386-
numpy.ndarray.imag
2387-
"""
2388-
return self._replace(data=self.data.imag)
2389-
23902368
def __array_wrap__(self, obj, context=None):
23912369
return Variable(self.dims, obj)
23922370

xarray/namedarray/core.py

+24-6
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,11 @@
2424
from xarray.namedarray._typing import (
2525
_arrayfunction_or_api,
2626
_chunkedarray,
27-
_DType,
2827
_DType_co,
2928
_ScalarType_co,
3029
_ShapeType_co,
3130
)
32-
from xarray.namedarray.utils import (
33-
_default,
34-
is_duck_dask_array,
35-
to_0d_object_array,
36-
)
31+
from xarray.namedarray.utils import _default, is_duck_dask_array, to_0d_object_array
3732

3833
if TYPE_CHECKING:
3934
from numpy.typing import ArrayLike, NDArray
@@ -46,6 +41,7 @@
4641
_Dim,
4742
_Dims,
4843
_DimsLike,
44+
_DType,
4945
_IntOrUnknown,
5046
_ScalarType,
5147
_Shape,
@@ -516,6 +512,28 @@ def data(self, data: duckarray[Any, _DType_co]) -> None:
516512
self._check_shape(data)
517513
self._data = data
518514

515+
@property
516+
def imag(self) -> Self:
517+
"""
518+
The imaginary part of the array.
519+
520+
See Also
521+
--------
522+
numpy.ndarray.imag
523+
"""
524+
return self._replace(data=self.data.imag) # type: ignore
525+
526+
@property
527+
def real(self) -> Self:
528+
"""
529+
The real part of the array.
530+
531+
See Also
532+
--------
533+
numpy.ndarray.real
534+
"""
535+
return self._replace(data=self.data.real) # type: ignore
536+
519537
def __dask_tokenize__(self) -> Hashable:
520538
# Use v.data, instead of v._data, in order to cope with the wrappers
521539
# around NetCDF and the like

xarray/tests/test_namedarray.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@
88
import pytest
99

1010
from xarray.core.indexing import ExplicitlyIndexed
11-
from xarray.namedarray._typing import (
12-
_arrayfunction_or_api,
13-
_DType_co,
14-
_ShapeType_co,
15-
)
11+
from xarray.namedarray._typing import _arrayfunction_or_api, _DType_co, _ShapeType_co
1612
from xarray.namedarray.core import NamedArray, from_array
1713
from xarray.namedarray.utils import _default
1814

@@ -171,6 +167,16 @@ def test_data(random_inputs: np.ndarray[Any, Any]) -> None:
171167
named_array.data = np.random.random((3, 4)).astype(np.float64)
172168

173169

170+
def test_real_and_imag() -> None:
171+
named_array: NamedArray[Any, Any]
172+
named_array = NamedArray(["x"], np.arange(3) - 1j * np.arange(3))
173+
expected_real = np.arange(3)
174+
assert np.array_equal(named_array.real.data, expected_real)
175+
176+
expected_imag = -np.arange(3)
177+
assert np.array_equal(named_array.imag.data, expected_imag)
178+
179+
174180
# Additional tests as per your original class-based code
175181
@pytest.mark.parametrize(
176182
"data, dtype",

0 commit comments

Comments
 (0)