Skip to content

Commit 6bc8c8a

Browse files
committed
DOC: Document scipy sparse matrix accepted in SparseSeries constructor
1 parent 3a12685 commit 6bc8c8a

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

doc/source/sparse.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,28 @@ To convert a ``SparseDataFrame`` back to sparse SciPy matrix in COO format, you
213213
214214
sdf.to_coo()
215215
216+
.. _sparse.scipysparse_series:
217+
216218
SparseSeries
217219
~~~~~~~~~~~~
218220

221+
.. versionadded:: 0.20.2
222+
223+
``SparseSeries``, ``SparseArray`` can be constructed from ``scipy.sparse.spmatrix`` objects of shape ``(1, n)`` or ``(n, 1)``.
224+
SciPy sparse matrices can also be assigned directly to a ``SparseDataFrame`` with an index.
225+
226+
.. ipython:: python
227+
228+
sa = pd.SparseSeries(sp_arr[:, 5])
229+
sa
230+
231+
sdf['x'] = sa
232+
sdf['y'] = sp_arr[:, 6]
233+
sdf[['z', 'w']] = sp_arr[:, [7, 8]]
234+
sdf.iloc[:, -5:]
235+
236+
Below interface is deprecated.
237+
219238
.. versionadded:: 0.16.0
220239

221240
A :meth:`SparseSeries.to_coo` method is implemented for transforming a ``SparseSeries`` indexed by a ``MultiIndex`` to a ``scipy.sparse.coo_matrix``.

doc/source/whatsnew/v0.20.2.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ Enhancements
2525
has been added to return the group order (:issue:`11642`); see
2626
:ref:`here <groupby.ngroup>`.
2727

28+
29+
- ``SparseSeries`` and ``SparseArray`` now support 1d ``scipy.sparse.spmatrix`` in constructor. Additionally, ``SparseDataFrame`` can be assigned columns of ``scipy.sparse.spmatrix``; see :ref:`here <sparse.scipysparse_series>`. (:issue:`15634`)
30+
2831
.. _whatsnew_0202.performance:
2932

3033
Performance Improvements

pandas/core/sparse/series.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,9 @@ def combine_first(self, other):
726726

727727
def to_coo(self, row_levels=(0, ), column_levels=(1, ), sort_labels=False):
728728
"""
729+
DEPRECATED; instead, make a SparseSeries with a two-level index,
730+
unstack it, then use .to_coo() on the resulting SparseDataFrame.
731+
729732
Create a scipy.sparse.coo_matrix from a SparseSeries with MultiIndex.
730733
731734
Use row_levels and column_levels to determine the row and column
@@ -783,6 +786,9 @@ def to_coo(self, row_levels=(0, ), column_levels=(1, ), sort_labels=False):
783786
@classmethod
784787
def from_coo(cls, A, dense_index=False):
785788
"""
789+
DEPRECATED; instead, pass 1d scipy.sparse matrices directly into
790+
SparseSeries constructor, and 2d into SparseDataFrame constructor.
791+
786792
Create a SparseSeries from a scipy.sparse.coo_matrix.
787793
788794
.. versionadded:: 0.16.0

0 commit comments

Comments
 (0)