Skip to content

Commit 8a2f29b

Browse files
Fix sparse typing (#8387)
* Fix sparse typing * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update variable.py * typos * Update variable.py * Update variable.py * Update variable.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update variable.py * Update variable.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent cfe4d71 commit 8a2f29b

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

xarray/core/variable.py

+22
Original file line numberDiff line numberDiff line change
@@ -2595,6 +2595,28 @@ def argmax(
25952595
"""
25962596
return self._unravel_argminmax("argmax", dim, axis, keep_attrs, skipna)
25972597

2598+
def _as_sparse(self, sparse_format=_default, fill_value=_default) -> Variable:
2599+
"""
2600+
Use sparse-array as backend.
2601+
"""
2602+
from xarray.namedarray.utils import _default as _default_named
2603+
2604+
if sparse_format is _default:
2605+
sparse_format = _default_named
2606+
2607+
if fill_value is _default:
2608+
fill_value = _default_named
2609+
2610+
out = super()._as_sparse(sparse_format, fill_value)
2611+
return cast("Variable", out)
2612+
2613+
def _to_dense(self) -> Variable:
2614+
"""
2615+
Change backend from sparse to np.array.
2616+
"""
2617+
out = super()._to_dense()
2618+
return cast("Variable", out)
2619+
25982620

25992621
class IndexVariable(Variable):
26002622
"""Wrapper for accommodating a pandas.Index in an xarray.Variable.

xarray/namedarray/_typing.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ class _sparsearrayfunction(
248248
Corresponds to np.ndarray.
249249
"""
250250

251-
def todense(self) -> NDArray[_ScalarType_co]:
251+
def todense(self) -> np.ndarray[Any, _DType_co]:
252252
...
253253

254254

@@ -262,9 +262,14 @@ class _sparsearrayapi(
262262
Corresponds to np.ndarray.
263263
"""
264264

265-
def todense(self) -> NDArray[_ScalarType_co]:
265+
def todense(self) -> np.ndarray[Any, _DType_co]:
266266
...
267267

268268

269269
# NamedArray can most likely use both __array_function__ and __array_namespace__:
270270
_sparsearrayfunction_or_api = (_sparsearrayfunction, _sparsearrayapi)
271+
272+
sparseduckarray = Union[
273+
_sparsearrayfunction[_ShapeType_co, _DType_co],
274+
_sparsearrayapi[_ShapeType_co, _DType_co],
275+
]

xarray/namedarray/core.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
_DType_co,
3030
_ScalarType_co,
3131
_ShapeType_co,
32+
_sparsearrayfunction_or_api,
3233
_SupportsImag,
3334
_SupportsReal,
3435
)
@@ -810,9 +811,9 @@ def _as_sparse(
810811
self,
811812
sparse_format: Literal["coo"] | Default = _default,
812813
fill_value: ArrayLike | Default = _default,
813-
) -> Self:
814+
) -> NamedArray[Any, _DType_co]:
814815
"""
815-
use sparse-array as backend.
816+
Use sparse-array as backend.
816817
"""
817818
import sparse
818819

@@ -832,18 +833,15 @@ def _as_sparse(
832833
raise ValueError(f"{sparse_format} is not a valid sparse format") from exc
833834

834835
data = as_sparse(astype(self, dtype).data, fill_value=fill_value)
835-
return self._replace(data=data)
836+
return self._new(data=data)
836837

837-
def _to_dense(self) -> Self:
838+
def _to_dense(self) -> NamedArray[Any, _DType_co]:
838839
"""
839-
Change backend from sparse to np.array
840+
Change backend from sparse to np.array.
840841
"""
841-
from xarray.namedarray._typing import _sparsearrayfunction_or_api
842-
843842
if isinstance(self._data, _sparsearrayfunction_or_api):
844-
# return self._replace(data=self._data.todense())
845-
data_: np.ndarray[Any, Any] = self._data.todense()
846-
return self._replace(data=data_)
843+
data_dense: np.ndarray[Any, _DType_co] = self._data.todense()
844+
return self._new(data=data_dense)
847845
else:
848846
raise TypeError("self.data is not a sparse array")
849847

0 commit comments

Comments
 (0)