From a54a326e309a265c45ad07e44db76b61e517e9c3 Mon Sep 17 00:00:00 2001 From: Cheuk Ting Ho Date: Sun, 29 Apr 2018 11:58:12 +0100 Subject: [PATCH 1/8] DOC: update the pandas.Series.str.strip docstring --- pandas/core/strings.py | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index c6d45ce5413ac..c18ed789b8d66 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -2151,12 +2151,45 @@ def encode(self, encoding, errors="strict"): return self._wrap_result(result) _shared_docs['str_strip'] = (""" - Strip whitespace (including newlines) from each string in the - Series/Index from %(side)s. Equivalent to :meth:`str.%(method)s`. + Remove leading and trailing characters. + + Strip whitespaces (including newlines) or a set of specified characters + from each string in the Series/Index from %(side)s. + Equivalent to :meth:`str.%(method)s`. + + Parameters + ---------- + to_strip : str + specifying the set of characters to be removed. + All combinations of this set of characters will be stripped. + Default value is None, which means whaitspaces will be removed. Returns ------- stripped : Series/Index of objects + + Examples + -------- + Striping whitespaces for Series + + >>>s = pd.Series([' ant', 'bee ', ' cat ']) + >>>s + 0 ant + 1 bee + 2 cat + dtype: object + >>>s.str.strip() + 0 ant + 1 bee + 2 cat + dtype: object + + Striping a set of characters for Index + >>>df = pd.DataFrame(index=['1.ant ','2._bee__','3. cat_']) + >>>pd.index + Index(['1.ant ', '2._bee__', '3. cat_'], dtype='object') + >>>df.index.str.strip('123._ ') + Index(['ant', 'bee', 'cat'], dtype='object') """) @Appender(_shared_docs['str_strip'] % dict(side='left and right sides', From fee3c610edd5ccfb442c87918316b1f559e31fa6 Mon Sep 17 00:00:00 2001 From: Cheuk Ting Ho Date: Sun, 29 Apr 2018 12:20:36 +0100 Subject: [PATCH 2/8] DOC: update the pandas.Series.str.strip docstring --- pandas/core/strings.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index c18ed789b8d66..bc840468a7196 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -2160,7 +2160,7 @@ def encode(self, encoding, errors="strict"): Parameters ---------- to_strip : str - specifying the set of characters to be removed. + Specifying the set of characters to be removed. All combinations of this set of characters will be stripped. Default value is None, which means whaitspaces will be removed. @@ -2168,27 +2168,36 @@ def encode(self, encoding, errors="strict"): ------- stripped : Series/Index of objects + See Also + -------- + str.slice : Slice substrings from each element in the Series/Index + Examples -------- Striping whitespaces for Series - >>>s = pd.Series([' ant', 'bee ', ' cat ']) - >>>s + >>> s = pd.Series([' ant', 'bee ', ' cat ']) + + >>> s 0 ant 1 bee 2 cat dtype: object - >>>s.str.strip() + + >>> s.str.strip() 0 ant 1 bee 2 cat dtype: object Striping a set of characters for Index - >>>df = pd.DataFrame(index=['1.ant ','2._bee__','3. cat_']) - >>>pd.index + + >>> df = pd.DataFrame(index=['1.ant ','2._bee__','3. cat_']) + + >>> df.index Index(['1.ant ', '2._bee__', '3. cat_'], dtype='object') - >>>df.index.str.strip('123._ ') + + >>> df.index.str.strip('123._ ') Index(['ant', 'bee', 'cat'], dtype='object') """) From 8b365120cf2c736810adf11a8e99f043cbcfa8d1 Mon Sep 17 00:00:00 2001 From: Cheuk Ting Ho Date: Mon, 30 Apr 2018 08:46:20 +0100 Subject: [PATCH 3/8] typo fixing and improve Index example --- pandas/core/strings.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index bc840468a7196..3021bb8a9ca34 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -2174,7 +2174,7 @@ def encode(self, encoding, errors="strict"): Examples -------- - Striping whitespaces for Series + Stripping whitespaces for Series >>> s = pd.Series([' ant', 'bee ', ' cat ']) @@ -2190,14 +2190,14 @@ def encode(self, encoding, errors="strict"): 2 cat dtype: object - Striping a set of characters for Index + Stripping a set of characters for Index - >>> df = pd.DataFrame(index=['1.ant ','2._bee__','3. cat_']) + >>> idx = pd.Index(['1.ant ','2._bee__','3. cat_']) - >>> df.index + >>> idx Index(['1.ant ', '2._bee__', '3. cat_'], dtype='object') - >>> df.index.str.strip('123._ ') + >>> idx.str.strip('123._ ') Index(['ant', 'bee', 'cat'], dtype='object') """) From 660b4fcfe2b5c6938f2fe2a287fa734352de23f4 Mon Sep 17 00:00:00 2001 From: Cheuk Ting Ho Date: Mon, 30 Apr 2018 08:50:04 +0100 Subject: [PATCH 4/8] Fix PEP8 --- pandas/core/strings.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 3021bb8a9ca34..08e6cc3b00924 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -2160,7 +2160,7 @@ def encode(self, encoding, errors="strict"): Parameters ---------- to_strip : str - Specifying the set of characters to be removed. + Specifying the set of characters to be removed. All combinations of this set of characters will be stripped. Default value is None, which means whaitspaces will be removed. @@ -2180,8 +2180,8 @@ def encode(self, encoding, errors="strict"): >>> s 0 ant - 1 bee - 2 cat + 1 bee + 2 cat dtype: object >>> s.str.strip() From e6fe9214dbff277083bafb9dcae2492f6d57fcb7 Mon Sep 17 00:00:00 2001 From: Cheuk Ting Ho Date: Mon, 30 Apr 2018 08:52:28 +0100 Subject: [PATCH 5/8] Improve parameter explaination --- pandas/core/strings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 08e6cc3b00924..5381d4970d537 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -2159,7 +2159,7 @@ def encode(self, encoding, errors="strict"): Parameters ---------- - to_strip : str + to_strip : str, optional Specifying the set of characters to be removed. All combinations of this set of characters will be stripped. Default value is None, which means whaitspaces will be removed. From fd3eda26da04142bf7bdece6e664ac2c2d0cec53 Mon Sep 17 00:00:00 2001 From: Cheuk Ting Ho Date: Mon, 30 Apr 2018 22:55:23 +0100 Subject: [PATCH 6/8] Improve documentation, adding lstrip and rstrip example --- pandas/core/strings.py | 48 ++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 5381d4970d537..8dde122567be1 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -2159,46 +2159,52 @@ def encode(self, encoding, errors="strict"): Parameters ---------- - to_strip : str, optional + to_strip : str or None, default None (whitespaces are removed) Specifying the set of characters to be removed. All combinations of this set of characters will be stripped. - Default value is None, which means whaitspaces will be removed. Returns ------- - stripped : Series/Index of objects + Series/Index of objects See Also -------- - str.slice : Slice substrings from each element in the Series/Index + Series.str.strip : Remove leading and trailing characters in Series/Index + Series.str.lstrip : Remove leading characters in Series/Index + Series.str.rstrip : Remove trailing characters in Series/Index Examples -------- - Stripping whitespaces for Series - - >>> s = pd.Series([' ant', 'bee ', ' cat ']) - + >>> s = pd.Series(['1. Ant. ', '2. Bee!\n', '3. Cat?\t']) >>> s - 0 ant - 1 bee - 2 cat + 0 1. Ant. + 1 2. Bee!\n + 2 3. Cat?\t dtype: object >>> s.str.strip() - 0 ant - 1 bee - 2 cat + 0 1. Ant. + 1 2. Bee! + 2 3. Cat? dtype: object - Stripping a set of characters for Index - - >>> idx = pd.Index(['1.ant ','2._bee__','3. cat_']) + >>> s.str.strip('123.!? \n\t') + 0 Ant + 1 Bee + 2 Cat + dtype: object - >>> idx - Index(['1.ant ', '2._bee__', '3. cat_'], dtype='object') + >>> s.str.lstrip('123.!? \n\t') + 0 Ant. + 1 Bee!\n + 2 Cat?\t + dtype: object - >>> idx.str.strip('123._ ') - Index(['ant', 'bee', 'cat'], dtype='object') + >>> s.str.rstrip('123.!? \n\t') + 0 1. Ant + 1 2. Bee + 2 3. Cat + dtype: object """) @Appender(_shared_docs['str_strip'] % dict(side='left and right sides', From c69b1e37a272f1477aa2bec353df4647e58275a0 Mon Sep 17 00:00:00 2001 From: Cheuk Ting Ho Date: Tue, 1 May 2018 21:40:40 +0100 Subject: [PATCH 7/8] simplify lstrip and rstrip --- pandas/core/strings.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 8dde122567be1..471722cc22624 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -2188,23 +2188,23 @@ def encode(self, encoding, errors="strict"): 2 3. Cat? dtype: object - >>> s.str.strip('123.!? \n\t') - 0 Ant - 1 Bee - 2 Cat - dtype: object - - >>> s.str.lstrip('123.!? \n\t') + >>> s.str.lstrip('123.') 0 Ant. 1 Bee!\n 2 Cat?\t dtype: object - >>> s.str.rstrip('123.!? \n\t') + >>> s.str.rstrip('.!? \n\t') 0 1. Ant 1 2. Bee 2 3. Cat dtype: object + + >>> s.str.strip('123.!? \n\t') + 0 Ant + 1 Bee + 2 Cat + dtype: object """) @Appender(_shared_docs['str_strip'] % dict(side='left and right sides', From 5160febe39327863bfdc8613147cee8b1713892c Mon Sep 17 00:00:00 2001 From: Cheuk Ting Ho Date: Sat, 5 May 2018 11:43:28 +0100 Subject: [PATCH 8/8] added np.nan example --- pandas/core/strings.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 471722cc22624..b52064e31f8b2 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -2159,9 +2159,10 @@ def encode(self, encoding, errors="strict"): Parameters ---------- - to_strip : str or None, default None (whitespaces are removed) + to_strip : str or None, default None. Specifying the set of characters to be removed. All combinations of this set of characters will be stripped. + If None then whitespaces are removed. Returns ------- @@ -2175,35 +2176,40 @@ def encode(self, encoding, errors="strict"): Examples -------- - >>> s = pd.Series(['1. Ant. ', '2. Bee!\n', '3. Cat?\t']) + >>> s = pd.Series(['1. Ant. ', '2. Bee!\n', '3. Cat?\t', np.nan]) >>> s 0 1. Ant. 1 2. Bee!\n 2 3. Cat?\t + 3 NaN dtype: object >>> s.str.strip() 0 1. Ant. 1 2. Bee! 2 3. Cat? + 3 NaN dtype: object >>> s.str.lstrip('123.') 0 Ant. 1 Bee!\n 2 Cat?\t + 3 NaN dtype: object >>> s.str.rstrip('.!? \n\t') 0 1. Ant 1 2. Bee 2 3. Cat + 3 NaN dtype: object >>> s.str.strip('123.!? \n\t') 0 Ant 1 Bee 2 Cat + 3 NaN dtype: object """)