Skip to content

DOC: improve docstring of function where #17665

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 3 commits into from
Sep 25, 2017
Merged
Changes from all 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
30 changes: 25 additions & 5 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5822,20 +5822,24 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,

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 update same for .mask as well.

Copy link
Member

Choose a reason for hiding this comment

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

mask and where now use the same docstring (with only parts of it substituted with the correct value). But keep doing this and providing a better explanation will become rather complex I think. Are you OK with just duplicating the docstring and have one for where and mask separately ?

Copy link
Contributor

Choose a reason for hiding this comment

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

oh that's right. I don't think duplicating is a good idea. maybe just put an example with mask here as well.

_shared_docs['where'] = ("""
Return an object of same shape as self and whose corresponding
entries are from self where cond is %(cond)s and otherwise are from
other.
entries are from self where `cond` is %(cond)s and otherwise are from
`other`.

Parameters
----------
cond : boolean %(klass)s, array-like, or callable
If cond is callable, it is computed on the %(klass)s and
Where `cond` is %(cond)s, keep the original value. Where
%(cond_rev)s, replace with corresponding value from `other`.
If `cond` is callable, it is computed on the %(klass)s and
should return boolean %(klass)s or array. The callable must
not change input %(klass)s (though pandas doesn't check it).

.. versionadded:: 0.18.1
A callable can be used as cond.

other : scalar, %(klass)s, or callable
Entries where `cond` is %(cond_rev)s are replaced with
corresponding value from `other`.
If other is callable, it is computed on the %(klass)s and
should return scalar or %(klass)s. The callable must not
change input %(klass)s (though pandas doesn't check it).
Expand Down Expand Up @@ -5881,6 +5885,20 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
3 3.0
4 4.0

>>> s.mask(s > 0)
0 0.0
1 NaN
2 NaN
3 NaN
4 NaN

>>> s.where(s > 1, 10)
0 10.0
1 10.0
2 2.0
3 3.0
4 4.0

>>> df = pd.DataFrame(np.arange(10).reshape(-1, 2), columns=['A', 'B'])
>>> m = df %% 3 == 0
>>> df.where(m, -df)
Expand Down Expand Up @@ -5911,7 +5929,8 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
""")

@Appender(_shared_docs['where'] % dict(_shared_doc_kwargs, cond="True",
name='where', name_other='mask'))
cond_rev="False", name='where',
name_other='mask'))
def where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
try_cast=False, raise_on_error=True):

Expand All @@ -5920,7 +5939,8 @@ def where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
raise_on_error)

@Appender(_shared_docs['where'] % dict(_shared_doc_kwargs, cond="False",
name='mask', name_other='where'))
cond_rev="True", name='mask',
name_other='where'))
def mask(self, cond, other=np.nan, inplace=False, axis=None, level=None,
try_cast=False, raise_on_error=True):

Expand Down