Use sparse-array as backend.
(
self,
sparse_format: Literal["coo"] | Default = _default,
fill_value: ArrayLike | Default = _default,
)
| 965 | return formatting_html.array_repr(self) |
| 966 | |
| 967 | def _as_sparse( |
| 968 | self, |
| 969 | sparse_format: Literal["coo"] | Default = _default, |
| 970 | fill_value: ArrayLike | Default = _default, |
| 971 | ) -> NamedArray[Any, _DType_co]: |
| 972 | """ |
| 973 | Use sparse-array as backend. |
| 974 | """ |
| 975 | import sparse |
| 976 | |
| 977 | from xarray.namedarray._array_api import astype |
| 978 | |
| 979 | # TODO: what to do if dask-backended? |
| 980 | if fill_value is _default: |
| 981 | dtype, fill_value = dtypes.maybe_promote(self.dtype) |
| 982 | else: |
| 983 | dtype = dtypes.result_type(self.dtype, fill_value) |
| 984 | |
| 985 | if sparse_format is _default: |
| 986 | sparse_format = "coo" |
| 987 | try: |
| 988 | as_sparse = getattr(sparse, f"as_{sparse_format.lower()}") |
| 989 | except AttributeError as exc: |
| 990 | raise ValueError(f"{sparse_format} is not a valid sparse format") from exc |
| 991 | |
| 992 | data = as_sparse(astype(self, dtype).data, fill_value=fill_value) |
| 993 | return self._new(data=data) |
| 994 | |
| 995 | def _to_dense(self) -> NamedArray[Any, _DType_co]: |
| 996 | """ |