Skip to content

DOC: Added example to NDFrame.where #13798

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 1 commit into from
Aug 15, 2016
Merged
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions doc/source/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,8 @@ To select a row where each column meets its own criterion:

df[row_mask]

.. _indexing.where_mask:

The :meth:`~pandas.DataFrame.where` Method and Masking
------------------------------------------------------

Expand Down Expand Up @@ -891,6 +893,15 @@ without creating a copy:
df_orig.where(df > 0, -df, inplace=True);
df_orig

.. note::

The signature for :func:`DataFrame.where` differs from :func:`numpy.where`.
Roughly ``df1.where(m, df2)`` is equivalent to ``np.where(m, df1, df2)``.

.. ipython:: python

df.where(df < 0, -df) == np.where(df < 0, df, -df)

**alignment**

Furthermore, ``where`` aligns the input boolean condition (ndarray or DataFrame),
Expand Down
58 changes: 56 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4690,17 +4690,71 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
Returns
-------
wh : same type as caller

Notes
-----
The %(name)s method is an application of the if-then idiom. For each
element in the calling DataFrame, if ``cond`` is ``%(cond)s`` the
element is used; otherwise the corresponding element from the DataFrame
``other`` is used.

The signature for :func:`DataFrame.where` differs from
:func:`numpy.where`. Roughly ``df1.where(m, df2)`` is equivalent to
``np.where(m, df1, df2)``.

For further details and examples see the ``%(name)s`` documentation in
:ref:`indexing <indexing.where_mask>`.

Examples
--------
>>> s = pd.Series(range(5))
>>> s.where(s > 0)
0 NaN
1 1.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)
A B
0 0 -1
1 -2 3
2 -4 -5
3 6 -7
4 -8 9
>>> df.where(m, -df) == np.where(m, df, -df)
Copy link
Contributor

Choose a reason for hiding this comment

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

add a comment before this one

A B
0 True True
1 True True
2 True True
3 True True
4 True True
>>> df.where(m, -df) == df.mask(~m, -df)
A B
0 True True
1 True True
2 True True
3 True True
4 True True

See Also
--------
:func:`DataFrame.%(name_other)s`
""")

@Appender(_shared_docs['where'] % dict(_shared_doc_kwargs, cond="True"))
@Appender(_shared_docs['where'] % dict(_shared_doc_kwargs, cond="True",
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):

other = com._apply_if_callable(other, self)
return self._where(cond, other, inplace, axis, level, try_cast,
raise_on_error)

@Appender(_shared_docs['where'] % dict(_shared_doc_kwargs, cond="False"))
@Appender(_shared_docs['where'] % dict(_shared_doc_kwargs, cond="False",
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