Skip to content

Fixed str.split IndexError with Empty String and expand=True #20320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down