Skip to content

Commit d716d80

Browse files
authored
DOC: add example for sparse.density and sparse.coo (#51909)
* DOC: add example for sparse.density and sparse.coo * fixup! DOC: add example for sparse.density and sparse.coo
1 parent 12e7e9c commit d716d80

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

ci/code_checks.sh

-2
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
547547
pandas.DataFrame.last_valid_index \
548548
pandas.DataFrame.attrs \
549549
pandas.DataFrame.plot \
550-
pandas.DataFrame.sparse.density \
551-
pandas.DataFrame.sparse.to_coo \
552550
pandas.DataFrame.to_gbq \
553551
pandas.DataFrame.style \
554552
pandas.DataFrame.__dataframe__

pandas/core/arrays/sparse/accessor.py

+13
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,13 @@ def to_coo(self):
329329
e.g. If the dtypes are float16 and float32, dtype will be upcast to
330330
float32. By numpy.find_common_type convention, mixing int64 and
331331
and uint64 will result in a float64 dtype.
332+
333+
Examples
334+
--------
335+
>>> df = pd.DataFrame({"A": pd.arrays.SparseArray([0, 1, 0, 1])})
336+
>>> df.sparse.to_coo()
337+
<4x1 sparse matrix of type '<class 'numpy.int64'>'
338+
with 2 stored elements in COOrdinate format>
332339
"""
333340
import_optional_dependency("scipy")
334341
from scipy.sparse import coo_matrix
@@ -357,6 +364,12 @@ def to_coo(self):
357364
def density(self) -> float:
358365
"""
359366
Ratio of non-sparse points to total (dense) data points.
367+
368+
Examples
369+
--------
370+
>>> df = pd.DataFrame({"A": pd.arrays.SparseArray([0, 1, 0, 1])})
371+
>>> df.sparse.density
372+
0.5
360373
"""
361374
tmp = np.mean([column.array.density for _, column in self._parent.items()])
362375
return tmp

0 commit comments

Comments
 (0)