Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix NamedArray.imag and NamedArray.real typing info #8369

Merged
merged 9 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
Self,
T_DuckArray,
)
from xarray.namedarray._typing import (
_ScalarType,
_ShapeType,
_SupportsImag,
_SupportsReal,
)

NON_NANOSECOND_WARNING = (
"Converting non-nanosecond precision {case} values to nanosecond precision. "
Expand Down Expand Up @@ -2365,6 +2371,32 @@ def notnull(self, keep_attrs: bool | None = None):
keep_attrs=keep_attrs,
)

@property
def imag(
self: Variable[_ShapeType, np.dtype[_SupportsImag[_ScalarType]]]
) -> Variable[_ShapeType, np.dtype[_ScalarType]]:
andersy005 marked this conversation as resolved.
Show resolved Hide resolved
"""
The imaginary part of the variable.

See Also
--------
numpy.ndarray.imag
"""
return self._new(data=self.data.imag)

@property
def real(
self: Variable[_ShapeType, np.dtype[_SupportsReal[_ScalarType]]]
) -> Variable[_ShapeType, np.dtype[_ScalarType]]:
"""
The real part of the variable.

See Also
--------
numpy.ndarray.real
"""
return self._new(data=self.data.real)

def __array_wrap__(self, obj, context=None):
return Variable(self.dims, obj)

Expand Down
14 changes: 10 additions & 4 deletions xarray/namedarray/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
_DType_co,
_ScalarType_co,
_ShapeType_co,
_SupportsImag,
_SupportsReal,
)
from xarray.namedarray.utils import _default, is_duck_dask_array, to_0d_object_array

Expand Down Expand Up @@ -513,26 +515,30 @@ def data(self, data: duckarray[Any, _DType_co]) -> None:
self._data = data

@property
def imag(self) -> Self:
def imag(
self: NamedArray[_ShapeType, np.dtype[_SupportsImag[_ScalarType]]], # type: ignore[type-var]
) -> NamedArray[_ShapeType, np.dtype[_ScalarType]]:
"""
The imaginary part of the array.

See Also
--------
numpy.ndarray.imag
"""
return self._replace(data=self.data.imag) # type: ignore
return self._new(data=self.data.imag)

@property
def real(self) -> Self:
def real(
self: NamedArray[_ShapeType, np.dtype[_SupportsReal[_ScalarType]]], # type: ignore[type-var]
) -> NamedArray[_ShapeType, np.dtype[_ScalarType]]:
"""
The real part of the array.

See Also
--------
numpy.ndarray.real
"""
return self._replace(data=self.data.real) # type: ignore
return self._new(data=self.data.real)

def __dask_tokenize__(self) -> Hashable:
# Use v.data, instead of v._data, in order to cope with the wrappers
Expand Down
24 changes: 18 additions & 6 deletions xarray/tests/test_namedarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,25 @@ def test_data(random_inputs: np.ndarray[Any, Any]) -> None:


def test_real_and_imag() -> None:
named_array: NamedArray[Any, Any]
named_array = NamedArray(["x"], np.arange(3) - 1j * np.arange(3))
expected_real = np.arange(3)
assert np.array_equal(named_array.real.data, expected_real)
expected_real: np.ndarray[Any, np.dtype[np.float64]]
expected_real = np.arange(3, dtype=np.float64)

expected_imag: np.ndarray[Any, np.dtype[np.float64]]
expected_imag = -np.arange(3, dtype=np.float64)

arr: np.ndarray[Any, np.dtype[np.complex128]]
arr = expected_real + 1j * expected_imag

named_array: NamedArray[Any, np.dtype[np.complex128]]
named_array = NamedArray(["x"], arr)

actual_real: np.ndarray[Any, np.dtype[np.float64]] = named_array.real.data
assert np.array_equal(actual_real, expected_real)
assert actual_real.dtype == expected_real.dtype

expected_imag = -np.arange(3)
assert np.array_equal(named_array.imag.data, expected_imag)
actual_imag: np.ndarray[Any, np.dtype[np.float64]] = named_array.imag.data
assert np.array_equal(actual_imag, expected_imag)
assert actual_imag.dtype == expected_imag.dtype


# Additional tests as per your original class-based code
Expand Down
Loading