From 52bc395c4efc64a66ca1d067ec71912e290bbb08 Mon Sep 17 00:00:00 2001 From: Jesper Dramsch Date: Sat, 1 Sep 2018 15:16:18 +0200 Subject: [PATCH 01/13] 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/13] 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 28ec7b0b4d170025a2c8813fadf88842f74dcad2 Mon Sep 17 00:00:00 2001 From: Jesper Dramsch Date: Sat, 1 Sep 2018 15:25:16 +0200 Subject: [PATCH 03/13] PEP8-ing example --- 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 898a362ad586a..9d9ed65b5ffcb 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1341,9 +1341,9 @@ def str_pad(arr, width, side='left', fillchar=' '): 0 panda 1 fox - >>> s.str.pad(10, 'right') - 0 panda - 1 fox + >>> s.str.pad(10, 'right', '-') + 0 panda----- + 1 fox------- >>> s.str.pad(10, 'both', '-') 0 --panda--- From 8337a530f1145c13ad1a2335270669b444756b0a Mon Sep 17 00:00:00 2001 From: Jesper Dramsch Date: Sat, 1 Sep 2018 15:54:09 +0200 Subject: [PATCH 04/13] Explicit names and some fixes --- pandas/core/strings.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 9d9ed65b5ffcb..98216c3a7455e 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1321,14 +1321,16 @@ def str_pad(arr, width, side='left', fillchar=' '): ---------- width : int Minimum width of resulting string; additional characters will be filled - with spaces + with character defined in fillchar side : {'left', 'right', 'both'}, default 'left' + Side from which to fill resulting string fillchar : str, default ' ' Additional character for filling, default is whitespace Returns ------- - Series or Index of objects + Series or Index of object + Returns Series or Index with minimum number of char in object Examples -------- @@ -1337,15 +1339,15 @@ def str_pad(arr, width, side='left', fillchar=' '): 0 panda 1 fox - >>> s.str.pad(10) + >>> s.str.pad(width=10) 0 panda 1 fox - >>> s.str.pad(10, 'right', '-') + >>> s.str.pad(width=10, side='right', fillchar='-') 0 panda----- 1 fox------- - >>> s.str.pad(10, 'both', '-') + >>> s.str.pad(width=10, side='both', fillchar='-') 0 --panda--- 1 ---fox---- """ From 0224b12b66331c2cd761433364d7d5336ec77c77 Mon Sep 17 00:00:00 2001 From: Jesper Dramsch Date: Sun, 2 Sep 2018 11:35:47 +0200 Subject: [PATCH 05/13] Shorten first line --- 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 98216c3a7455e..2ec3f92c5fe0b 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1314,8 +1314,7 @@ 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 up to width Parameters ---------- From 763f3c37418c3ab6e4d0f63135f56d182bfc35e1 Mon Sep 17 00:00:00 2001 From: Jesper Dramsch Date: Sun, 2 Sep 2018 11:47:38 +0200 Subject: [PATCH 06/13] Add periods --- pandas/core/strings.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 2ec3f92c5fe0b..7943b9b635c4e 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1314,17 +1314,17 @@ 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 up to width + Pad strings in the Series/Index up to width. Parameters ---------- width : int Minimum width of resulting string; additional characters will be filled - with character defined in fillchar + with character defined in fillchar. side : {'left', 'right', 'both'}, default 'left' - Side from which to fill resulting string + Side from which to fill resulting string. fillchar : str, default ' ' - Additional character for filling, default is whitespace + Additional character for filling, default is whitespace. Returns ------- From 50940c851524d17a80ff5396fd34638002849745 Mon Sep 17 00:00:00 2001 From: Jesper Dramsch Date: Mon, 3 Sep 2018 19:07:10 +0200 Subject: [PATCH 07/13] Examples fixed, See Also added This escalated --- pandas/core/strings.py | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 7943b9b635c4e..226b3714f95e6 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1333,22 +1333,46 @@ def str_pad(arr, width, side='left', fillchar=' '): Examples -------- - >>> s = pd.Series(["panda", "fox"]) + >>> s = pd.Series(["caribou", "tiger"]) >>> s - 0 panda - 1 fox + 0 caribou + 1 tiger + dtype: object >>> s.str.pad(width=10) - 0 panda - 1 fox + 0 caribou + 1 tiger + dtype: object >>> s.str.pad(width=10, side='right', fillchar='-') - 0 panda----- - 1 fox------- + 0 caribou--- + 1 tiger----- + dtype: object >>> s.str.pad(width=10, side='both', fillchar='-') - 0 --panda--- - 1 ---fox---- + 0 -caribou-- + 1 --tiger--- + dtype: object + + >>> s = pd.Series(["caribou", 0, np.NaN]) + >>> s.str.pad(width=10, fillchar='-') + 0 ---caribou + 1 NaN + 2 NaN + dtype: object + + See Also + -------- + Series.str.rjust: Fills the left side of strings with an arbitrary + character. + Series.str.ljust: Fills the right side of strings with an arbitrary + character. + Series.str.pad: Fills the specified sides of strings with an arbitrary + character. + Series.str.center: Fills boths sides of strings with an arbitrary + character. + Series.str.zfill: Pad strings in the Series/Index by prepending '0' + character. """ if not isinstance(fillchar, compat.string_types): From c473a4f7af99f6b14f70d101b721e059af4deb9f Mon Sep 17 00:00:00 2001 From: Jesper Dramsch Date: Mon, 3 Sep 2018 19:09:16 +0200 Subject: [PATCH 08/13] Whitespace be gone --- pandas/core/strings.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 226b3714f95e6..a822c1e9e1b99 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1353,7 +1353,7 @@ def str_pad(arr, width, side='left', fillchar=' '): 0 -caribou-- 1 --tiger--- dtype: object - + >>> s = pd.Series(["caribou", 0, np.NaN]) >>> s.str.pad(width=10, fillchar='-') 0 ---caribou @@ -1371,7 +1371,7 @@ def str_pad(arr, width, side='left', fillchar=' '): character. Series.str.center: Fills boths sides of strings with an arbitrary character. - Series.str.zfill: Pad strings in the Series/Index by prepending '0' + Series.str.zfill: Pad strings in the Series/Index by prepending '0' character. """ From 6f6853ecfd16ddd0c0d0277266d3ea7f3de31914 Mon Sep 17 00:00:00 2001 From: Jesper Dramsch Date: Mon, 3 Sep 2018 19:10:02 +0200 Subject: [PATCH 09/13] Period --- 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 a822c1e9e1b99..591eddc34a831 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1329,7 +1329,7 @@ def str_pad(arr, width, side='left', fillchar=' '): Returns ------- Series or Index of object - Returns Series or Index with minimum number of char in object + Returns Series or Index with minimum number of char in object. Examples -------- From 74566563f3f7638fda3410c72d4e76d2840ba88f Mon Sep 17 00:00:00 2001 From: Jesper Dramsch Date: Sat, 22 Sep 2018 01:56:33 +0200 Subject: [PATCH 10/13] Sorting and Validation --- pandas/core/strings.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 591eddc34a831..50e818db75ab4 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1331,6 +1331,17 @@ def str_pad(arr, width, side='left', fillchar=' '): Series or Index of object Returns Series or Index with minimum number of char in object. + See Also + -------- + Series.str.rjust: Fills the left side of strings with an arbitrary + character. + Series.str.ljust: Fills the right side of strings with an arbitrary + character. + Series.str.center: Fills boths sides of strings with an arbitrary + character. + Series.str.zfill: Pad strings in the Series/Index by prepending '0' + character. + Examples -------- >>> s = pd.Series(["caribou", "tiger"]) @@ -1360,21 +1371,7 @@ def str_pad(arr, width, side='left', fillchar=' '): 1 NaN 2 NaN dtype: object - - See Also - -------- - Series.str.rjust: Fills the left side of strings with an arbitrary - character. - Series.str.ljust: Fills the right side of strings with an arbitrary - character. - Series.str.pad: Fills the specified sides of strings with an arbitrary - character. - Series.str.center: Fills boths sides of strings with an arbitrary - character. - Series.str.zfill: Pad strings in the Series/Index by prepending '0' - character. """ - if not isinstance(fillchar, compat.string_types): msg = 'fillchar must be a character, not {0}' raise TypeError(msg.format(type(fillchar).__name__)) From f2d221d323f3c6bb39bfdcd98f375bc9619f68e7 Mon Sep 17 00:00:00 2001 From: Jesper Dramsch Date: Sat, 22 Sep 2018 01:59:47 +0200 Subject: [PATCH 11/13] Add See also info --- pandas/core/strings.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 50e818db75ab4..7ce40829c932b 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1334,13 +1334,13 @@ def str_pad(arr, width, side='left', fillchar=' '): See Also -------- Series.str.rjust: Fills the left side of strings with an arbitrary - character. + character. Equivalent to pd.str.pad(side='left'). Series.str.ljust: Fills the right side of strings with an arbitrary - character. + character. Equivalent to pd.str.pad(side='right'). Series.str.center: Fills boths sides of strings with an arbitrary - character. + character. Equivalent to pd.str.pad(side='both'). Series.str.zfill: Pad strings in the Series/Index by prepending '0' - character. + character. Equivalent to pd.str.pad(side='right', fillchar='0'). Examples -------- From 0640204fe9814993837eaffac19d749989d4373d Mon Sep 17 00:00:00 2001 From: Jesper Dramsch Date: Sat, 22 Sep 2018 02:00:39 +0200 Subject: [PATCH 12/13] Delete obsolete example --- pandas/core/strings.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 7ce40829c932b..524c8ff8b2152 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1364,13 +1364,6 @@ def str_pad(arr, width, side='left', fillchar=' '): 0 -caribou-- 1 --tiger--- dtype: object - - >>> s = pd.Series(["caribou", 0, np.NaN]) - >>> s.str.pad(width=10, fillchar='-') - 0 ---caribou - 1 NaN - 2 NaN - dtype: object """ if not isinstance(fillchar, compat.string_types): msg = 'fillchar must be a character, not {0}' From c78531116c15e35130178b87a19869f67d8c5e1d Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Tue, 25 Sep 2018 18:46:11 +0200 Subject: [PATCH 13/13] small fixup --- 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 524c8ff8b2152..c32431ac253b2 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1320,7 +1320,7 @@ def str_pad(arr, width, side='left', fillchar=' '): ---------- width : int Minimum width of resulting string; additional characters will be filled - with character defined in fillchar. + with character defined in `fillchar`. side : {'left', 'right', 'both'}, default 'left' Side from which to fill resulting string. fillchar : str, default ' ' @@ -1334,13 +1334,13 @@ def str_pad(arr, width, side='left', fillchar=' '): See Also -------- Series.str.rjust: Fills the left side of strings with an arbitrary - character. Equivalent to pd.str.pad(side='left'). + character. Equivalent to ``Series.str.pad(side='left')``. Series.str.ljust: Fills the right side of strings with an arbitrary - character. Equivalent to pd.str.pad(side='right'). + character. Equivalent to ``Series.str.pad(side='right')``. Series.str.center: Fills boths sides of strings with an arbitrary - character. Equivalent to pd.str.pad(side='both'). + character. Equivalent to ``Series.str.pad(side='both')``. Series.str.zfill: Pad strings in the Series/Index by prepending '0' - character. Equivalent to pd.str.pad(side='right', fillchar='0'). + character. Equivalent to ``Series.str.pad(side='left', fillchar='0')``. Examples --------