Skip to content

Commit 5279a17

Browse files
JennaVergeynstjreback
authored andcommitted
DOC: improve docstring of function where (#17665)
1 parent 45a795e commit 5279a17

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

pandas/core/generic.py

+25-5
Original file line numberDiff line numberDiff line change
@@ -5825,20 +5825,24 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
58255825

58265826
_shared_docs['where'] = ("""
58275827
Return an object of same shape as self and whose corresponding
5828-
entries are from self where cond is %(cond)s and otherwise are from
5829-
other.
5828+
entries are from self where `cond` is %(cond)s and otherwise are from
5829+
`other`.
58305830
58315831
Parameters
58325832
----------
58335833
cond : boolean %(klass)s, array-like, or callable
5834-
If cond is callable, it is computed on the %(klass)s and
5834+
Where `cond` is %(cond)s, keep the original value. Where
5835+
%(cond_rev)s, replace with corresponding value from `other`.
5836+
If `cond` is callable, it is computed on the %(klass)s and
58355837
should return boolean %(klass)s or array. The callable must
58365838
not change input %(klass)s (though pandas doesn't check it).
58375839
58385840
.. versionadded:: 0.18.1
58395841
A callable can be used as cond.
58405842
58415843
other : scalar, %(klass)s, or callable
5844+
Entries where `cond` is %(cond_rev)s are replaced with
5845+
corresponding value from `other`.
58425846
If other is callable, it is computed on the %(klass)s and
58435847
should return scalar or %(klass)s. The callable must not
58445848
change input %(klass)s (though pandas doesn't check it).
@@ -5884,6 +5888,20 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
58845888
3 3.0
58855889
4 4.0
58865890
5891+
>>> s.mask(s > 0)
5892+
0 0.0
5893+
1 NaN
5894+
2 NaN
5895+
3 NaN
5896+
4 NaN
5897+
5898+
>>> s.where(s > 1, 10)
5899+
0 10.0
5900+
1 10.0
5901+
2 2.0
5902+
3 3.0
5903+
4 4.0
5904+
58875905
>>> df = pd.DataFrame(np.arange(10).reshape(-1, 2), columns=['A', 'B'])
58885906
>>> m = df %% 3 == 0
58895907
>>> df.where(m, -df)
@@ -5914,7 +5932,8 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
59145932
""")
59155933

59165934
@Appender(_shared_docs['where'] % dict(_shared_doc_kwargs, cond="True",
5917-
name='where', name_other='mask'))
5935+
cond_rev="False", name='where',
5936+
name_other='mask'))
59185937
def where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
59195938
try_cast=False, raise_on_error=True):
59205939

@@ -5923,7 +5942,8 @@ def where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
59235942
raise_on_error)
59245943

59255944
@Appender(_shared_docs['where'] % dict(_shared_doc_kwargs, cond="False",
5926-
name='mask', name_other='where'))
5945+
cond_rev="True", name='mask',
5946+
name_other='where'))
59275947
def mask(self, cond, other=np.nan, inplace=False, axis=None, level=None,
59285948
try_cast=False, raise_on_error=True):
59295949

0 commit comments

Comments
 (0)