@@ -4690,17 +4690,71 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
4690
4690
Returns
4691
4691
-------
4692
4692
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`
4693
4745
""" )
4694
4746
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' ))
4696
4749
def where (self , cond , other = np .nan , inplace = False , axis = None , level = None ,
4697
4750
try_cast = False , raise_on_error = True ):
4698
4751
4699
4752
other = com ._apply_if_callable (other , self )
4700
4753
return self ._where (cond , other , inplace , axis , level , try_cast ,
4701
4754
raise_on_error )
4702
4755
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' ))
4704
4758
def mask (self , cond , other = np .nan , inplace = False , axis = None , level = None ,
4705
4759
try_cast = False , raise_on_error = True ):
4706
4760
0 commit comments