Skip to content

Commit a197837

Browse files
sechildsmroeschke
authored andcommitted
DOC: Update is_sparse docstring (#19983)
* BUG: Identify SparseDataFrame as sparse The is_sparse function checks to see if an array-like is spare by checking to see if it is an instance of ABCSparseArray or ABCSparseSeries. This commit adds ABCSparseDataFrame to that list -- so it can detect that a DataFrame (which is an array-like object) is sparse. Added a test for this. * Revert "BUG: Identify SparseDataFrame as sparse" This reverts commit 10dffd1. The previous commit's change was not necessary. Will add a docstring to clarify the behaviour of the method. * DOC: Revise is_sparce docstring Clean up the docstring for is_sparse so it confirms to the documentation style guide. Add additional examples and clarify that is_sparse expect a 1-dimensional array-like. * DOC: Adjust is_sparse docstring. Responding to pull request comments.
1 parent fe52d9f commit a197837

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

pandas/core/dtypes/common.py

+37-10
Original file line numberDiff line numberDiff line change
@@ -150,32 +150,59 @@ def is_object_dtype(arr_or_dtype):
150150

151151
def is_sparse(arr):
152152
"""
153-
Check whether an array-like is a pandas sparse array.
153+
Check whether an array-like is a 1-D pandas sparse array.
154+
155+
Check that the one-dimensional array-like is a pandas sparse array.
156+
Returns True if it is a pandas sparse array, not another type of
157+
sparse array.
154158
155159
Parameters
156160
----------
157161
arr : array-like
158-
The array-like to check.
162+
Array-like to check.
159163
160164
Returns
161165
-------
162-
boolean : Whether or not the array-like is a pandas sparse array.
166+
bool
167+
Whether or not the array-like is a pandas sparse array.
168+
169+
See Also
170+
--------
171+
DataFrame.to_sparse : Convert DataFrame to a SparseDataFrame.
172+
Series.to_sparse : Convert Series to SparseSeries.
173+
Series.to_dense : Return dense representation of a Series.
163174
164175
Examples
165176
--------
166-
>>> is_sparse(np.array([1, 2, 3]))
167-
False
168-
>>> is_sparse(pd.SparseArray([1, 2, 3]))
177+
Returns `True` if the parameter is a 1-D pandas sparse array.
178+
179+
>>> is_sparse(pd.SparseArray([0, 0, 1, 0]))
169180
True
170-
>>> is_sparse(pd.SparseSeries([1, 2, 3]))
181+
>>> is_sparse(pd.SparseSeries([0, 0, 1, 0]))
171182
True
172183
173-
This function checks only for pandas sparse array instances, so
174-
sparse arrays from other libraries will return False.
184+
Returns `False` if the parameter is not sparse.
185+
186+
>>> is_sparse(np.array([0, 0, 1, 0]))
187+
False
188+
>>> is_sparse(pd.Series([0, 1, 0, 0]))
189+
False
190+
191+
Returns `False` if the parameter is not a pandas sparse array.
175192
176193
>>> from scipy.sparse import bsr_matrix
177-
>>> is_sparse(bsr_matrix([1, 2, 3]))
194+
>>> is_sparse(bsr_matrix([0, 1, 0, 0]))
178195
False
196+
197+
Returns `False` if the parameter has more than one dimension.
198+
199+
>>> df = pd.SparseDataFrame([389., 24., 80.5, np.nan],
200+
columns=['max_speed'],
201+
index=['falcon', 'parrot', 'lion', 'monkey'])
202+
>>> is_sparse(df)
203+
False
204+
>>> is_sparse(df.max_speed)
205+
True
179206
"""
180207
from pandas.core.arrays.sparse import SparseDtype
181208

0 commit comments

Comments
 (0)