From 089700bec461c25113764c713ceb9410615875a6 Mon Sep 17 00:00:00 2001 From: Cecilia Isven Date: Fri, 3 Aug 2018 09:44:10 +0100 Subject: [PATCH 1/5] Update docstring for Series.str.len --- pandas/core/strings.py | 46 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 6349af4d2e0ac..3bb0457c8ccd0 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -2801,11 +2801,53 @@ def rindex(self, sub, start=0, end=None): return self._wrap_result(result) _shared_docs['len'] = (""" - Compute length of each string in the Series/Index. + Compute length of each element in the Series/Index. The element may be + a sequence (such as a string, tuple or list) or a collection + (such as a dictionary). Returns ------- - lengths : Series/Index of integer values + Series or Index of integer values + A Series or Index of integer values indicating the length of each + element in the Series or Index. + + See Also + -------- + str.len : Python built-in function returning the length (number of items) of an object. + + Examples + -------- + + Returning a series of integer values as floats when `NaN` is returned as a + result. + + >>> s = pd.Series(['dog', 5, 'bird', np.nan]) + >>> s + 0 dog + 1 5 + 2 bird + 3 NaN + dtype: object + >>> s.str.len() + 0 3.0 + 1 NaN + 2 4.0 + 3 NaN + dtype: float64 + + Returning the length (number of entries) of dictionaries, lists or tuples as integer values. + + >>> s = pd.Series([{'foo':'bar'}, [2,3,5,7], ('one', 'two', 'three')]) + >>> s + 0 {'foo': 'bar'} + 1 [1, 3, 5, 7] + 2 (one, two, three) + dtype: object + >>> s.str.len() + 0 1 + 1 4 + 2 3 + dtype: int64 """) len = _noarg_wrapper(len, docstring=_shared_docs['len'], dtype=int) From b72edeca247ca41eba2510bbee3bb911a0b249d7 Mon Sep 17 00:00:00 2001 From: Cecilia Isven Date: Fri, 3 Aug 2018 09:59:58 +0100 Subject: [PATCH 2/5] Style changes to conform with PEP-8 --- pandas/core/strings.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 3bb0457c8ccd0..a3085620c3571 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -2813,7 +2813,7 @@ def rindex(self, sub, start=0, end=None): See Also -------- - str.len : Python built-in function returning the length (number of items) of an object. + str.len : Python built-in function returning the length of an object. Examples -------- @@ -2835,7 +2835,8 @@ def rindex(self, sub, start=0, end=None): 3 NaN dtype: float64 - Returning the length (number of entries) of dictionaries, lists or tuples as integer values. + Returning the length (number of entries) of dictionaries, lists or + tuples as integer values. >>> s = pd.Series([{'foo':'bar'}, [2,3,5,7], ('one', 'two', 'three')]) >>> s From 05ec61ab047726ecd013e7c286dacc1618de1299 Mon Sep 17 00:00:00 2001 From: Cecilia Isven Date: Mon, 6 Aug 2018 17:35:10 +0100 Subject: [PATCH 3/5] Add See Also, consolidate examples, improve syntax --- pandas/core/strings.py | 44 ++++++++++++++++-------------------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index a3085620c3571..c91830bc0be19 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -2807,48 +2807,38 @@ def rindex(self, sub, start=0, end=None): Returns ------- - Series or Index of integer values + Series or Index of int A Series or Index of integer values indicating the length of each element in the Series or Index. See Also -------- str.len : Python built-in function returning the length of an object. + Series.size : Returns the length of the Series. Examples -------- + Returning the length (number of characters) in a string. Returning the + number of entries for dictionaries, lists or tuples. - Returning a series of integer values as floats when `NaN` is returned as a - result. - - >>> s = pd.Series(['dog', 5, 'bird', np.nan]) + >>> s = pd.Series(['dog', '', 5, {'foo' : 'bar'}, [2, 3, 5, 7], \ + ('one', 'two', 'three')]) >>> s - 0 dog - 1 5 - 2 bird - 3 NaN + 0 dog + 1 + 2 5 + 3 {'foo': 'bar'} + 4 [2, 3, 5, 7] + 5 (one, two, three) dtype: object >>> s.str.len() 0 3.0 - 1 NaN - 2 4.0 - 3 NaN + 1 0.0 + 2 NaN + 3 1.0 + 4 4.0 + 5 3.0 dtype: float64 - - Returning the length (number of entries) of dictionaries, lists or - tuples as integer values. - - >>> s = pd.Series([{'foo':'bar'}, [2,3,5,7], ('one', 'two', 'three')]) - >>> s - 0 {'foo': 'bar'} - 1 [1, 3, 5, 7] - 2 (one, two, three) - dtype: object - >>> s.str.len() - 0 1 - 1 4 - 2 3 - dtype: int64 """) len = _noarg_wrapper(len, docstring=_shared_docs['len'], dtype=int) From 218f6fed784f104b4e310aa3330018283068375b Mon Sep 17 00:00:00 2001 From: Cecilia Isven Date: Mon, 6 Aug 2018 17:40:45 +0100 Subject: [PATCH 4/5] Improve readability --- pandas/core/strings.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index c91830bc0be19..a01f39799a31d 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -2801,7 +2801,7 @@ def rindex(self, sub, start=0, end=None): return self._wrap_result(result) _shared_docs['len'] = (""" - Compute length of each element in the Series/Index. The element may be + Compute the length of each element in the Series/Index. The element may be a sequence (such as a string, tuple or list) or a collection (such as a dictionary). @@ -2821,8 +2821,7 @@ def rindex(self, sub, start=0, end=None): Returning the length (number of characters) in a string. Returning the number of entries for dictionaries, lists or tuples. - >>> s = pd.Series(['dog', '', 5, {'foo' : 'bar'}, [2, 3, 5, 7], \ - ('one', 'two', 'three')]) + >>> s = pd.Series(['dog', '', 5, {'foo' : 'bar'}, [2, 3, 5, 7], ('one', 'two', 'three')]) >>> s 0 dog 1 From f395314e72c41bd72b841578fdcb4c8cbb99bca5 Mon Sep 17 00:00:00 2001 From: Cecilia Isven Date: Mon, 6 Aug 2018 17:54:48 +0100 Subject: [PATCH 5/5] Change series definition structure --- pandas/core/strings.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index a01f39799a31d..6ef736d8a0b09 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -2801,7 +2801,7 @@ def rindex(self, sub, start=0, end=None): return self._wrap_result(result) _shared_docs['len'] = (""" - Compute the length of each element in the Series/Index. The element may be + Computes the length of each element in the Series/Index. The element may be a sequence (such as a string, tuple or list) or a collection (such as a dictionary). @@ -2818,10 +2818,15 @@ def rindex(self, sub, start=0, end=None): Examples -------- - Returning the length (number of characters) in a string. Returning the + Returns the length (number of characters) in a string. Returns the number of entries for dictionaries, lists or tuples. - >>> s = pd.Series(['dog', '', 5, {'foo' : 'bar'}, [2, 3, 5, 7], ('one', 'two', 'three')]) + >>> s = pd.Series(['dog', + ... '', + ... 5, + ... {'foo' : 'bar'}, + ... [2, 3, 5, 7], + ... ('one', 'two', 'three')]) >>> s 0 dog 1