From ae39f29061a5174ce962a4af395db93d9fd4cc6a Mon Sep 17 00:00:00 2001 From: Will Ayd Date: Tue, 13 Mar 2018 00:35:47 -0700 Subject: [PATCH 1/2] Fixed bug with str.split, expand and no pater --- pandas/core/strings.py | 3 ++- pandas/tests/test_strings.py | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 11081535cf63f..924550b133c3c 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1615,7 +1615,8 @@ def cons_row(x): if result: # propagate nan values to match longest sequence (GH 18450) max_len = max(len(x) for x in result) - result = [x * max_len if x[0] is np.nan else x for x in result] + result = [x * max_len if len(x) and x[0] is np.nan else x for x + in result] if not isinstance(expand, bool): raise ValueError("expand must be True or False") diff --git a/pandas/tests/test_strings.py b/pandas/tests/test_strings.py index a878d6ed7b052..8ab1ee5574aab 100644 --- a/pandas/tests/test_strings.py +++ b/pandas/tests/test_strings.py @@ -2144,6 +2144,13 @@ def test_split_nan_expand(self): # tm.assert_frame_equal does not differentiate assert all(np.isnan(x) for x in result.iloc[1]) + def test_split_no_pat_and_expand(self): + # GH 20002 + df = DataFrame({'foo': ['']}) + exp = DataFrame([[]]) + res = df['foo'].str.split(expand=True) + tm.assert_frame_equal(res, exp) + def test_split_with_name(self): # GH 12617 From 980f13647ceb94b93f688a870789c2f0eb05b02a Mon Sep 17 00:00:00 2001 From: Will Ayd Date: Tue, 13 Mar 2018 00:36:12 -0700 Subject: [PATCH 2/2] Updated whatsnew --- doc/source/whatsnew/v0.23.0.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index f686a042c1a74..45dd61e4e7571 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -936,6 +936,7 @@ Indexing - Bug in :class:`IntervalIndex` where set operations that returned an empty ``IntervalIndex`` had the wrong dtype (:issue:`19101`) - Bug in :meth:`DataFrame.drop_duplicates` where no ``KeyError`` is raised when passing in columns that don't exist on the ``DataFrame`` (issue:`19726`) - Bug in ``Index`` subclasses constructors that ignore unexpected keyword arguments (:issue:`19348`) +- Bug in :meth:`Series.str.split` where splitting an empty string without a `pater` and with ``expand=True`` would raise an ``IndexError`` (:issue:`20002`) MultiIndex