Skip to content

Commit

Permalink
python str.{strip, lstrip, rstrip}
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Jan 10, 2022
1 parent 599c589 commit 75bf676
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 11 deletions.
18 changes: 18 additions & 0 deletions py-polars/polars/internals/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2478,6 +2478,24 @@ def to_lowercase(self) -> Expr:
"""
return wrap_expr(self._pyexpr.str_to_lowercase())

def strip(self) -> Expr:
"""
Remove leading and trailing whitespace.
"""
return wrap_expr(self._pyexpr.str_strip())

def lstrip(self) -> Expr:
"""
Remove leading whitespace.
"""
return wrap_expr(self._pyexpr.str_lstrip())

def rstrip(self) -> Expr:
"""
Remove leading whitespace.
"""
return wrap_expr(self._pyexpr.str_rstrip())

def contains(self, pattern: str) -> Expr:
"""
Check if string contains regex.
Expand Down
31 changes: 20 additions & 11 deletions py-polars/polars/internals/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3633,29 +3633,38 @@ def replace_all(self, pattern: str, value: str) -> Series:
"""
return wrap_s(self._s.str_replace_all(pattern, value))

def to_lowercase(self) -> Series:
def strip(self) -> Series:
"""
Modify the strings to their lowercase equivalent.
Remove leading and trailing whitespace.
"""
return wrap_s(self._s.str_to_lowercase())
s = wrap_s(self._s)
return s.to_frame().select(pli.col(s.name).str.strip()).to_series()

def to_uppercase(self) -> Series:
def lstrip(self) -> Series:
"""
Modify the strings to their uppercase equivalent.
Remove leading whitespace.
"""
return wrap_s(self._s.str_to_uppercase())
s = wrap_s(self._s)
return s.to_frame().select(pli.col(s.name).str.lstrip()).to_series()

def rstrip(self) -> Series:
"""
Remove trailing whitespace.
Remove leading whitespace.
"""
return self.replace(r"[ \t]+$", "")
s = wrap_s(self._s)
return s.to_frame().select(pli.col(s.name).str.rstrip()).to_series()

def lstrip(self) -> Series:
def to_lowercase(self) -> Series:
"""
Remove leading whitespace.
Modify the strings to their lowercase equivalent.
"""
return self.replace(r"^\s*", "")
return wrap_s(self._s.str_to_lowercase())

def to_uppercase(self) -> Series:
"""
Modify the strings to their uppercase equivalent.
"""
return wrap_s(self._s.str_to_uppercase())

def slice(self, start: int, length: Optional[int] = None) -> Series:
"""
Expand Down
37 changes: 37 additions & 0 deletions py-polars/src/lazy/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use polars_core::prelude::QuantileInterpolOptions;
use pyo3::prelude::*;
use pyo3::types::{PyBool, PyFloat, PyInt, PyString};
use pyo3::{class::basic::CompareOp, PyNumberProtocol, PyObjectProtocol};
use std::borrow::Cow;

#[pyclass]
#[repr(transparent)]
Expand Down Expand Up @@ -361,6 +362,42 @@ impl PyExpr {
.into()
}

pub fn str_strip(&self) -> PyExpr {
let function = |s: Series| {
let ca = s.utf8()?;

Ok(ca.apply(|s| Cow::Borrowed(s.trim())).into_series())
};
self.clone()
.inner
.map(function, GetOutput::same_type())
.into()
}

pub fn str_rstrip(&self) -> PyExpr {
let function = |s: Series| {
let ca = s.utf8()?;

Ok(ca.apply(|s| Cow::Borrowed(s.trim_end())).into_series())
};
self.clone()
.inner
.map(function, GetOutput::same_type())
.into()
}

pub fn str_lstrip(&self) -> PyExpr {
let function = |s: Series| {
let ca = s.utf8()?;

Ok(ca.apply(|s| Cow::Borrowed(s.trim_start())).into_series())
};
self.clone()
.inner
.map(function, GetOutput::same_type())
.into()
}

pub fn str_to_uppercase(&self) -> PyExpr {
let function = |s: Series| {
let ca = s.utf8()?;
Expand Down
10 changes: 10 additions & 0 deletions py-polars/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1416,3 +1416,13 @@ def test_product() -> None:
a = pl.Series("a", [None, 2, 3])
out = a.product()
assert out is None


def test_strip() -> None:
a = pl.Series("a", ["trailing ", " leading", " both "])
expected = pl.Series("a", ["trailing", " leading", " both"])
verify_series_and_expr_api(a, expected, "str.rstrip")
expected = pl.Series("a", ["trailing ", "leading", "both "])
verify_series_and_expr_api(a, expected, "str.lstrip")
expected = pl.Series("a", ["trailing", "leading", "both"])
verify_series_and_expr_api(a, expected, "str.strip")

0 comments on commit 75bf676

Please sign in to comment.