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

Test masked array #8396

Merged
merged 5 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 6 additions & 7 deletions xarray/namedarray/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@

from xarray.core.types import Dims
from xarray.namedarray._typing import (
DuckArray,
_AttrsLike,
_Chunks,
_Dim,
Expand Down Expand Up @@ -144,9 +143,9 @@ def _new(
@overload
def from_array(
dims: _DimsLike,
data: DuckArray[_ScalarType],
data: duckarray[_ShapeType, _DType],
attrs: _AttrsLike = ...,
) -> _NamedArray[_ScalarType]:
) -> NamedArray[_ShapeType, _DType]:
...


Expand All @@ -155,15 +154,15 @@ def from_array(
dims: _DimsLike,
data: ArrayLike,
attrs: _AttrsLike = ...,
) -> _NamedArray[Any]:
) -> NamedArray[Any, Any]:
...


def from_array(
dims: _DimsLike,
data: DuckArray[_ScalarType] | ArrayLike,
data: duckarray[_ShapeType, _DType] | ArrayLike,
attrs: _AttrsLike = None,
) -> _NamedArray[_ScalarType] | _NamedArray[Any]:
) -> NamedArray[_ShapeType, _DType] | NamedArray[Any, Any]:
"""
Create a Named array from an array-like object.

Expand All @@ -184,7 +183,7 @@ def from_array(
"Array is already a Named array. Use 'data.data' to retrieve the data array"
)

# TODO: dask.array.ma.masked_array also exists, better way?
# TODO: dask.array.ma.MaskedArray also exists, better way?
if isinstance(data, np.ma.MaskedArray):
mask = np.ma.getmaskarray(data) # type: ignore[no-untyped-call]
if mask.any():
Expand Down
8 changes: 6 additions & 2 deletions xarray/tests/test_namedarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,14 @@ def test_duck_array_typevar(a: duckarray[Any, _DType]) -> duckarray[Any, _DType]

numpy_a: NDArray[np.int64]
numpy_a = np.array([2.1, 4], dtype=np.dtype(np.int64))
test_duck_array_typevar(numpy_a)

masked_a: np.ma.MaskedArray[Any, np.dtype[np.int64]]
masked_a = np.ma.asarray([2.1, 4], dtype=np.dtype(np.int64)) # type: ignore[no-untyped-call]
test_duck_array_typevar(masked_a)

custom_a: CustomArrayIndexable[Any, np.dtype[np.int64]]
custom_a = CustomArrayIndexable(numpy_a)

test_duck_array_typevar(numpy_a)
test_duck_array_typevar(custom_a)

# Test numpy's array api:
Expand Down
Loading