Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jun 14, 2024
1 parent 16cab2a commit 6ef72e6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion narwhals/_pandas_like/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ def ends_with(self, suffix: str) -> PandasExpr:
"ends_with",
suffix,
)

def contains(self, pat: str, regex: bool = True) -> PandasExpr:
return reuse_series_namespace_implementation(
self._expr,
Expand Down
6 changes: 2 additions & 4 deletions narwhals/_pandas_like/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,11 +503,9 @@ def ends_with(self, suffix: str) -> PandasSeries:
return self._series._from_series(
self._series._series.str.endswith(suffix),
)

def contains(self, pat: str, regex: bool = True) -> PandasSeries:
return self._series._from_series(
self._series._series.str.contains(pat, regex)
)
return self._series._from_series(self._series._series.str.contains(pat, regex))

def head(self, n: int = 5) -> PandasSeries:
return self._series._from_series(
Expand Down
13 changes: 8 additions & 5 deletions narwhals/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -1461,7 +1461,7 @@ def ends_with(self, suffix: str) -> Expr:
return self._expr.__class__(
lambda plx: self._expr._call(plx).str.ends_with(suffix)
)

def contains(self, pattern: str, literal: bool = False) -> Expr:
"""
Check if string contains a substring that matches a pattern.
Expand All @@ -1485,9 +1485,11 @@ def contains(self, pattern: str, literal: bool = False) -> Expr:
>>> @nw.narwhalify
... def func(df):
... return df.with_columns(
... default_match = nw.col("pets").str.contains("parrot|Dove"),
... case_insensitive_match = nw.col("pets").str.contains("(?i)parrot|Dove"),
... literal_match = nw.col("pets").str.contains("parrot|Dove", literal=True)
... default_match=nw.col("pets").str.contains("parrot|Dove"),
... case_insensitive_match=nw.col("pets").str.contains("(?i)parrot|Dove"),
... literal_match=nw.col("pets").str.contains(
... "parrot|Dove", literal=True
... ),
... )
We can then pass either pandas or Polars to `func`:
Expand Down Expand Up @@ -1522,7 +1524,8 @@ def func(plx: Any) -> Any:
if literal == True:
return self._expr._call(plx).str.contains(pat=pattern, regex=False)
else:
return self._expr._call(plx).str.contains(pat=pattern)
return self._expr._call(plx).str.contains(pat=pattern)

return self._expr.__class__(func)

def head(self, n: int = 5) -> Expr:
Expand Down
16 changes: 11 additions & 5 deletions narwhals/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1611,16 +1611,22 @@ def __init__(self, series: Series) -> None:

def ends_with(self, suffix: str) -> Series:
return self._series.__class__(self._series._series.str.ends_with(suffix))

def contains(self, pattern: str, literal: bool = False) -> Series:
if self._series._is_polars:
return self._series.__class__(self._series._series.str.contains(pattern, literal))

return self._series.__class__(
self._series._series.str.contains(pattern, literal)
)

if self._series._is_polars == False:
if literal == True:
return self._series.__class__(self._series._series.str.contains(pat=pattern, regex=False))
return self._series.__class__(
self._series._series.str.contains(pat=pattern, regex=False)
)
else:
return self._series.__class__(self._series._series.str.contains(pat=pattern))
return self._series.__class__(
self._series._series.str.contains(pat=pattern)
)

def head(self, n: int = 5) -> Series:
"""
Expand Down

0 comments on commit 6ef72e6

Please sign in to comment.