Skip to content

Commit

Permalink
BUG: Let Series.str.split accept no arguments (like str.split) close #…
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Sep 8, 2012
1 parent 670921f commit ad33f4e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ pandas 0.8.2
- Fix issue calling sort on result of Series.unique (#1807)
- Fix numerical issue leading to square root of negative number in
rolling_std (#1840)
- Let Series.str.split accept no arguments (like str.split) (#1859)

pandas 0.8.1
============
Expand Down
16 changes: 10 additions & 6 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,23 +474,27 @@ def str_center(arr, width):
return str_pad(arr, width, side='both')


def str_split(arr, pat, n=0):
def str_split(arr, pat=None, n=0):
"""
Split each string (a la re.split) in array by given pattern, propagating NA
values
Parameters
----------
pat : string
String or regular expression to split on
pat : string, default None
String or regular expression to split on. If None, splits on whitespace
n : int, default 0 (all)
Returns
-------
split : array
"""
regex = re.compile(pat)
f = lambda x: regex.split(x, maxsplit=n)
if pat is None:
f = lambda x: x.split()
else:
regex = re.compile(pat)
f = lambda x: regex.split(x, maxsplit=n)

return _na_map(f, arr)


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

@copy(str_split)
def split(self, pat, n=0):
def split(self, pat=None, n=0):
result = str_split(self.series, pat, n=n)
return self._wrap_result(result)

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 @@ -463,6 +463,13 @@ def test_split(self):
[u'f', u'g', u'h']])
tm.assert_series_equal(result, exp)

def test_split_noargs(self):
# #1859
s = Series(['Wes McKinney', 'Travis Oliphant'])

result = s.str.split()
self.assertEquals(result[1], ['Travis', 'Oliphant'])

def test_slice(self):
values = Series(['aafootwo','aabartwo', NA, 'aabazqux'])

Expand Down

0 comments on commit ad33f4e

Please sign in to comment.