From 4103c0543558cb3ea6250a58b60e52cb7928a544 Mon Sep 17 00:00:00 2001 From: "H. Vetinari" Date: Tue, 16 Oct 2018 19:20:07 +0200 Subject: [PATCH 1/3] BUG: sets in str.cat --- doc/source/whatsnew/v0.24.0.txt | 2 +- pandas/core/strings.py | 4 ++-- pandas/tests/test_strings.py | 10 +++++++++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index d0aa156cf5059..04ce55fed8d52 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -976,7 +976,7 @@ Numeric Strings ^^^^^^^ -- +- Bug in :meth:`Series.str.cat` which falsely did not raise if `others` was a `set` (:issue:`23009`) - - diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 4086021bc61a6..48e1c539ae098 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1996,12 +1996,12 @@ def _get_series_list(self, others, ignore_index=False): elif isinstance(others, np.ndarray) and others.ndim == 2: others = DataFrame(others, index=idx) return ([others[x] for x in others], False) - elif is_list_like(others): + elif is_list_like(others) and not isinstance(others, set): others = list(others) # ensure iterators do not get read twice etc # in case of list-like `others`, all elements must be # either one-dimensional list-likes or scalars - if all(is_list_like(x) for x in others): + if all(is_list_like(x) and not isinstance(x, set) for x in others): los = [] join_warn = False depr_warn = False diff --git a/pandas/tests/test_strings.py b/pandas/tests/test_strings.py index 75b1bcb8b2938..59bd62271e988 100644 --- a/pandas/tests/test_strings.py +++ b/pandas/tests/test_strings.py @@ -301,7 +301,15 @@ def test_str_cat_mixed_inputs(self, box): with tm.assert_raises_regex(TypeError, rgx): s.str.cat([u, [u, d]]) - # forbidden input type, e.g. int + # forbidden input type: set + with tm.assert_raises_regex(TypeError, rgx): + s.str.cat(set(u)) + + # forbidden input type: set in list + with tm.assert_raises_regex(TypeError, rgx): + s.str.cat([u, set(u)]) + + # other forbidden input type, e.g. int with tm.assert_raises_regex(TypeError, rgx): s.str.cat(1) From b091d1258db1dddbe278d5faf5e099db8a116366 Mon Sep 17 00:00:00 2001 From: "H. Vetinari" Date: Thu, 18 Oct 2018 18:19:57 +0200 Subject: [PATCH 2/3] Incorporate allow_sets-kwarg for is_list_like --- 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 48e1c539ae098..c824ad1712a5a 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1996,12 +1996,12 @@ def _get_series_list(self, others, ignore_index=False): elif isinstance(others, np.ndarray) and others.ndim == 2: others = DataFrame(others, index=idx) return ([others[x] for x in others], False) - elif is_list_like(others) and not isinstance(others, set): + elif is_list_like(others, allow_sets=False): others = list(others) # ensure iterators do not get read twice etc # in case of list-like `others`, all elements must be # either one-dimensional list-likes or scalars - if all(is_list_like(x) and not isinstance(x, set) for x in others): + if all(is_list_like(x, allow_sets=False) for x in others): los = [] join_warn = False depr_warn = False From deb912eb8b60aeec8bee1ed7d30504de492eb8e9 Mon Sep 17 00:00:00 2001 From: "H. Vetinari" Date: Mon, 22 Oct 2018 16:07:34 +0200 Subject: [PATCH 3/3] Review (jreback) --- doc/source/whatsnew/v0.24.0.txt | 3 ++- pandas/tests/test_strings.py | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 04ce55fed8d52..3f3a01cf6e850 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -210,6 +210,7 @@ Backwards incompatible API changes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - A newly constructed empty :class:`DataFrame` with integer as the ``dtype`` will now only be cast to ``float64`` if ``index`` is specified (:issue:`22858`) +- :meth:`Series.str.cat` will now raise if `others` is a `set` (:issue:`23009`) .. _whatsnew_0240.api_breaking.deps: @@ -976,7 +977,7 @@ Numeric Strings ^^^^^^^ -- Bug in :meth:`Series.str.cat` which falsely did not raise if `others` was a `set` (:issue:`23009`) +- - - diff --git a/pandas/tests/test_strings.py b/pandas/tests/test_strings.py index 59bd62271e988..87bf89229a64e 100644 --- a/pandas/tests/test_strings.py +++ b/pandas/tests/test_strings.py @@ -302,10 +302,12 @@ def test_str_cat_mixed_inputs(self, box): s.str.cat([u, [u, d]]) # forbidden input type: set + # GH 23009 with tm.assert_raises_regex(TypeError, rgx): s.str.cat(set(u)) # forbidden input type: set in list + # GH 23009 with tm.assert_raises_regex(TypeError, rgx): s.str.cat([u, set(u)])