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
All sparse formats are supported, but matrices that are not in :mod:`COOrdinate <scipy.sparse>` format will be converted, copying data as needed.
288
-
To convert a ``SparseDataFrame`` back to sparse SciPy matrix in COO format, you can use the :meth:`SparseDataFrame.to_coo` method:
285
+
To convert back to sparse SciPy matrix in COO format, you can use the :meth:`DataFrame.sparse.to_coo` method:
289
286
290
287
.. ipython:: python
291
288
292
-
sdf.to_coo()
289
+
sdf.sparse.to_coo()
293
290
294
-
SparseSeries
295
-
~~~~~~~~~~~~
296
-
297
-
A :meth:`SparseSeries.to_coo` method is implemented for transforming a ``SparseSeries`` indexed by a ``MultiIndex`` to a ``scipy.sparse.coo_matrix``.
291
+
:meth:`Series.sparse.to_coo` is implemented for transforming a ``Series`` with sparse values indexed by a ``MultiIndex`` to a ``scipy.sparse.coo_matrix``.
298
292
299
293
The method requires a ``MultiIndex`` with two or more levels.
300
294
301
295
.. ipython:: python
302
-
:okwarning:
303
296
304
297
s = pd.Series([3.0, np.nan, 1.0, 3.0, np.nan, np.nan])
@@ -309,19 +302,17 @@ The method requires a ``MultiIndex`` with two or more levels.
309
302
(2, 1, 'b', 0),
310
303
(2, 1, 'b', 1)],
311
304
names=['A', 'B', 'C', 'D'])
312
-
313
305
s
314
-
# SparseSeries
315
-
ss = s.to_sparse()
306
+
ss = s.astype('Sparse')
316
307
ss
317
308
318
-
In the example below, we transform the ``SparseSeries`` to a sparse representation of a 2-d array by specifying that the first and second ``MultiIndex`` levels define labels for the rows and the third and fourth levels define labels for the columns. We also specify that the column and row labels should be sorted in the final sparse representation.
309
+
In the example below, we transform the ``Series`` to a sparse representation of a 2-d array by specifying that the first and second ``MultiIndex`` levels define labels for the rows and the third and fourth levels define labels for the columns. We also specify that the column and row labels should be sorted in the final sparse representation.
319
310
320
311
.. ipython:: python
321
312
322
-
A, rows, columns = ss.to_coo(row_levels=['A', 'B'],
323
-
column_levels=['C', 'D'],
324
-
sort_labels=True)
313
+
A, rows, columns = ss.sparse.to_coo(row_levels=['A', 'B'],
314
+
column_levels=['C', 'D'],
315
+
sort_labels=True)
325
316
326
317
A
327
318
A.todense()
@@ -332,16 +323,16 @@ Specifying different row and column labels (and not sorting them) yields a diffe
332
323
333
324
.. ipython:: python
334
325
335
-
A, rows, columns = ss.to_coo(row_levels=['A', 'B', 'C'],
336
-
column_levels=['D'],
337
-
sort_labels=False)
326
+
A, rows, columns = ss.sparse.to_coo(row_levels=['A', 'B', 'C'],
327
+
column_levels=['D'],
328
+
sort_labels=False)
338
329
339
330
A
340
331
A.todense()
341
332
rows
342
333
columns
343
334
344
-
A convenience method :meth:`SparseSeries.from_coo` is implemented for creating a ``SparseSeries`` from a ``scipy.sparse.coo_matrix``.
335
+
A convenience method :meth:`Series.sparse.from_coo` is implemented for creating a ``Series`` with sparse values from a ``scipy.sparse.coo_matrix``.
345
336
346
337
.. ipython:: python
347
338
@@ -351,23 +342,28 @@ A convenience method :meth:`SparseSeries.from_coo` is implemented for creating a
351
342
A
352
343
A.todense()
353
344
354
-
The default behaviour (with ``dense_index=False``) simply returns a ``SparseSeries`` containing
345
+
The default behaviour (with ``dense_index=False``) simply returns a ``Series`` containing
355
346
only the non-null entries.
356
347
357
348
.. ipython:: python
358
-
:okwarning:
359
349
360
-
ss = pd.SparseSeries.from_coo(A)
350
+
ss = pd.Series.sparse.from_coo(A)
361
351
ss
362
352
363
353
Specifying ``dense_index=True`` will result in an index that is the Cartesian product of the
364
354
row and columns coordinates of the matrix. Note that this will consume a significant amount of memory
365
355
(relative to ``dense_index=False``) if the sparse matrix is large (and sparse) enough.
0 commit comments