Skip to content

Commit

Permalink
ENH: args for strip/lstrip/rstrip #2411
Browse files Browse the repository at this point in the history
  • Loading branch information
changhiskhan authored and wesm committed Dec 7, 2012
1 parent 2d9e25a commit 0a3fd63
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -25,4 +25,5 @@ pandas.egg-info
*\#*\#
.tox
pandas/io/*.dat
pandas/io/*.json
pandas/io/*.json
*.log
1 change: 1 addition & 0 deletions RELEASE.rst
Expand Up @@ -99,6 +99,7 @@ pandas 0.10.0
- Implement ``value_vars`` in ``melt`` and add ``melt`` to pandas namespace
(#2412)
- Added boolean comparison operators to Panel
- Enable ``Series.str.strip/lstrip/rstrip`` methods to take an argument (#2411)

**Bug fixes**

Expand Down
3 changes: 3 additions & 0 deletions doc/source/v0.10.0.txt
Expand Up @@ -130,6 +130,8 @@ Adding experimental support for Panel4D and factory functions to create n-dimens



- Enable ``Series.str.strip/lstrip/rstrip`` methods to take an argument (GH2411_)

API changes
~~~~~~~~~~~

Expand Down Expand Up @@ -177,3 +179,4 @@ on GitHub for a complete list.
.. _GH2224: https://github.com/pydata/pandas/issues/2224
.. _GH2431: https://github.com/pydata/pandas/issues/2431
.. _GH2412: https://github.com/pydata/pandas/issues/2412
.. _GH2411: https://github.com/pydata/pandas/issues/2411
42 changes: 33 additions & 9 deletions pandas/core/strings.py
Expand Up @@ -450,39 +450,51 @@ def str_slice_replace(arr, start=None, stop=None, repl=None):
raise NotImplementedError


def str_strip(arr):
def str_strip(arr, to_strip=None):
"""
Strip whitespace (including newlines) from each string in the array
Parameters
----------
to_strip : str or unicode
Returns
-------
stripped : array
"""
return _na_map(lambda x: x.strip(), arr)
return _na_map(lambda x: x.strip(to_strip), arr)


def str_lstrip(arr):
def str_lstrip(arr, to_strip=None):
"""
Strip whitespace (including newlines) from left side of each string in the
array
Parameters
----------
to_strip : str or unicode
Returns
-------
stripped : array
"""
return _na_map(lambda x: x.lstrip(), arr)
return _na_map(lambda x: x.lstrip(to_strip), arr)


def str_rstrip(arr):
def str_rstrip(arr, to_strip=None):
"""
Strip whitespace (including newlines) from right side of each string in the
array
Parameters
----------
to_strip : str or unicode
Returns
-------
stripped : array
"""
return _na_map(lambda x: x.rstrip(), arr)
return _na_map(lambda x: x.rstrip(to_strip), arr)


def str_wrap(arr, width=80):
Expand Down Expand Up @@ -685,15 +697,27 @@ def encode(self, encoding, errors="strict"):
result = str_encode(self.series, encoding, errors)
return self._wrap_result(result)

@copy(str_strip)
def strip(self, to_strip=None):
result = str_strip(self.series, to_strip)
return self._wrap_result(result)

@copy(str_lstrip)
def lstrip(self, to_strip=None):
result = str_lstrip(self.series, to_strip)
return self._wrap_result(result)

@copy(str_rstrip)
def rstrip(self, to_strip=None):
result = str_rstrip(self.series, to_strip)
return self._wrap_result(result)

count = _pat_wrapper(str_count, flags=True)
startswith = _pat_wrapper(str_startswith, na=True)
endswith = _pat_wrapper(str_endswith, na=True)
findall = _pat_wrapper(str_findall, flags=True)
match = _pat_wrapper(str_match, flags=True)

len = _noarg_wrapper(str_len)
strip = _noarg_wrapper(str_strip)
rstrip = _noarg_wrapper(str_rstrip)
lstrip = _noarg_wrapper(str_lstrip)
lower = _noarg_wrapper(str_lower)
upper = _noarg_wrapper(str_upper)
32 changes: 32 additions & 0 deletions pandas/tests/test_strings.py
Expand Up @@ -539,6 +539,7 @@ def test_strip_lstrip_rstrip(self):
exp = Series([' aa', ' bb', NA, 'cc'])
tm.assert_series_equal(result, exp)

def test_strip_lstrip_rstrip_mixed(self):
#mixed
mixed = Series([' aa ', NA, ' bb \t\n', True, datetime.today(),
None, 1, 2.])
Expand All @@ -564,6 +565,7 @@ def test_strip_lstrip_rstrip(self):
self.assert_(isinstance(rs, Series))
tm.assert_almost_equal(rs, xp)

def test_strip_lstrip_rstrip_unicode(self):
#unicode
values = Series([u' aa ', u' bb \n', NA, u'cc '])

Expand All @@ -579,6 +581,36 @@ def test_strip_lstrip_rstrip(self):
exp = Series([u' aa', u' bb', NA, u'cc'])
tm.assert_series_equal(result, exp)

def test_strip_lstrip_rstrip_args(self):
values = Series(['xxABCxx', 'xx BNSD', 'LDFJH xx'])

rs = values.str.strip('x')
xp = Series(['ABC', ' BNSD', 'LDFJH '])
assert_series_equal(rs, xp)

rs = values.str.lstrip('x')
xp = Series(['ABCxx', ' BNSD', 'LDFJH xx'])
assert_series_equal(rs, xp)

rs = values.str.rstrip('x')
xp = Series(['xxABC', 'xx BNSD', 'LDFJH '])
assert_series_equal(rs, xp)

def test_strip_lstrip_rstrip_args_unicode(self):
values = Series([u'xxABCxx', u'xx BNSD', u'LDFJH xx'])

rs = values.str.strip(u'x')
xp = Series(['ABC', ' BNSD', 'LDFJH '])
assert_series_equal(rs, xp)

rs = values.str.lstrip(u'x')
xp = Series(['ABCxx', ' BNSD', 'LDFJH xx'])
assert_series_equal(rs, xp)

rs = values.str.rstrip(u'x')
xp = Series(['xxABC', 'xx BNSD', 'LDFJH '])
assert_series_equal(rs, xp)

def test_wrap(self):
pass

Expand Down

0 comments on commit 0a3fd63

Please sign in to comment.