Skip to content

Commit 7a3ebce

Browse files
babkytm9k1
authored andcommitted
Fix nonzero of a SparseArray (pandas-dev#21175)
1 parent 9578e3f commit 7a3ebce

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,7 @@ Sparse
14381438
- Bug in ``DataFrame.groupby`` not including ``fill_value`` in the groups for non-NA ``fill_value`` when grouping by a sparse column (:issue:`5078`)
14391439
- Bug in unary inversion operator (``~``) on a ``SparseSeries`` with boolean values. The performance of this has also been improved (:issue:`22835`)
14401440
- Bug in :meth:`SparseArary.unique` not returning the unique values (:issue:`19595`)
1441+
- Bug in :meth:`SparseArray.nonzero` and :meth:`SparseDataFrame.dropna` returning shifted/incorrect results (:issue:`21172`)
14411442

14421443
Build Changes
14431444
^^^^^^^^^^^^^

pandas/tests/arrays/sparse/test_array.py

+17
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,23 @@ def test_fillna_overlap(self):
784784
exp = SparseArray([1, 3, 3, 3, 3], fill_value=0, dtype=np.float64)
785785
tm.assert_sp_array_equal(res, exp)
786786

787+
def test_nonzero(self):
788+
# Tests regression #21172.
789+
sa = pd.SparseArray([
790+
float('nan'),
791+
float('nan'),
792+
1, 0, 0,
793+
2, 0, 0, 0,
794+
3, 0, 0
795+
])
796+
expected = np.array([2, 5, 9], dtype=np.int32)
797+
result, = sa.nonzero()
798+
tm.assert_numpy_array_equal(expected, result)
799+
800+
sa = pd.SparseArray([0, 0, 1, 0, 0, 2, 0, 0, 0, 3, 0, 0])
801+
result, = sa.nonzero()
802+
tm.assert_numpy_array_equal(expected, result)
803+
787804

788805
class TestSparseArrayAnalytics(object):
789806

pandas/tests/sparse/frame/test_frame.py

+13
Original file line numberDiff line numberDiff line change
@@ -1360,3 +1360,16 @@ def test_assign_with_sparse_frame(self):
13601360

13611361
for column in res.columns:
13621362
assert type(res[column]) is SparseSeries
1363+
1364+
@pytest.mark.parametrize("inplace", [True, False])
1365+
@pytest.mark.parametrize("how", ["all", "any"])
1366+
def test_dropna(self, inplace, how):
1367+
# Tests regression #21172.
1368+
expected = pd.SparseDataFrame({"F2": [0, 1]})
1369+
input_df = pd.SparseDataFrame(
1370+
{"F1": [float('nan'), float('nan')], "F2": [0, 1]}
1371+
)
1372+
result_df = input_df.dropna(axis=1, inplace=inplace, how=how)
1373+
if inplace:
1374+
result_df = input_df
1375+
tm.assert_sp_frame_equal(expected, result_df)

0 commit comments

Comments
 (0)