Skip to content

[ArrowStringArray] use pyarrow string trimming functions if available #41219

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

Merged
merged 1 commit into from
Apr 29, 2021
Merged
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
27 changes: 27 additions & 0 deletions pandas/core/arrays/string_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,3 +831,30 @@ def _str_lower(self):

def _str_upper(self):
return type(self)(pc.utf8_upper(self._data))

def _str_strip(self, to_strip=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ought to create an issue to remove the hasattr when we bump the required min pyarrow version

if to_strip is None:
if hasattr(pc, "utf8_trim_whitespace"):
return type(self)(pc.utf8_trim_whitespace(self._data))
else:
if hasattr(pc, "utf8_trim"):
return type(self)(pc.utf8_trim(self._data, characters=to_strip))
return super()._str_strip(to_strip)

def _str_lstrip(self, to_strip=None):
if to_strip is None:
if hasattr(pc, "utf8_ltrim_whitespace"):
return type(self)(pc.utf8_ltrim_whitespace(self._data))
else:
if hasattr(pc, "utf8_ltrim"):
return type(self)(pc.utf8_ltrim(self._data, characters=to_strip))
return super()._str_lstrip(to_strip)

def _str_rstrip(self, to_strip=None):
if to_strip is None:
if hasattr(pc, "utf8_rtrim_whitespace"):
return type(self)(pc.utf8_rtrim_whitespace(self._data))
else:
if hasattr(pc, "utf8_rtrim"):
return type(self)(pc.utf8_rtrim(self._data, characters=to_strip))
return super()._str_rstrip(to_strip)
20 changes: 10 additions & 10 deletions pandas/tests/strings/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,19 +570,19 @@ def test_slice_replace():
tm.assert_series_equal(result, exp)


def test_strip_lstrip_rstrip():
values = Series([" aa ", " bb \n", np.nan, "cc "])
def test_strip_lstrip_rstrip(any_string_dtype):
values = Series([" aa ", " bb \n", np.nan, "cc "], dtype=any_string_dtype)

result = values.str.strip()
exp = Series(["aa", "bb", np.nan, "cc"])
exp = Series(["aa", "bb", np.nan, "cc"], dtype=any_string_dtype)
tm.assert_series_equal(result, exp)

result = values.str.lstrip()
exp = Series(["aa ", "bb \n", np.nan, "cc "])
exp = Series(["aa ", "bb \n", np.nan, "cc "], dtype=any_string_dtype)
tm.assert_series_equal(result, exp)

result = values.str.rstrip()
exp = Series([" aa", " bb", np.nan, "cc"])
exp = Series([" aa", " bb", np.nan, "cc"], dtype=any_string_dtype)
tm.assert_series_equal(result, exp)


Expand All @@ -609,19 +609,19 @@ def test_strip_lstrip_rstrip_mixed():
tm.assert_almost_equal(rs, xp)


def test_strip_lstrip_rstrip_args():
values = Series(["xxABCxx", "xx BNSD", "LDFJH xx"])
def test_strip_lstrip_rstrip_args(any_string_dtype):
values = Series(["xxABCxx", "xx BNSD", "LDFJH xx"], dtype=any_string_dtype)

rs = values.str.strip("x")
xp = Series(["ABC", " BNSD", "LDFJH "])
xp = Series(["ABC", " BNSD", "LDFJH "], dtype=any_string_dtype)
tm.assert_series_equal(rs, xp)

rs = values.str.lstrip("x")
xp = Series(["ABCxx", " BNSD", "LDFJH xx"])
xp = Series(["ABCxx", " BNSD", "LDFJH xx"], dtype=any_string_dtype)
tm.assert_series_equal(rs, xp)

rs = values.str.rstrip("x")
xp = Series(["xxABC", "xx BNSD", "LDFJH "])
xp = Series(["xxABC", "xx BNSD", "LDFJH "], dtype=any_string_dtype)
tm.assert_series_equal(rs, xp)


Expand Down