Skip to content

Commit ad33f4e

Browse files
committed
BUG: Let Series.str.split accept no arguments (like str.split) close #1859
1 parent 670921f commit ad33f4e

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

RELEASE.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ pandas 0.8.2
100100
- Fix issue calling sort on result of Series.unique (#1807)
101101
- Fix numerical issue leading to square root of negative number in
102102
rolling_std (#1840)
103+
- Let Series.str.split accept no arguments (like str.split) (#1859)
103104

104105
pandas 0.8.1
105106
============

pandas/core/strings.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -474,23 +474,27 @@ def str_center(arr, width):
474474
return str_pad(arr, width, side='both')
475475

476476

477-
def str_split(arr, pat, n=0):
477+
def str_split(arr, pat=None, n=0):
478478
"""
479479
Split each string (a la re.split) in array by given pattern, propagating NA
480480
values
481481
482482
Parameters
483483
----------
484-
pat : string
485-
String or regular expression to split on
484+
pat : string, default None
485+
String or regular expression to split on. If None, splits on whitespace
486486
n : int, default 0 (all)
487487
488488
Returns
489489
-------
490490
split : array
491491
"""
492-
regex = re.compile(pat)
493-
f = lambda x: regex.split(x, maxsplit=n)
492+
if pat is None:
493+
f = lambda x: x.split()
494+
else:
495+
regex = re.compile(pat)
496+
f = lambda x: regex.split(x, maxsplit=n)
497+
494498
return _na_map(f, arr)
495499

496500

@@ -690,7 +694,7 @@ def cat(self, others=None, sep=None, na_rep=None):
690694
return self._wrap_result(result)
691695

692696
@copy(str_split)
693-
def split(self, pat, n=0):
697+
def split(self, pat=None, n=0):
694698
result = str_split(self.series, pat, n=n)
695699
return self._wrap_result(result)
696700

pandas/tests/test_strings.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,13 @@ def test_split(self):
463463
[u'f', u'g', u'h']])
464464
tm.assert_series_equal(result, exp)
465465

466+
def test_split_noargs(self):
467+
# #1859
468+
s = Series(['Wes McKinney', 'Travis Oliphant'])
469+
470+
result = s.str.split()
471+
self.assertEquals(result[1], ['Travis', 'Oliphant'])
472+
466473
def test_slice(self):
467474
values = Series(['aafootwo','aabartwo', NA, 'aabazqux'])
468475

0 commit comments

Comments
 (0)