Skip to content

DOC: Update is_sparse docstring #19983

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 6 commits into from
Nov 13, 2018
Merged
Changes from all commits
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
47 changes: 37 additions & 10 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,32 +150,59 @@ def is_object_dtype(arr_or_dtype):

def is_sparse(arr):
"""
Check whether an array-like is a pandas sparse array.
Check whether an array-like is a 1-D pandas sparse array.

Check that the one-dimensional array-like is a pandas sparse array.
Returns True if it is a pandas sparse array, not another type of
sparse array.
Copy link
Member

Choose a reason for hiding this comment

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

It will take some research, but I think it would be useful to explain what are the use cases of this function, besides what it does.

Copy link
Member

Choose a reason for hiding this comment

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

Personally I find this paragraph not so clear. I think something like this would be shorter and also clearer: "Return True if arr is pandas.SparseArray or pandas.SparseSeries, and False for any other type."


Parameters
----------
arr : array-like
The array-like to check.
Array-like to check.

Returns
-------
boolean : Whether or not the array-like is a pandas sparse array.
bool
Whether or not the array-like is a pandas sparse array.

See Also
--------
DataFrame.to_sparse : Convert DataFrame to a SparseDataFrame.
Series.to_sparse : Convert Series to SparseSeries.
Series.to_dense : Return dense representation of a Series.
Copy link
Member

Choose a reason for hiding this comment

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

Based on my comment above, I think we want to include in the see also the mentioned methods pandas.SparseArray and pandas.SparseSeries. Also, may be it makes sense to get rid of DataFrame.to_sparse, as it's not so directly related.


Examples
--------
>>> is_sparse(np.array([1, 2, 3]))
False
>>> is_sparse(pd.SparseArray([1, 2, 3]))
Returns `True` if the parameter is a 1-D pandas sparse array.

>>> is_sparse(pd.SparseArray([0, 0, 1, 0]))
True
>>> is_sparse(pd.SparseSeries([1, 2, 3]))
>>> is_sparse(pd.SparseSeries([0, 0, 1, 0]))
True

This function checks only for pandas sparse array instances, so
sparse arrays from other libraries will return False.
Returns `False` if the parameter is not sparse.

>>> is_sparse(np.array([0, 0, 1, 0]))
False
>>> is_sparse(pd.Series([0, 1, 0, 0]))
False

Returns `False` if the parameter is not a pandas sparse array.

>>> from scipy.sparse import bsr_matrix
>>> is_sparse(bsr_matrix([1, 2, 3]))
>>> is_sparse(bsr_matrix([0, 1, 0, 0]))
False

Returns `False` if the parameter has more than one dimension.
Copy link
Member

Choose a reason for hiding this comment

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

I'd be explicit here in saying, return false if it has more than one dimension, even if it is sparse. Minor change, but I think it's a bit more clear.


>>> df = pd.SparseDataFrame([389., 24., 80.5, np.nan],
columns=['max_speed'],
index=['falcon', 'parrot', 'lion', 'monkey'])
>>> is_sparse(df)
False
>>> is_sparse(df.max_speed)
True
"""
from pandas.core.arrays.sparse import SparseDtype

Expand Down