Skip to content

Commit 9b6bfcd

Browse files
committed
DOC: Improve docs (GH19312) for Series.nonzero()
1 parent 1c0a48c commit 9b6bfcd

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

pandas/core/series.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,13 +490,14 @@ def compress(self, condition, *args, **kwargs):
490490

491491
def nonzero(self):
492492
"""
493-
Return the indices of the elements that are non-zero
493+
Return the integer indices of the elements that are non-zero
494494
495495
This method is equivalent to calling `numpy.nonzero` on the
496496
series data. For compatibility with NumPy, the return value is
497497
the same (a tuple with an array of indices for each dimension),
498498
but it will always be a one-item tuple because series only have
499-
one dimension.
499+
one dimension. Note that the method returns the *integer* indices
500+
regardless of the index of the series.
500501
501502
Examples
502503
--------
@@ -508,6 +509,15 @@ def nonzero(self):
508509
3 4
509510
dtype: int64
510511
512+
>>> s = pd.Series([0, 3, 0, 4])
513+
>>> s.index=['a', 'b', 'c', 'd']
514+
>>> s.nonzero() # same return although index of s is different
515+
(array([1, 3]),)
516+
>>> s.iloc[s.nonzero()[0]]
517+
b 3
518+
d 4
519+
dtype: int64
520+
511521
See Also
512522
--------
513523
numpy.nonzero

0 commit comments

Comments
 (0)