29
29
_DType_co ,
30
30
_ScalarType_co ,
31
31
_ShapeType_co ,
32
+ _sparsearrayfunction_or_api ,
32
33
_SupportsImag ,
33
34
_SupportsReal ,
34
35
)
@@ -810,9 +811,9 @@ def _as_sparse(
810
811
self ,
811
812
sparse_format : Literal ["coo" ] | Default = _default ,
812
813
fill_value : ArrayLike | Default = _default ,
813
- ) -> Self :
814
+ ) -> NamedArray [ Any , _DType_co ] :
814
815
"""
815
- use sparse-array as backend.
816
+ Use sparse-array as backend.
816
817
"""
817
818
import sparse
818
819
@@ -832,18 +833,15 @@ def _as_sparse(
832
833
raise ValueError (f"{ sparse_format } is not a valid sparse format" ) from exc
833
834
834
835
data = as_sparse (astype (self , dtype ).data , fill_value = fill_value )
835
- return self ._replace (data = data )
836
+ return self ._new (data = data )
836
837
837
- def _to_dense (self ) -> Self :
838
+ def _to_dense (self ) -> NamedArray [ Any , _DType_co ] :
838
839
"""
839
- Change backend from sparse to np.array
840
+ Change backend from sparse to np.array.
840
841
"""
841
- from xarray .namedarray ._typing import _sparsearrayfunction_or_api
842
-
843
842
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 )
847
845
else :
848
846
raise TypeError ("self.data is not a sparse array" )
849
847
0 commit comments