From 52bc395c4efc64a66ca1d067ec71912e290bbb08 Mon Sep 17 00:00:00 2001 From: Jesper Dramsch Date: Sat, 1 Sep 2018 15:16:18 +0200 Subject: [PATCH 01/11] Cleaned up docstring for str_pad and corrected documentation to match behaviour. Added examples --- pandas/core/strings.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index e455c751057d1..d209a80cc4e3e 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1314,8 +1314,8 @@ def str_index(arr, sub, start=0, end=None, side='left'): def str_pad(arr, width, side='left', fillchar=' '): """ - Pad strings in the Series/Index with an additional character to - specified side. + Pad strings in the Series/Index with additional characters on + specified side to fill up to specified width. Parameters ---------- @@ -1323,12 +1323,32 @@ def str_pad(arr, width, side='left', fillchar=' '): Minimum width of resulting string; additional characters will be filled with spaces side : {'left', 'right', 'both'}, default 'left' - fillchar : str + fillchar : str, default ' ' Additional character for filling, default is whitespace Returns ------- - padded : Series/Index of objects + Series or Index of objects + + Examples + -------- + >>> s = pd.Series(["panda", "fox"]) + >>> s + 0 panda + 1 fox + + >>> s.str.pad(10) + 0 panda + 1 fox + + >>> s.str.pad(10, 'right') + 0 panda + 1 fox + + >>> s.str.pad(10, 'both', '-') + 0 --panda--- + 1 ---fox---- + """ if not isinstance(fillchar, compat.string_types): From 5d5b192f409b4c4145a133e75de571d872801833 Mon Sep 17 00:00:00 2001 From: Jesper Dramsch Date: Sat, 1 Sep 2018 15:18:48 +0200 Subject: [PATCH 02/11] PEP8-ing --- pandas/core/strings.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index d209a80cc4e3e..898a362ad586a 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1336,7 +1336,7 @@ def str_pad(arr, width, side='left', fillchar=' '): >>> s 0 panda 1 fox - + >>> s.str.pad(10) 0 panda 1 fox @@ -1348,7 +1348,6 @@ def str_pad(arr, width, side='left', fillchar=' '): >>> s.str.pad(10, 'both', '-') 0 --panda--- 1 ---fox---- - """ if not isinstance(fillchar, compat.string_types): From f9f8f8be3a6d7b31d0588daf1543fac275ddd038 Mon Sep 17 00:00:00 2001 From: Jesper Dramsch Date: Sat, 1 Sep 2018 16:03:13 +0200 Subject: [PATCH 03/11] Add examples and description for str.slice --- pandas/core/strings.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 898a362ad586a..3796abafada2b 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1404,17 +1404,40 @@ def str_rsplit(arr, pat=None, n=None): def str_slice(arr, start=None, stop=None, step=None): """ - Slice substrings from each element in the Series/Index + Slice substrings from each element in the Series or Index Parameters ---------- start : int or None + Start position for slice operation stop : int or None + Stop position for slice operation step : int or None + Step size for slice operation Returns ------- - sliced : Series/Index of objects + Series or Index of object + Series or Index containing sliced substring from original string + + Examples + -------- + >>> s = pd.Series(["panda", "fox"]) + >>> s + 0 panda + 1 fox + + >>> s.str.slice(start=2) + 0 nda + 1 x + + >>> s.str.slice(stop=2) + 0 pa + 1 fo + + >>> s.str.slice(step=2) + 0 pna + 1 fx """ obj = slice(start, stop, step) f = lambda x: x[obj] From 0a40a10b78d4905770f9f49495adc48e29233048 Mon Sep 17 00:00:00 2001 From: Jesper Dramsch Date: Sat, 1 Sep 2018 16:17:24 +0200 Subject: [PATCH 04/11] Decluttering the changes. Sorry --- pandas/core/strings.py | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 3796abafada2b..4569050624399 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1314,8 +1314,8 @@ def str_index(arr, sub, start=0, end=None, side='left'): def str_pad(arr, width, side='left', fillchar=' '): """ - Pad strings in the Series/Index with additional characters on - specified side to fill up to specified width. + Pad strings in the Series/Index with an additional character to + specified side. Parameters ---------- @@ -1323,31 +1323,12 @@ def str_pad(arr, width, side='left', fillchar=' '): Minimum width of resulting string; additional characters will be filled with spaces side : {'left', 'right', 'both'}, default 'left' - fillchar : str, default ' ' + fillchar : str Additional character for filling, default is whitespace Returns ------- - Series or Index of objects - - Examples - -------- - >>> s = pd.Series(["panda", "fox"]) - >>> s - 0 panda - 1 fox - - >>> s.str.pad(10) - 0 panda - 1 fox - - >>> s.str.pad(10, 'right') - 0 panda - 1 fox - - >>> s.str.pad(10, 'both', '-') - 0 --panda--- - 1 ---fox---- + padded : Series/Index of objects """ if not isinstance(fillchar, compat.string_types): From 26e354ba2b079ba16f32435b6d58eef640fe11ac Mon Sep 17 00:00:00 2001 From: Jesper Dramsch Date: Sat, 1 Sep 2018 16:23:16 +0200 Subject: [PATCH 05/11] Some fixes --- 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 4569050624399..c2f92e5986fbb 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1399,7 +1399,7 @@ def str_slice(arr, start=None, stop=None, step=None): Returns ------- Series or Index of object - Series or Index containing sliced substring from original string + Series or Index containing sliced substring from original string object Examples -------- From 6384d10d42bf1be6833c3157a3fea14478b9114f Mon Sep 17 00:00:00 2001 From: Jesper Dramsch Date: Fri, 28 Sep 2018 14:50:01 +0100 Subject: [PATCH 06/11] Updates Examples and added periods --- pandas/core/strings.py | 48 +++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index c2f92e5986fbb..91cf8e1f7bb75 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1385,40 +1385,54 @@ def str_rsplit(arr, pat=None, n=None): def str_slice(arr, start=None, stop=None, step=None): """ - Slice substrings from each element in the Series or Index + Slice substrings from each element in the Series or Index. Parameters ---------- - start : int or None - Start position for slice operation - stop : int or None - Stop position for slice operation - step : int or None - Step size for slice operation + start : int, optional + Start position for slice operation. + stop : int, optional + Stop position for slice operation. + step : int, optional + Step size for slice operation. Returns ------- Series or Index of object - Series or Index containing sliced substring from original string object + Series or Index containing sliced substring from original string object. Examples -------- - >>> s = pd.Series(["panda", "fox"]) + >>> s = pd.Series(["koala", "fox", "chameleon"]) >>> s - 0 panda - 1 fox + 0 koala + 1 fox + 2 chameleon + dtype: object - >>> s.str.slice(start=2) - 0 nda - 1 x + >>> s.str.slice(start=1) + 0 oala + 1 ox + 2 hameleon + dtype: object >>> s.str.slice(stop=2) - 0 pa + 0 ko 1 fo + 2 ch + dtype: object >>> s.str.slice(step=2) - 0 pna - 1 fx + 0 kaa + 1 fx + 2 caeen + dtype: object + + >>> s.str.slice(start=0, stop=5, step=3) + 0 kl + 1 f + 2 cm + dtype: object """ obj = slice(start, stop, step) f = lambda x: x[obj] From 756db731ca2fe68a7780955ef9b69afe030cd37f Mon Sep 17 00:00:00 2001 From: Jesper Dramsch Date: Fri, 28 Sep 2018 15:04:00 +0100 Subject: [PATCH 07/11] Slicing and See Also --- pandas/core/strings.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 91cf8e1f7bb75..ed76eae838565 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1401,6 +1401,12 @@ def str_slice(arr, start=None, stop=None, step=None): Series or Index of object Series or Index containing sliced substring from original string object. + See Also + -------- + Series.str.slice_replace : Replace a slice with a string. + Series.str.get : Return element at position. + Equivalent to `Series.str.slice(start=i, stop=i+1)` with `i` being the position. + Examples -------- >>> s = pd.Series(["koala", "fox", "chameleon"]) @@ -1433,6 +1439,13 @@ def str_slice(arr, start=None, stop=None, step=None): 1 f 2 cm dtype: object + + Equivalent behaviour to: + >>> s.str[0:5:3] + 0 kl + 1 f + 2 cm + dtype: object """ obj = slice(start, stop, step) f = lambda x: x[obj] From 91b699684815565ba306403126d21ae87592346f Mon Sep 17 00:00:00 2001 From: Jesper Dramsch Date: Fri, 28 Sep 2018 15:06:48 +0100 Subject: [PATCH 08/11] PEP8 --- pandas/core/strings.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index ed76eae838565..984e23a5a78ff 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1399,13 +1399,14 @@ def str_slice(arr, start=None, stop=None, step=None): Returns ------- Series or Index of object - Series or Index containing sliced substring from original string object. + Series or Index from sliced substring from original string object. See Also -------- Series.str.slice_replace : Replace a slice with a string. - Series.str.get : Return element at position. - Equivalent to `Series.str.slice(start=i, stop=i+1)` with `i` being the position. + Series.str.get : Return element at position. + Equivalent to `Series.str.slice(start=i, stop=i+1)` with `i` + being the position. Examples -------- From 6cd73bfdc9a839bdcc780657fa0820a352a65cf5 Mon Sep 17 00:00:00 2001 From: Jesper Dramsch Date: Fri, 28 Sep 2018 15:08:09 +0100 Subject: [PATCH 09/11] Trailing whitespace --- 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 984e23a5a78ff..36c5b196bc68d 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1405,7 +1405,7 @@ def str_slice(arr, start=None, stop=None, step=None): -------- Series.str.slice_replace : Replace a slice with a string. Series.str.get : Return element at position. - Equivalent to `Series.str.slice(start=i, stop=i+1)` with `i` + Equivalent to `Series.str.slice(start=i, stop=i+1)` with `i` being the position. Examples From 2f05b9aa903cd75a21e818110cd6dbc211a7c0b6 Mon Sep 17 00:00:00 2001 From: Jesper Dramsch Date: Tue, 2 Oct 2018 15:19:45 +0100 Subject: [PATCH 10/11] Newline --- pandas/core/strings.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 36c5b196bc68d..20854287c0620 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1442,6 +1442,7 @@ def str_slice(arr, start=None, stop=None, step=None): dtype: object Equivalent behaviour to: + >>> s.str[0:5:3] 0 kl 1 f From d49a2e7dc2cd257a3e16055d313c60dff61b2fd9 Mon Sep 17 00:00:00 2001 From: Jesper Dramsch Date: Wed, 3 Oct 2018 23:27:01 +0200 Subject: [PATCH 11/11] Make Travis happy --- 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 2cdb6f7d322b7..5a23951145cb4 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1494,7 +1494,7 @@ def str_slice(arr, start=None, stop=None, step=None): dtype: object Equivalent behaviour to: - + >>> s.str[0:5:3] 0 kl 1 f