-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Conversation
>>> 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> |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, thanks @Moisan !
doc/source/whatsnew/vX.X.X.rst
file if fixing a bug or adding a new feature.