From f9d9b4a15038546e7f5f421336924f68a1508604 Mon Sep 17 00:00:00 2001 From: Jenna Vergeynst Date: Mon, 25 Sep 2017 13:55:49 +0200 Subject: [PATCH 1/3] modify docstring of function where --- pandas/core/generic.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 3d55e07df6eac..12183fdb76c72 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5822,13 +5822,15 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None, _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 True, keep the original value. Where False, 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). @@ -5836,6 +5838,8 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None, A callable can be used as cond. other : scalar, %(klass)s, or callable + Entries where `cond` is False 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). @@ -5881,8 +5885,15 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None, 3 3.0 4 4.0 + >>> 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 + >>> m = df % 3 == 0 >>> df.where(m, -df) A B 0 0 -1 From 4b1e882bd64f73c739c68e2cac73b17fcca11f81 Mon Sep 17 00:00:00 2001 From: Jenna Vergeynst Date: Mon, 25 Sep 2017 14:47:05 +0200 Subject: [PATCH 2/3] modified shared docstring where-mask --- pandas/core/generic.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 12183fdb76c72..92af56d384765 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5828,7 +5828,7 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None, Parameters ---------- cond : boolean %(klass)s, array-like, or callable - Where `cond` is True, keep the original value. Where False, replace + 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 @@ -5838,7 +5838,7 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None, A callable can be used as cond. other : scalar, %(klass)s, or callable - Entries where `cond` is False are replaced with corresponding + 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 @@ -5885,7 +5885,14 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None, 3 3.0 4 4.0 - >>> s.where(s>1, 10) + >>> 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 @@ -5893,7 +5900,7 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None, 4 4.0 >>> df = pd.DataFrame(np.arange(10).reshape(-1, 2), columns=['A', 'B']) - >>> m = df % 3 == 0 + >>> m = df %% 3 == 0 >>> df.where(m, -df) A B 0 0 -1 @@ -5922,7 +5929,7 @@ 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): @@ -5931,7 +5938,7 @@ 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): From cf8f0811211d97f81e000f8e0b8fb1842ca367d0 Mon Sep 17 00:00:00 2001 From: Jenna Vergeynst Date: Mon, 25 Sep 2017 14:53:51 +0200 Subject: [PATCH 3/3] solved PEP8 issues --- pandas/core/generic.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 92af56d384765..8e2a91ee9fd61 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5828,8 +5828,8 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None, Parameters ---------- cond : boolean %(klass)s, array-like, or callable - Where `cond` is %(cond)s, keep the original value. Where %(cond_rev)s, replace - with corresponding value from `other`. + 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). @@ -5838,8 +5838,8 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None, 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`. + 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). @@ -5929,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", - cond_rev="False", 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): @@ -5938,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", - cond_rev="True", 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):