Skip to content

Commit 10dffd1

Browse files
committed
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.
1 parent 0bfb61b commit 10dffd1

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,7 @@ Sparse
987987
- Bug in :class:`SparseDataFrame.to_csv` causing exception (:issue:`19384`)
988988
- Bug in :class:`SparseSeries.memory_usage` which caused segfault by accessing non sparse elements (:issue:`19368`)
989989
- Bug in constructing a ``SparseArray``: if ``data`` is a scalar and ``index`` is defined it will coerce to ``float64`` regardless of scalar's dtype. (:issue:`19163`)
990+
- Bug in :func:`is_sparse` which would report a ``SparseDataFrame`` as not sparse.
990991

991992
Reshaping
992993
^^^^^^^^^

pandas/core/dtypes/common.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
ExtensionDtype)
1313
from .generic import (ABCCategorical, ABCPeriodIndex,
1414
ABCDatetimeIndex, ABCSeries,
15-
ABCSparseArray, ABCSparseSeries, ABCCategoricalIndex,
15+
ABCSparseArray, ABCSparseSeries, ABCSparseDataFrame,
16+
ABCCategoricalIndex,
1617
ABCIndexClass, ABCDateOffset)
1718
from .inference import is_string_like, is_list_like
1819
from .inference import * # noqa
@@ -149,7 +150,8 @@ def is_sparse(arr):
149150
False
150151
"""
151152

152-
return isinstance(arr, (ABCSparseArray, ABCSparseSeries))
153+
return isinstance(arr, (ABCSparseArray, ABCSparseSeries,
154+
ABCSparseDataFrame))
153155

154156

155157
def is_scipy_sparse(arr):

pandas/tests/dtypes/test_common.py

+1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ def test_is_object():
139139
def test_is_sparse(check_scipy):
140140
assert com.is_sparse(pd.SparseArray([1, 2, 3]))
141141
assert com.is_sparse(pd.SparseSeries([1, 2, 3]))
142+
assert com.is_sparse(pd.SparseDataFrame([1, 2, 3]))
142143

143144
assert not com.is_sparse(np.array([1, 2, 3]))
144145

0 commit comments

Comments
 (0)