From 75da7b9e74c2cc4c87e10416e91886bb634087fe Mon Sep 17 00:00:00 2001 From: Pranav Date: Thu, 12 Apr 2018 05:00:50 +0530 Subject: [PATCH 1/3] DOC: Updated the docstring of pandas.Series.str.get --- pandas/core/strings.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 23c891ec4fcd0..82c8ab87b253b 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1651,17 +1651,36 @@ def str_translate(arr, table, deletechars=None): def str_get(arr, i): """ + Extract element from each component at specified position. + Extract element from lists, tuples, or strings in each element in the Series/Index. Parameters ---------- i : int - Integer index (location) + Position of element to extract. Returns ------- items : Series/Index of objects + + Examples + -------- + >>> s = pd.Series(["String", (1, 2, 3), ["a", "b", "c"], 123]) + >>> s + 0 String + 1 (1, 2, 3) + 2 [a, b, c] + 3 123 + dtype: object + + >>> s.str.get(1) + 0 t + 1 2 + 2 b + 3 NaN + dtype: object """ f = lambda x: x[i] if len(x) > i >= -len(x) else np.nan return _na_map(f, arr) From 9ce7f42db961c7afafe1e006ac152c8890dadbaf Mon Sep 17 00:00:00 2001 From: Pranav Suri Date: Tue, 24 Apr 2018 11:45:52 +0530 Subject: [PATCH 2/3] Updated docstring of pandas.Series.str.get Added an example with a dictionary, a negative integer and an example with a negative index, i.e., not present in the dictionary. --- pandas/core/strings.py | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 82c8ab87b253b..09890535b44ae 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1667,19 +1667,36 @@ def str_get(arr, i): Examples -------- - >>> s = pd.Series(["String", (1, 2, 3), ["a", "b", "c"], 123]) + >>> s = pd.Series(["String", + (1, 2, 3), + ["a", "b", "c"], + 123, -456, + {1:"Hello", "2":"World"}]) >>> s - 0 String - 1 (1, 2, 3) - 2 [a, b, c] - 3 123 + 0 String + 1 (1, 2, 3) + 2 [a, b, c] + 3 123 + 4 -456 + 5 {1: 'Hello', '2': 'World'} dtype: object - + >>> s.str.get(1) - 0 t - 1 2 - 2 b + 0 t + 1 2 + 2 b + 3 NaN + 4 NaN + 5 Hello + dtype: object + + >>> s.str.get(-1) + 0 g + 1 3 + 2 c 3 NaN + 4 NaN + 5 NaN dtype: object """ f = lambda x: x[i] if len(x) > i >= -len(x) else np.nan From 26fdb8452e704777a60da8eb2eea09826310b459 Mon Sep 17 00:00:00 2001 From: Pranav Suri Date: Tue, 24 Apr 2018 15:26:46 +0530 Subject: [PATCH 3/3] Fixed PEP8 issues (trailing whitespaces) --- pandas/core/strings.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 09890535b44ae..bbb7ec7e77a59 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1667,10 +1667,10 @@ def str_get(arr, i): Examples -------- - >>> s = pd.Series(["String", - (1, 2, 3), - ["a", "b", "c"], - 123, -456, + >>> s = pd.Series(["String", + (1, 2, 3), + ["a", "b", "c"], + 123, -456, {1:"Hello", "2":"World"}]) >>> s 0 String @@ -1680,7 +1680,7 @@ def str_get(arr, i): 4 -456 5 {1: 'Hello', '2': 'World'} dtype: object - + >>> s.str.get(1) 0 t 1 2 @@ -1689,7 +1689,7 @@ def str_get(arr, i): 4 NaN 5 Hello dtype: object - + >>> s.str.get(-1) 0 g 1 3