@@ -350,6 +350,35 @@ def _deserialize_scipy_sparse(data):
350350 pass
351351
352352
353+ # ----------------------------------------------------------------------
354+ # Set up serialization for pydata/sparse tensors.
355+
356+ def _register_pydata_sparse_handlers (serialization_context ):
357+ try :
358+ import sparse
359+
360+ def _serialize_pydata_sparse (obj ):
361+ if isinstance (obj , sparse .coo .core .COO ):
362+ return 'coo' , pyarrow .SparseTensorCOO .from_numpy (
363+ obj .data , obj .coords .T , shape = obj .shape )
364+
365+ def _deserialize_pydata_sparse (data ):
366+ if data [0 ] == 'coo' :
367+ data_array , coords = data [1 ].to_numpy ()
368+ return sparse .COO (
369+ data = data_array [:, 0 ],
370+ coords = coords .T , shape = data [1 ].shape )
371+
372+ serialization_context .register_type (
373+ sparse .coo .core .COO , 'sparse.coo.core.COO' ,
374+ custom_serializer = _serialize_pydata_sparse ,
375+ custom_deserializer = _deserialize_pydata_sparse )
376+
377+ except ImportError :
378+ # no pydata/sparse
379+ pass
380+
381+
353382def register_default_serialization_handlers (serialization_context ):
354383
355384 # ----------------------------------------------------------------------
@@ -403,6 +432,7 @@ def register_default_serialization_handlers(serialization_context):
403432 _register_collections_serialization_handlers (serialization_context )
404433 _register_custom_pandas_handlers (serialization_context )
405434 _register_scipy_handlers (serialization_context )
435+ _register_pydata_sparse_handlers (serialization_context )
406436
407437
408438def default_serialization_context ():
0 commit comments