Skip to content

Fix nonzero of a SparseArray #21175

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 22 commits into from
Nov 17, 2018
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,7 @@ Sparse
- Bug in ``DataFrame.groupby`` not including ``fill_value`` in the groups for non-NA ``fill_value`` when grouping by a sparse column (:issue:`5078`)
- Bug in unary inversion operator (``~``) on a ``SparseSeries`` with boolean values. The performance of this has also been improved (:issue:`22835`)
- Bug in :meth:`SparseArary.unique` not returning the unique values (:issue:`19595`)
- Bug in :meth:`SparseArray.nonzero` and :meth:`SparseDataFrame.dropna` returning shifted/incorrect results (:issue:`21172`)

Build Changes
^^^^^^^^^^^^^
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/arrays/sparse/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,23 @@ def test_fillna_overlap(self):
exp = SparseArray([1, 3, 3, 3, 3], fill_value=0, dtype=np.float64)
tm.assert_sp_array_equal(res, exp)

def test_nonzero(self):
# Tests regression #21172.
sa = pd.SparseArray([
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add the issue number

float('nan'),
float('nan'),
1, 0, 0,
2, 0, 0, 0,
3, 0, 0
])
expected = np.array([2, 5, 9], dtype=np.int32)
result, = sa.nonzero()
tm.assert_numpy_array_equal(expected, result)

sa = pd.SparseArray([0, 0, 1, 0, 0, 2, 0, 0, 0, 3, 0, 0])
result, = sa.nonzero()
tm.assert_numpy_array_equal(expected, result)


class TestSparseArrayAnalytics(object):

Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/sparse/frame/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1360,3 +1360,16 @@ def test_assign_with_sparse_frame(self):

for column in res.columns:
assert type(res[column]) is SparseSeries

@pytest.mark.parametrize("inplace", [True, False])
@pytest.mark.parametrize("how", ["all", "any"])
def test_dropna(self, inplace, how):
# Tests regression #21172.
expected = pd.SparseDataFrame({"F2": [0, 1]})
input_df = pd.SparseDataFrame(
{"F1": [float('nan'), float('nan')], "F2": [0, 1]}
)
result_df = input_df.dropna(axis=1, inplace=inplace, how=how)
if inplace:
result_df = input_df
tm.assert_sp_frame_equal(expected, result_df)