Skip to content

DOC: Improve sparse.rst #45376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/reference/arrays.rst
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ be stored efficiently as a :class:`arrays.SparseArray`.

The ``Series.sparse`` accessor may be used to access sparse-specific attributes
and methods if the :class:`Series` contains sparse values. See
:ref:`api.series.sparse` for more.
:ref:`api.series.sparse` and :ref:`the user guide <sparse>` for more.


.. _api.arrays.string:
Expand Down
20 changes: 10 additions & 10 deletions doc/source/user_guide/sparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ array that are ``nan`` aren't actually stored, only the non-``nan`` elements are
Those non-``nan`` elements have a ``float64`` dtype.

The sparse objects exist for memory efficiency reasons. Suppose you had a
large, mostly NA ``DataFrame``:
large, mostly NA :class:`DataFrame`:

.. ipython:: python

Expand Down Expand Up @@ -139,7 +139,7 @@ Sparse calculation
------------------

You can apply NumPy `ufuncs <https://numpy.org/doc/stable/reference/ufuncs.html>`_
to ``SparseArray`` and get a ``SparseArray`` as a result.
to :class:`arrays.SparseArray` and get a :class:`arrays.SparseArray` as a result.

.. ipython:: python

Expand Down Expand Up @@ -183,7 +183,7 @@ your code, rather than ignoring the warning.
**Construction**

From an array-like, use the regular :class:`Series` or
:class:`DataFrame` constructors with :class:`SparseArray` values.
:class:`DataFrame` constructors with :class:`arrays.SparseArray` values.

.. code-block:: python

Expand Down Expand Up @@ -240,7 +240,7 @@ Sparse-specific properties, like ``density``, are available on the ``.sparse`` a
**General differences**

In a ``SparseDataFrame``, *all* columns were sparse. A :class:`DataFrame` can have a mixture of
sparse and dense columns. As a consequence, assigning new columns to a ``DataFrame`` with sparse
sparse and dense columns. As a consequence, assigning new columns to a :class:`DataFrame` with sparse
values will not automatically convert the input to be sparse.

.. code-block:: python
Expand Down Expand Up @@ -269,7 +269,7 @@ have no replacement.
Interaction with scipy.sparse
-----------------------------

Use :meth:`DataFrame.sparse.from_spmatrix` to create a ``DataFrame`` with sparse values from a sparse matrix.
Use :meth:`DataFrame.sparse.from_spmatrix` to create a :class:`DataFrame` with sparse values from a sparse matrix.

.. versionadded:: 0.25.0

Expand All @@ -294,9 +294,9 @@ To convert back to sparse SciPy matrix in COO format, you can use the :meth:`Dat

sdf.sparse.to_coo()

:meth:`Series.sparse.to_coo` is implemented for transforming a ``Series`` with sparse values indexed by a :class:`MultiIndex` to a :class:`scipy.sparse.coo_matrix`.
:meth:`Series.sparse.to_coo` is implemented for transforming a :class:`Series` with sparse values indexed by a :class:`MultiIndex` to a :class:`scipy.sparse.coo_matrix`.

The method requires a ``MultiIndex`` with two or more levels.
The method requires a :class:`MultiIndex` with two or more levels.

.. ipython:: python

Expand All @@ -315,7 +315,7 @@ The method requires a ``MultiIndex`` with two or more levels.
ss = s.astype('Sparse')
ss

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.
In the example below, we transform the :class:`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.

.. ipython:: python

Expand All @@ -341,7 +341,7 @@ Specifying different row and column labels (and not sorting them) yields a diffe
rows
columns

A convenience method :meth:`Series.sparse.from_coo` is implemented for creating a ``Series`` with sparse values from a ``scipy.sparse.coo_matrix``.
A convenience method :meth:`Series.sparse.from_coo` is implemented for creating a :class:`Series` with sparse values from a ``scipy.sparse.coo_matrix``.

.. ipython:: python

Expand All @@ -350,7 +350,7 @@ A convenience method :meth:`Series.sparse.from_coo` is implemented for creating
A
A.todense()

The default behaviour (with ``dense_index=False``) simply returns a ``Series`` containing
The default behaviour (with ``dense_index=False``) simply returns a :class:`Series` containing
only the non-null entries.

.. ipython:: python
Expand Down