Skip to content

DOC: add example for sparse.density and sparse.coo #51909

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
Mar 16, 2023
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: 0 additions & 2 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.DataFrame.last_valid_index \
pandas.DataFrame.attrs \
pandas.DataFrame.plot \
pandas.DataFrame.sparse.density \
pandas.DataFrame.sparse.to_coo \
pandas.DataFrame.to_gbq \
pandas.DataFrame.style \
pandas.DataFrame.__dataframe__
Expand Down
13 changes: 13 additions & 0 deletions pandas/core/arrays/sparse/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,13 @@ def to_coo(self):
e.g. If the dtypes are float16 and float32, dtype will be upcast to
float32. By numpy.find_common_type convention, mixing int64 and
and uint64 will result in a float64 dtype.

Examples
--------
>>> df = pd.DataFrame({"A": pd.arrays.SparseArray([0, 1, 0, 1])})
>>> df.sparse.to_coo()
<4x1 sparse matrix of type '<class 'numpy.int64'>'
with 2 stored elements in COOrdinate format>
"""
import_optional_dependency("scipy")
from scipy.sparse import coo_matrix
Expand Down Expand Up @@ -357,6 +364,12 @@ def to_coo(self):
def density(self) -> float:
"""
Ratio of non-sparse points to total (dense) data points.

Examples
--------
>>> df = pd.DataFrame({"A": pd.arrays.SparseArray([0, 1, 0, 1])})
>>> df.sparse.density
0.5
"""
tmp = np.mean([column.array.density for _, column in self._parent.items()])
return tmp
Expand Down