You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/source/sparse.rst
+25-2
Original file line number
Diff line number
Diff line change
@@ -186,9 +186,32 @@ the correct dense result.
186
186
Interaction with scipy.sparse
187
187
-----------------------------
188
188
189
-
Experimental api to transform between sparse pandas and scipy.sparse structures.
189
+
.. versionadded:: 0.20.0
190
190
191
-
A :meth:`SparseSeries.to_coo` method is implemented for transforming a ``SparseSeries`` indexed by a ``MultiIndex`` to a ``scipy.sparse.coo_matrix``.
191
+
Pandas supports creating sparse dataframes directly from ``scipy.sparse`` matrices.
192
+
193
+
.. ipython:: python
194
+
195
+
from scipy.sparse import csr_matrix
196
+
197
+
arr = np.random.random(size=(1000, 5))
198
+
arr[arr <.9] =0
199
+
200
+
sp_arr = csr_matrix(arr)
201
+
sp_arr
202
+
203
+
sdf = pd.SparseDataFrame(sp_arr)
204
+
sdf
205
+
206
+
All sparse formats are supported, but matrices that aren't in :mod:`COOrdinate <scipy.sparse>` format will be converted to it, copying the data as needed. To convert a ``SparseDataFrame`` back to sparse SciPy matrix in COO format, you can use :meth:`SparseDataFrame.to_coo` method:
207
+
208
+
.. ipython:: python
209
+
210
+
sdf.to_coo()
211
+
212
+
.. versionadded:: 0.16.0
213
+
214
+
Additionally, a :meth:`SparseSeries.to_coo` method is implemented for transforming a ``SparseSeries`` indexed by a ``MultiIndex`` to a ``scipy.sparse.coo_matrix``.
192
215
193
216
The method requires a ``MultiIndex`` with two or more levels.
Pandas now supports creating sparse dataframes directly from ``scipy.sparse.spmatrix`` instances. See the :ref:`documentation <sparse.scipysparse>` for more information. (:issue:`4343`)
192
+
193
+
All sparse formats are supported, but matrices that aren't in :mod:`COOrdinate <scipy.sparse>` format will be converted to it, copying the data as needed.
194
+
195
+
.. ipython:: python
196
+
197
+
from scipy.sparse import csr_matrix
198
+
arr = np.random.random(size=(1000, 5))
199
+
arr[arr < .9] = 0
200
+
sp_arr = csr_matrix(arr)
201
+
sp_arr
202
+
sdf = pd.SparseDataFrame(sp_arr)
203
+
sdf
204
+
205
+
To convert a ``SparseDataFrame`` back to sparse SciPy matrix in COO format, you can use:
0 commit comments