Skip to content

Commit

Permalink
Test masked array (#8396)
Browse files Browse the repository at this point in the history
* Test masked array

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update test_namedarray.py

* Update core.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
Illviljan and pre-commit-ci[bot] authored Nov 3, 2023
1 parent 10f2aa1 commit 83fbcf0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
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

0 comments on commit 83fbcf0

Please sign in to comment.