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 12 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.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1353,6 +1353,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 ``SparseArray.nonzero`` and `SparseDataFrame.dropna` returning shifted/invalid results (:issue:`21172`)
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 :func: to reference the api of these

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure


Build Changes
^^^^^^^^^^^^^
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/sparse/frame/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1350,3 +1350,11 @@ def test_assign_with_sparse_frame(self):

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

def test_dropna(self):
tm.assert_sp_frame_equal(
Copy link
Contributor

Choose a reason for hiding this comment

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

use the result= and expected= format

add the issue number as a comment

pd.SparseDataFrame({"F2": [0, 1]}),
pd.SparseDataFrame(
{"F1": [float('nan'), float('nan')], "F2": [0, 1]}
).dropna(axis=1, inplace=False, how='all')
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 test for inplace=True/False, how='all', 'any'

via parametrization

)
23 changes: 23 additions & 0 deletions pandas/tests/sparse/test_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import numpy as np

import pandas as pd
from pandas.util import testing as tm


class TestSparseArray(object):

def test_nonzero(self):
sa = pd.SparseArray([
float('nan'),
float('nan'),
1, 0, 0,
2, 0, 0, 0,
3, 0, 0
])
tm.assert_numpy_array_equal(np.array([2, 5, 9], dtype=np.int32),
sa.nonzero()[0])

sa = pd.SparseArray(
[0, 0, 1, 0, 0, 2, 0, 0, 0, 3, 0, 0])
tm.assert_numpy_array_equal(np.array([2, 5, 9], dtype=np.int32),
sa.nonzero()[0])