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

Conversation

Moisan
Copy link
Contributor

@Moisan Moisan commented Mar 11, 2023

  • closes #xxxx (Replace xxxx with the GitHub issue number)
  • Tests added and passed if fixing a bug or adding a new feature
  • All code checks passed.
  • Added type annotations to new arguments/methods/functions.
  • Added an entry in the latest doc/source/whatsnew/vX.X.X.rst file if fixing a bug or adding a new feature.

Comment on lines 339 to 343
>>> 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)

@mroeschke mroeschke added Docs Sparse Sparse Data Type labels Mar 13, 2023
@MarcoGorelli MarcoGorelli added this to the 2.1 milestone Mar 16, 2023
Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

Nice, thanks @Moisan !

@MarcoGorelli MarcoGorelli merged commit d716d80 into pandas-dev:main Mar 16, 2023
@Moisan Moisan deleted the sparse_doc_example branch March 16, 2023 22:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Sparse Sparse Data Type
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants