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 1 commit
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
18 changes: 18 additions & 0 deletions pandas/core/arrays/sparse/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<class 'numpy.int64'>'
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 '<class 'numpy.float64'>'
with 2 stored elements in COOrdinate format>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for working on this - does fill_value do anything here?

I'd suggest either:

  • writing an example where fill_value has an effect
  • just keeping the example above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to_coo() requires the fill_value to be zero, but when using floats, it is np.nan by default:

>>> df = pd.DataFrame({"A": pd.arrays.SparseArray([0, 1.234, 0, 8.32])})
>>> df.sparse.to_coo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/thierrymoisan/dev/pandas/pandas/core/arrays/sparse/accessor.py", line 344, in to_coo
    raise ValueError("fill value must be 0 when converting to COO matrix")
ValueError: fill value must be 0 when converting to COO matrix

So I believe fill_value does have an effect in this example.

Copy link
Member

@MarcoGorelli MarcoGorelli Mar 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for explaining - to be honest I'd leave this second example out, I'm not sure what it adds (either that, or add a sentence before the example explaining what it does)

"""
import_optional_dependency("scipy")
from scipy.sparse import coo_matrix
Expand Down Expand Up @@ -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
Expand Down