@@ -382,19 +382,19 @@ shape: {self.shape}"""
382
382
@staticmethod
383
383
def from_scipy (obj , dim_names = None ):
384
384
"""
385
- Convert scipy.sparse.coo_matrix to arrow::SparseCOOTensor
385
+ Convert scipy.sparse.coo_array or scipy.sparse. coo_matrix to arrow::SparseCOOTensor
386
386
387
387
Parameters
388
388
----------
389
- obj : scipy.sparse.csr_matrix
390
- The scipy matrix that should be converted.
389
+ obj : scipy.sparse.coo_array or scipy.sparse.coo_matrix
390
+ The scipy array or matrix that should be converted.
391
391
dim_names : list, optional
392
392
Names of the dimensions.
393
393
"""
394
394
import scipy.sparse
395
- if not isinstance (obj, scipy.sparse.coo_matrix):
395
+ if not ( isinstance (obj, scipy.sparse.coo_array) or isinstance (obj, scipy.sparse. coo_matrix) ):
396
396
raise TypeError (
397
- f" Expected scipy.sparse.coo_matrix, got {type(obj)}" )
397
+ f" Expected scipy.sparse.coo_array or scipy.sparse. coo_matrix, got {type(obj)}" )
398
398
399
399
cdef shared_ptr[CSparseCOOTensor] csparse_tensor
400
400
cdef vector[int64_t] c_shape
@@ -409,10 +409,11 @@ shape: {self.shape}"""
409
409
row = obj.row
410
410
col = obj.col
411
411
412
- # When SciPy's coo_matrix has canonical format, its indices matrix is
413
- # sorted in column-major order. As Arrow's SparseCOOIndex is sorted
414
- # in row-major order if it is canonical, we must sort indices matrix
415
- # into row-major order to keep its canonicalness, here.
412
+ # When SciPy's coo_array and coo_matrix have canonical format, their
413
+ # indices matrix is sorted in column-major order. As Arrow's
414
+ # SparseCOOIndex is sorted in row-major order if it is canonical,
415
+ # we must sort indices matrix into row-major order to keep it's
416
+ # canonicalness here.
416
417
if obj.has_canonical_format:
417
418
order = np.lexsort((col, row)) # sort in row-major order
418
419
row = row[order]
@@ -493,9 +494,9 @@ shape: {self.shape}"""
493
494
494
495
def to_scipy (self ):
495
496
"""
496
- Convert arrow::SparseCOOTensor to scipy.sparse.coo_matrix .
497
+ Convert arrow::SparseCOOTensor to scipy.sparse.coo_array .
497
498
"""
498
- from scipy.sparse import coo_matrix
499
+ from scipy.sparse import coo_array
499
500
cdef PyObject* out_data
500
501
cdef PyObject* out_coords
501
502
@@ -504,12 +505,12 @@ shape: {self.shape}"""
504
505
data = PyObject_to_object(out_data)
505
506
coords = PyObject_to_object(out_coords)
506
507
row, col = coords[:, 0 ], coords[:, 1 ]
507
- result = coo_matrix ((data[:, 0 ], (row, col)), shape = self .shape)
508
+ result = coo_array ((data[:, 0 ], (row, col)), shape = self .shape)
508
509
509
510
# As the description in from_scipy above, we sorted indices matrix
510
- # in row-major order if SciPy's coo_matrix has canonical format.
511
- # So, we must call sum_duplicates() to make the result coo_matrix
512
- # has canonical format.
511
+ # in row-major order if SciPy's coo_array has canonical format.
512
+ # So, we must call sum_duplicates() to make the resulting coo_array
513
+ # have canonical format.
513
514
if self .has_canonical_format:
514
515
result.sum_duplicates()
515
516
return result
@@ -693,19 +694,19 @@ shape: {self.shape}"""
693
694
@staticmethod
694
695
def from_scipy (obj , dim_names = None ):
695
696
"""
696
- Convert scipy.sparse.csr_matrix to arrow::SparseCSRMatrix.
697
+ Convert scipy.sparse.csr_array or scipy.sparse. csr_matrix to arrow::SparseCSRMatrix.
697
698
698
699
Parameters
699
700
----------
700
- obj : scipy.sparse.csr_matrix
701
+ obj : scipy.sparse.csr_array or scipy.sparse. csr_matrix
701
702
The scipy matrix that should be converted.
702
703
dim_names : list, optional
703
704
Names of the dimensions.
704
705
"""
705
706
import scipy.sparse
706
- if not isinstance (obj, scipy.sparse.csr_matrix):
707
+ if not ( isinstance (obj, scipy.sparse.csr_array) or isinstance (obj, scipy.sparse. csr_matrix) ):
707
708
raise TypeError (
708
- f" Expected scipy.sparse.csr_matrix, got {type(obj)}" )
709
+ f" Expected scipy.sparse.csr_array or scipy.sparse. csr_matrix, got {type(obj)}" )
709
710
710
711
cdef shared_ptr[CSparseCSRMatrix] csparse_tensor
711
712
cdef vector[int64_t] c_shape
@@ -764,9 +765,9 @@ shape: {self.shape}"""
764
765
765
766
def to_scipy (self ):
766
767
"""
767
- Convert arrow::SparseCSRMatrix to scipy.sparse.csr_matrix .
768
+ Convert arrow::SparseCSRMatrix to scipy.sparse.csr_array .
768
769
"""
769
- from scipy.sparse import csr_matrix
770
+ from scipy.sparse import csr_array
770
771
cdef PyObject* out_data
771
772
cdef PyObject* out_indptr
772
773
cdef PyObject* out_indices
@@ -778,7 +779,7 @@ shape: {self.shape}"""
778
779
data = PyObject_to_object(out_data)
779
780
indptr = PyObject_to_object(out_indptr)
780
781
indices = PyObject_to_object(out_indices)
781
- result = csr_matrix ((data[:, 0 ], indices, indptr), shape = self .shape)
782
+ result = csr_array ((data[:, 0 ], indices, indptr), shape = self .shape)
782
783
return result
783
784
784
785
def to_tensor (self ):
0 commit comments