Skip to content

Commit 5f49638

Browse files
jalexvigjorisvandenbossche
authored andcommitted
DOC: Added example and notes to NDFrame.where (pandas-dev#13798)
1 parent 5b0a8b0 commit 5f49638

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

doc/source/indexing.rst

+11
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,8 @@ To select a row where each column meets its own criterion:
828828
829829
df[row_mask]
830830
831+
.. _indexing.where_mask:
832+
831833
The :meth:`~pandas.DataFrame.where` Method and Masking
832834
------------------------------------------------------
833835

@@ -891,6 +893,15 @@ without creating a copy:
891893
df_orig.where(df > 0, -df, inplace=True);
892894
df_orig
893895
896+
.. note::
897+
898+
The signature for :func:`DataFrame.where` differs from :func:`numpy.where`.
899+
Roughly ``df1.where(m, df2)`` is equivalent to ``np.where(m, df1, df2)``.
900+
901+
.. ipython:: python
902+
903+
df.where(df < 0, -df) == np.where(df < 0, df, -df)
904+
894905
**alignment**
895906

896907
Furthermore, ``where`` aligns the input boolean condition (ndarray or DataFrame),

pandas/core/generic.py

+56-2
Original file line numberDiff line numberDiff line change
@@ -4690,17 +4690,71 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
46904690
Returns
46914691
-------
46924692
wh : same type as caller
4693+
4694+
Notes
4695+
-----
4696+
The %(name)s method is an application of the if-then idiom. For each
4697+
element in the calling DataFrame, if ``cond`` is ``%(cond)s`` the
4698+
element is used; otherwise the corresponding element from the DataFrame
4699+
``other`` is used.
4700+
4701+
The signature for :func:`DataFrame.where` differs from
4702+
:func:`numpy.where`. Roughly ``df1.where(m, df2)`` is equivalent to
4703+
``np.where(m, df1, df2)``.
4704+
4705+
For further details and examples see the ``%(name)s`` documentation in
4706+
:ref:`indexing <indexing.where_mask>`.
4707+
4708+
Examples
4709+
--------
4710+
>>> s = pd.Series(range(5))
4711+
>>> s.where(s > 0)
4712+
0 NaN
4713+
1 1.0
4714+
2 2.0
4715+
3 3.0
4716+
4 4.0
4717+
4718+
>>> df = pd.DataFrame(np.arange(10).reshape(-1, 2), columns=['A', 'B'])
4719+
>>> m = df %% 3 == 0
4720+
>>> df.where(m, -df)
4721+
A B
4722+
0 0 -1
4723+
1 -2 3
4724+
2 -4 -5
4725+
3 6 -7
4726+
4 -8 9
4727+
>>> df.where(m, -df) == np.where(m, df, -df)
4728+
A B
4729+
0 True True
4730+
1 True True
4731+
2 True True
4732+
3 True True
4733+
4 True True
4734+
>>> df.where(m, -df) == df.mask(~m, -df)
4735+
A B
4736+
0 True True
4737+
1 True True
4738+
2 True True
4739+
3 True True
4740+
4 True True
4741+
4742+
See Also
4743+
--------
4744+
:func:`DataFrame.%(name_other)s`
46934745
""")
46944746

4695-
@Appender(_shared_docs['where'] % dict(_shared_doc_kwargs, cond="True"))
4747+
@Appender(_shared_docs['where'] % dict(_shared_doc_kwargs, cond="True",
4748+
name='where', name_other='mask'))
46964749
def where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
46974750
try_cast=False, raise_on_error=True):
46984751

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

4703-
@Appender(_shared_docs['where'] % dict(_shared_doc_kwargs, cond="False"))
4756+
@Appender(_shared_docs['where'] % dict(_shared_doc_kwargs, cond="False",
4757+
name='mask', name_other='where'))
47044758
def mask(self, cond, other=np.nan, inplace=False, axis=None, level=None,
47054759
try_cast=False, raise_on_error=True):
47064760

0 commit comments

Comments
 (0)