From dd24bc8e5d012df727b4fdbc5074a18aa2ac8e05 Mon Sep 17 00:00:00 2001 From: privat Date: Thu, 22 Mar 2018 20:17:50 +0000 Subject: [PATCH 1/5] added example to the str.join function --- pandas/core/strings.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index b98fa106336fc..176f767bb8b16 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -952,6 +952,42 @@ def str_join(arr, sep): Returns ------- joined : Series/Index of objects + + Notes + ----- + If any of the lists does not contain string objects the result of the join + will be `NaN`. + + Examples + -------- + + >>> df = pd.Series({1: ["foo", "bar", "foobar"], + 2: ['test', 'test2', 'test3'], + 3: ['ham', 'eggs', 'chicken']}) + 1 [foo, bar, foobar] + 2 [test, test2, test3] + 3 [ham, eggs, chicken] + dtype: object + + >>> df.str.join("_") + 1 foo_bar_foobar + 2 test_test2_test3 + 3 ham_eggs_chicken + dtype: object + + >>> df = pd.Series({1: [1.1, 2.2, 3.3], + 2: ['test', 'test2', 'test3'], + 3: ['ham', 'eggs', 'chicken']}) + 1 [1.1, 2.2, 3.3] + 2 [test, test2, test3] + 3 [ham, eggs, chicken] + dtype: object + + >>> df.str.join("-") + 1 NaN + 2 test-test2-test3 + 3 ham-eggs-chicken + dtype: object """ return _na_map(sep.join, arr) From 60549cdaa13de6cd82cb29af927d54d7739cb96b Mon Sep 17 00:00:00 2001 From: privat Date: Thu, 22 Mar 2018 21:05:28 +0000 Subject: [PATCH 2/5] fixed pep8 issues and reformated examples --- pandas/core/strings.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 176f767bb8b16..277bd1f5a642f 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -941,13 +941,14 @@ def str_get_dummies(arr, sep='|'): def str_join(arr, sep): """ - Join lists contained as elements in the Series/Index with - passed delimiter. Equivalent to :meth:`str.join`. + Join lists contained as elements in the Series/Index with passed delimiter. + + This function is an equivalent to :meth:`str.join`. Parameters ---------- sep : string - Delimiter + Delimiter to use between list entries. Returns ------- @@ -958,31 +959,43 @@ def str_join(arr, sep): If any of the lists does not contain string objects the result of the join will be `NaN`. + See Also + -------- + split: Split strings around given separator/delimiter. + Examples -------- >>> df = pd.Series({1: ["foo", "bar", "foobar"], - 2: ['test', 'test2', 'test3'], - 3: ['ham', 'eggs', 'chicken']}) + ... 2: ['test', 'test2', 'test3'], + ... 3: ['ham', 'eggs', 'chicken']}) + >>> df 1 [foo, bar, foobar] 2 [test, test2, test3] 3 [ham, eggs, chicken] dtype: object + Join all lists using "_". + >>> df.str.join("_") 1 foo_bar_foobar 2 test_test2_test3 3 ham_eggs_chicken dtype: object + Example with a list that contains non-string elements. + >>> df = pd.Series({1: [1.1, 2.2, 3.3], - 2: ['test', 'test2', 'test3'], - 3: ['ham', 'eggs', 'chicken']}) + ... 2: ['test', 'test2', 'test3'], + ... 3: ['ham', 'eggs', 'chicken']}) + >>> df 1 [1.1, 2.2, 3.3] 2 [test, test2, test3] 3 [ham, eggs, chicken] dtype: object + Join all lists using an "-", the list of floats will become a NaN. + >>> df.str.join("-") 1 NaN 2 test-test2-test3 From a169347030a779ba56a79813c413d0c6fd31b78a Mon Sep 17 00:00:00 2001 From: privat Date: Thu, 22 Mar 2018 21:54:28 +0000 Subject: [PATCH 3/5] changed to animal names --- pandas/core/strings.py | 51 ++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 277bd1f5a642f..258f0f4e6495e 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -943,16 +943,18 @@ def str_join(arr, sep): """ Join lists contained as elements in the Series/Index with passed delimiter. + If the elements of a Series are lists themselves, join the content of these + lists using the delimiter passed to the function. This function is an equivalent to :meth:`str.join`. Parameters ---------- - sep : string + sep : str Delimiter to use between list entries. Returns ------- - joined : Series/Index of objects + Series/Index of objects Notes ----- @@ -961,45 +963,46 @@ def str_join(arr, sep): See Also -------- - split: Split strings around given separator/delimiter. + str.join : Standard library version of this method. + Series.str.split : Split strings around given separator/delimiter. Examples -------- - >>> df = pd.Series({1: ["foo", "bar", "foobar"], - ... 2: ['test', 'test2', 'test3'], - ... 3: ['ham', 'eggs', 'chicken']}) + >>> df = pd.Series({1: ['dog', 'cat', 'fish'], + ... 2: ['lion', 'elephant', 'zebra'], + ... 3: ['cow', 'pig', 'chicken']}) >>> df - 1 [foo, bar, foobar] - 2 [test, test2, test3] - 3 [ham, eggs, chicken] + 1 [dog, cat, fish] + 2 [lion, elephant, zebra] + 3 [cow, pig, chicken] dtype: object - Join all lists using "_". + Join all lists using '_'. - >>> df.str.join("_") - 1 foo_bar_foobar - 2 test_test2_test3 - 3 ham_eggs_chicken + >>> df.str.join('_') + 1 dog_cat_fish + 2 lion_elephant_zebra + 3 cow_pig_chicken dtype: object Example with a list that contains non-string elements. >>> df = pd.Series({1: [1.1, 2.2, 3.3], - ... 2: ['test', 'test2', 'test3'], - ... 3: ['ham', 'eggs', 'chicken']}) + ... 2: ['lion', 'elephant', 'zebra'], + ... 3: ['cow', 'pig', 'chicken']}) >>> df - 1 [1.1, 2.2, 3.3] - 2 [test, test2, test3] - 3 [ham, eggs, chicken] + 1 [1.1, 2.2, 3.3] + 2 [lion, elephant, zebra] + 3 [cow, pig, chicken] dtype: object - Join all lists using an "-", the list of floats will become a NaN. + Join all lists using an '-', the list of floats will become a NaN. - >>> df.str.join("-") - 1 NaN - 2 test-test2-test3 - 3 ham-eggs-chicken + >>> df.str.join('-') + 1 NaN + 2 lion-elephant-zebra + 3 cow-pig-chicken dtype: object """ return _na_map(sep.join, arr) From 333193eb13057f0008d0203e189f659e54c8cada Mon Sep 17 00:00:00 2001 From: privat Date: Fri, 23 Mar 2018 11:54:28 +0000 Subject: [PATCH 4/5] applied comments on PR, df -> s and compact example --- pandas/core/strings.py | 39 +++++++++++---------------------------- 1 file changed, 11 insertions(+), 28 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 258f0f4e6495e..80b99e79be381 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -969,40 +969,23 @@ def str_join(arr, sep): Examples -------- - >>> df = pd.Series({1: ['dog', 'cat', 'fish'], - ... 2: ['lion', 'elephant', 'zebra'], - ... 3: ['cow', 'pig', 'chicken']}) - >>> df - 1 [dog, cat, fish] - 2 [lion, elephant, zebra] - 3 [cow, pig, chicken] - dtype: object - - Join all lists using '_'. - - >>> df.str.join('_') - 1 dog_cat_fish - 2 lion_elephant_zebra - 3 cow_pig_chicken - dtype: object - Example with a list that contains non-string elements. - >>> df = pd.Series({1: [1.1, 2.2, 3.3], - ... 2: ['lion', 'elephant', 'zebra'], - ... 3: ['cow', 'pig', 'chicken']}) - >>> df - 1 [1.1, 2.2, 3.3] - 2 [lion, elephant, zebra] - 3 [cow, pig, chicken] + >>> s = pd.Series({1: ['lion', 'elephant', 'zebra'], + ... 2: [1.1, 2.2, 3.3], + ... 3: [np.nan, np.nan, np.nan]}) + >>> s + 1 [lion, elephant, zebra] + 2 [1.1, 2.2, 3.3] + 3 [nan, nan, nan] dtype: object Join all lists using an '-', the list of floats will become a NaN. - >>> df.str.join('-') - 1 NaN - 2 lion-elephant-zebra - 3 cow-pig-chicken + >>> s.str.join('-') + 1 lion-elephant-zebra + 2 NaN + 3 NaN dtype: object """ return _na_map(sep.join, arr) From 323d0722546cd08706fd62b47d11c86b802ef1f1 Mon Sep 17 00:00:00 2001 From: fdroessler Date: Fri, 23 Mar 2018 22:50:10 +0000 Subject: [PATCH 5/5] added mixed type examples --- pandas/core/strings.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 80b99e79be381..6796c14a18eaa 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -954,7 +954,7 @@ def str_join(arr, sep): Returns ------- - Series/Index of objects + Series/Index: object Notes ----- @@ -971,21 +971,28 @@ def str_join(arr, sep): Example with a list that contains non-string elements. - >>> s = pd.Series({1: ['lion', 'elephant', 'zebra'], - ... 2: [1.1, 2.2, 3.3], - ... 3: [np.nan, np.nan, np.nan]}) + >>> s = pd.Series([['lion', 'elephant', 'zebra'], + ... [1.1, 2.2, 3.3], + ... ['cat', np.nan, 'dog'], + ... ['cow', 4.5, 'goat'] + ... ['duck', ['swan', 'fish'], 'guppy']]) >>> s - 1 [lion, elephant, zebra] - 2 [1.1, 2.2, 3.3] - 3 [nan, nan, nan] + 0 [lion, elephant, zebra] + 1 [1.1, 2.2, 3.3] + 2 [cat, nan, dog] + 3 [cow, 4.5, goat] + 4 [duck, [swan, fish], guppy] dtype: object - Join all lists using an '-', the list of floats will become a NaN. + Join all lists using an '-', the lists containing object(s) of types other + than str will become a NaN. >>> s.str.join('-') - 1 lion-elephant-zebra + 0 lion-elephant-zebra + 1 NaN 2 NaN 3 NaN + 4 NaN dtype: object """ return _na_map(sep.join, arr)