Fix nonzero of a SparseArray
State: closed

The nonzero operation returned the nonzero locations of the underlying index. However we need to get the nonzero locations in the real array. For this operation to be faster an inverse index structure would be beneficial or it could be implemented using binary search.

```python
sa = pd.SparseArray([float('nan'), float('nan'), 1, 0, 0, 2, 0, 0, 0, 3, 0, 0])
```
returned `0, 3, 7`. The index is shifted by two because of the two first `NaN`s and that's why the `0, 3, 7` are returned. The correct result would be `2, 5, 9` and is found in the method.

For the above sample the code works. However for other implementations of `SparseIndex` it could be broken.

- [x] closes #21172
- [x] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry


Labels:
name: Sparse
description: Sparse Data Type

Comments:
Hello @babky! Thanks for updating the PR.

 - There are no PEP8 issues in the file [`pandas/tests/sparse/frame/test_frame.py`](https://github.com/babky/pandas/blob/e53e3fd73ea17caa720125bb040d841fce895671/pandas/tests/sparse/frame/test_frame.py) !

 - There are no PEP8 issues in the file [`pandas/tests/sparse/test_indexing.py`](https://github.com/babky/pandas/blob/e53e3fd73ea17caa720125bb040d841fce895671/pandas/tests/sparse/test_indexing.py) !



##### Comment last updated on November 08, 2018 at 09:20 Hours UTC
@babky int32 isn’t imported, needs to be changed to np.int32.
@babky small fixup needed to get this working
hi, i will fix them in near future, was on a vacation... 
needs tests, whatsnew entry, and should be a vectorized soln.
cc @TomAugspurger pinging you here since you are working on sparse, to make sure this PR would not conflict with your work 
At a glance, this approach won’t work with my implementation since calls super to get ndarray.nonzero
The issue looks gone in the current master. I've added some unit tests to cover the issue. From my really fast overlook of the tests I think that the tests could be added.
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/21175?src=pr&el=h1) Report
> Merging [#21175](https://codecov.io/gh/pandas-dev/pandas/pull/21175?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/db2066b7d31450aaa87f2efcef492162a3d08309?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/21175/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/21175?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #21175      +/-   ##
==========================================
+ Coverage   92.25%   92.25%   +<.01%     
==========================================
  Files         161      161              
  Lines       51390    51390              
==========================================
+ Hits        47411    47412       +1     
+ Misses       3979     3978       -1
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.65% <ø> (ø)` | :arrow_up: |
| #single | `42.31% <ø> (ø)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/21175?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/arrays/sparse.py](https://codecov.io/gh/pandas-dev/pandas/pull/21175/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvYXJyYXlzL3NwYXJzZS5weQ==) | `91.94% <0%> (+0.12%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/21175?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/21175?src=pr&el=footer). Last update [db2066b...2c912f1](https://codecov.io/gh/pandas-dev/pandas/pull/21175?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

@jbrockmendel @TomAugspurger: I believe the issue is gone in master branch and I just added the tests which should prevent a regression.
Moved it one more time. We reorganized things.
@jreback the comments should be resolved now
thanks @babky