3333except ImportError :
3434 sparse = None
3535
36-
3736tensor_type_pairs = [
3837 ('i1' , pa .int8 ()),
3938 ('i2' , pa .int16 ()),
@@ -444,6 +443,8 @@ def test_sparse_coo_tensor_scipy_roundtrip(dtype_str, arrow_type):
444443 assert sparse_tensor .has_canonical_format
445444 assert out_scipy_matrix .has_canonical_format
446445
446+ assert scipy_matrix .nnz == 0
447+ assert scipy_matrix .nnz == sparse_tensor .non_zero_length
447448 assert np .array_equal (scipy_matrix .data , out_scipy_matrix .data )
448449 assert np .array_equal (scipy_matrix .row , out_scipy_matrix .row )
449450 assert np .array_equal (scipy_matrix .col , out_scipy_matrix .col )
@@ -479,6 +480,19 @@ def test_sparse_csr_matrix_scipy_roundtrip(dtype_str, arrow_type):
479480 dense_array = sparse_array .toarray ()
480481 assert np .array_equal (dense_array , sparse_tensor .to_tensor ().to_numpy ())
481482
483+ scipy_matrix = csr_matrix ([[0 , 0 ], [0 , 0 ]])
484+ sparse_tensor = pa .SparseCSRMatrix .from_scipy (scipy_matrix ,
485+ dim_names = dim_names )
486+ out_scipy_matrix = sparse_tensor .to_scipy ()
487+ dense_array = scipy_matrix .toarray ()
488+
489+ assert scipy_matrix .nnz == 0
490+ assert scipy_matrix .nnz == sparse_tensor .non_zero_length
491+ assert np .array_equal (scipy_matrix .data , out_scipy_matrix .data )
492+ assert np .array_equal (scipy_matrix .indptr , out_scipy_matrix .indptr )
493+ assert np .array_equal (scipy_matrix .indices , out_scipy_matrix .indices )
494+ assert np .array_equal (dense_array , sparse_tensor .to_tensor ().to_numpy ())
495+
482496
483497@pytest .mark .skipif (not sparse , reason = "requires pydata/sparse" )
484498@pytest .mark .parametrize ('dtype_str,arrow_type' , tensor_type_pairs )
@@ -504,3 +518,16 @@ def test_pydata_sparse_sparse_coo_tensor_roundtrip(dtype_str, arrow_type):
504518 assert np .array_equal (sparse_array .coords , out_sparse_array .coords )
505519 assert np .array_equal (sparse_array .todense (),
506520 sparse_tensor .to_tensor ().to_numpy ())
521+
522+ sparse_array = sparse .COO .from_numpy ([[0 , 0 ], [0 , 0 ]])
523+ sparse_tensor = pa .SparseCOOTensor .from_pydata_sparse (sparse_array ,
524+ dim_names = dim_names )
525+ out_sparse_array = sparse_tensor .to_pydata_sparse ()
526+ dense_array = sparse_array .todense ()
527+
528+ assert sparse_array .nnz == 0
529+ assert sparse_array .nnz == sparse_tensor .non_zero_length
530+ assert out_sparse_array .nnz == sparse_tensor .non_zero_length
531+ assert np .array_equal (sparse_array .data , out_sparse_array .data )
532+ assert np .array_equal (sparse_array .coords , out_sparse_array .coords )
533+ assert np .array_equal (dense_array , sparse_tensor .to_tensor ().to_numpy ())
0 commit comments