From d7d23ca4f20ae387b7366899ceb80a449b36546a Mon Sep 17 00:00:00 2001 From: Sage Muhammad Abdullah Date: Sat, 15 Feb 2020 16:33:33 +0700 Subject: [PATCH 1/6] DOC: SA01 Add See Also section in Index.where --- pandas/core/indexes/base.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index c896e68f7a188..99f843fde5f56 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3948,6 +3948,10 @@ def where(self, cond, other=None): Returns ------- Index + + See Also + -------- + DataFrame.where : Replace values in a DataFrame where the condition is False. """ if other is None: other = self._na_value From 1586cc078404fdb81cd34b837d8210312908bfa7 Mon Sep 17 00:00:00 2001 From: Sage Muhammad Abdullah Date: Sat, 15 Feb 2020 17:04:13 +0700 Subject: [PATCH 2/6] DOC: PR07 Add descriptions for Index.where parameters --- pandas/core/indexes/base.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 99f843fde5f56..26c012882e12d 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3943,7 +3943,9 @@ def where(self, cond, other=None): Parameters ---------- cond : bool array-like with the same length as self - other : scalar, or array-like + Condition to select the entries on. + other : scalar, or array-like, default None + Replacement if the condition is False. Returns ------- From abeaf01bf9d1a0e0ef428c152a051acedaf3b24a Mon Sep 17 00:00:00 2001 From: Sage Muhammad Abdullah Date: Sat, 15 Feb 2020 17:13:54 +0700 Subject: [PATCH 3/6] DOC: RT03 Add return value description for Index.where --- pandas/core/indexes/base.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 26c012882e12d..99cad98d78519 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3949,7 +3949,9 @@ def where(self, cond, other=None): Returns ------- - Index + pandas.Index + Copy of self with entries replaced from other + where the condition is False. See Also -------- From 2d7af913842b97515766ba6b2a821cb3b22d9f73 Mon Sep 17 00:00:00 2001 From: Sage Muhammad Abdullah Date: Sat, 15 Feb 2020 17:28:01 +0700 Subject: [PATCH 4/6] DOC: SS06 Make summary for Index.where fit in one line --- pandas/core/indexes/base.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 99cad98d78519..0028c52342b6b 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3936,9 +3936,9 @@ def memory_usage(self, deep: bool = False) -> int: def where(self, cond, other=None): """ - Return an Index of same shape as self and whose corresponding - entries are from self where cond is True and otherwise are from - other. + Return a copy of self with entries replaced where cond is False. + + The replacement is taken from other. Parameters ---------- From 49c0c8d700b62bbd3d50e7eacf1f74524fd982f7 Mon Sep 17 00:00:00 2001 From: Sage Muhammad Abdullah Date: Sat, 15 Feb 2020 17:45:46 +0700 Subject: [PATCH 5/6] DOC: EX01 Add Examples section in Index.where --- pandas/core/indexes/base.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 0028c52342b6b..0cefe3eb3d9cb 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3956,6 +3956,17 @@ def where(self, cond, other=None): See Also -------- DataFrame.where : Replace values in a DataFrame where the condition is False. + + Examples + -------- + >>> idx1 = pd.Index([42, 21, 34, 96, 72]) + >>> idx1 + Int64Index([42, 21, 34, 96, 72], dtype='int64') + >>> idx2 = pd.Index([140, 150, 140, 190, 170]) + >>> idx2 + Int64Index([140, 150, 140, 190, 170], dtype='int64') + >>> idx1.where((idx2 - 100) > idx1, idx2) + Int64Index([140, 21, 34, 190, 170], dtype='int64') """ if other is None: other = self._na_value From 90c00c75c6de5b9ec7c5a1a7a8bdc2ad92cb25c7 Mon Sep 17 00:00:00 2001 From: Sage Muhammad Abdullah Date: Sun, 23 Feb 2020 08:12:19 +0700 Subject: [PATCH 6/6] DOC: Improve documentation for Index.where --- pandas/core/indexes/base.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 0cefe3eb3d9cb..6b6753ff31e29 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3936,37 +3936,35 @@ def memory_usage(self, deep: bool = False) -> int: def where(self, cond, other=None): """ - Return a copy of self with entries replaced where cond is False. + Replace values where the condition is False. The replacement is taken from other. Parameters ---------- cond : bool array-like with the same length as self - Condition to select the entries on. + Condition to select the values on. other : scalar, or array-like, default None Replacement if the condition is False. Returns ------- pandas.Index - Copy of self with entries replaced from other + A copy of self with values replaced from other where the condition is False. See Also -------- - DataFrame.where : Replace values in a DataFrame where the condition is False. + Series.where : Same method for Series. + DataFrame.where : Same method for DataFrame. Examples -------- - >>> idx1 = pd.Index([42, 21, 34, 96, 72]) - >>> idx1 - Int64Index([42, 21, 34, 96, 72], dtype='int64') - >>> idx2 = pd.Index([140, 150, 140, 190, 170]) - >>> idx2 - Int64Index([140, 150, 140, 190, 170], dtype='int64') - >>> idx1.where((idx2 - 100) > idx1, idx2) - Int64Index([140, 21, 34, 190, 170], dtype='int64') + >>> idx = pd.Index(['car', 'bike', 'train', 'tractor']) + >>> idx + Index(['car', 'bike', 'train', 'tractor'], dtype='object') + >>> idx.where(idx.isin(['car', 'train']), 'other') + Index(['car', 'other', 'train', 'other'], dtype='object') """ if other is None: other = self._na_value