From 962986765cb5ff3c00a96b5c7113a70153cb7b94 Mon Sep 17 00:00:00 2001 From: Thierry Moisan Date: Sat, 11 Mar 2023 14:21:11 -0500 Subject: [PATCH 1/2] DOC: add example for sparse.density and sparse.coo --- ci/code_checks.sh | 2 -- pandas/core/arrays/sparse/accessor.py | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/ci/code_checks.sh b/ci/code_checks.sh index 8fefa47c16bab..eeaa277b1ab2c 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -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__ diff --git a/pandas/core/arrays/sparse/accessor.py b/pandas/core/arrays/sparse/accessor.py index 7980638deb438..8f6b0e0288e8f 100644 --- a/pandas/core/arrays/sparse/accessor.py +++ b/pandas/core/arrays/sparse/accessor.py @@ -329,6 +329,18 @@ 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 '' + with 2 stored elements in COOrdinate format> + >>> sparse_array = pd.arrays.SparseArray([0, 1.234, 0, 8.32], fill_value=0) + >>> df = pd.DataFrame({"A": sparse_array}) + >>> df.sparse.to_coo() + <4x1 sparse matrix of type '' + with 2 stored elements in COOrdinate format> """ import_optional_dependency("scipy") from scipy.sparse import coo_matrix @@ -357,6 +369,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 From 9bbeabb75f6e574613c874cb5d2eef811eb13400 Mon Sep 17 00:00:00 2001 From: Thierry Moisan Date: Wed, 15 Mar 2023 15:28:30 -0400 Subject: [PATCH 2/2] fixup! DOC: add example for sparse.density and sparse.coo --- pandas/core/arrays/sparse/accessor.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pandas/core/arrays/sparse/accessor.py b/pandas/core/arrays/sparse/accessor.py index 8f6b0e0288e8f..ca1e73d3e6865 100644 --- a/pandas/core/arrays/sparse/accessor.py +++ b/pandas/core/arrays/sparse/accessor.py @@ -336,11 +336,6 @@ def to_coo(self): >>> df.sparse.to_coo() <4x1 sparse matrix of type '' with 2 stored elements in COOrdinate format> - >>> sparse_array = pd.arrays.SparseArray([0, 1.234, 0, 8.32], fill_value=0) - >>> df = pd.DataFrame({"A": sparse_array}) - >>> df.sparse.to_coo() - <4x1 sparse matrix of type '' - with 2 stored elements in COOrdinate format> """ import_optional_dependency("scipy") from scipy.sparse import coo_matrix