Skip to content

Commit 9e5d69c

Browse files
committed
DOC: Correct and expand Series.nonzero docstring
- Mention that a tuple of indices is returned and explain why. - Instead of numpy.ndarray.nonzero, point to numpy.nonzero, which has the full documentation. - Add example.
1 parent 5d9475a commit 9e5d69c

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

pandas/core/series.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,27 @@ def compress(self, condition, axis=0, out=None, **kwargs):
331331

332332
def nonzero(self):
333333
"""
334-
return the a boolean array of the underlying data is nonzero
334+
Return the indices of the elements that are non-zero
335335
336-
See also
336+
This method is equivalent to calling `numpy.nonzero` on the
337+
series data. For compatability with NumPy, the return value is
338+
the same (a tuple with an array of indices for each dimension),
339+
but it will always be a one-item tuple because series only have
340+
one dimension.
341+
342+
Examples
343+
--------
344+
>>> s = pd.Series([0, 3, 0, 4])
345+
>>> s.nonzero()
346+
(array([1, 3]),)
347+
>>> s.iloc[s.nonzero()[0]]
348+
1 3
349+
3 4
350+
dtype: int64
351+
352+
See Also
337353
--------
338-
numpy.ndarray.nonzero
354+
numpy.nonzero
339355
"""
340356
return self.values.nonzero()
341357

0 commit comments

Comments
 (0)