Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Propogating NaN values when using str.split (#18450)
  • Loading branch information
WillAyd committed Nov 25, 2017
commit a08e9403b796743ecd00980e57f85cfbb051166f
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.21.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,6 @@ Categorical
Other
^^^^^

-
- :meth:`Series.str.split()` will now propogate ``NaN`` values across all expanded columns instead of ``None`` (:issue:`18450`)
-
-
4 changes: 4 additions & 0 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,10 @@ def cons_row(x):
return [x]

result = [cons_row(x) for x in result]
if result:
# propogate 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]

if not isinstance(expand, bool):
raise ValueError("expand must be True or False")
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2086,6 +2086,15 @@ def test_rsplit_to_multiindex_expand(self):
tm.assert_index_equal(result, exp)
assert result.nlevels == 2

def test_split_nan_expand(self):
s = Series(["foo,bar,baz", NA])
result = s.str.split(",", expand=True)
exp = DataFrame([["foo", "bar", "baz"], [NA, NA, NA]])
tm.assert_frame_equal(result, exp)

# extra nan check - see GH 18463
assert all(np.isnan(x) for x in result.iloc[1])

def test_split_with_name(self):
# GH 12617

Expand Down