From 6ef72e621497a0ee82e7e7fab5061addce9f151e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Jun 2024 13:03:29 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- narwhals/_pandas_like/expr.py | 2 +- narwhals/_pandas_like/series.py | 6 ++---- narwhals/expression.py | 13 ++++++++----- narwhals/series.py | 16 +++++++++++----- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/narwhals/_pandas_like/expr.py b/narwhals/_pandas_like/expr.py index 4e858740c..d57189f3a 100644 --- a/narwhals/_pandas_like/expr.py +++ b/narwhals/_pandas_like/expr.py @@ -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, diff --git a/narwhals/_pandas_like/series.py b/narwhals/_pandas_like/series.py index d82a30668..24bf4fc11 100644 --- a/narwhals/_pandas_like/series.py +++ b/narwhals/_pandas_like/series.py @@ -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( diff --git a/narwhals/expression.py b/narwhals/expression.py index a1631a57d..fcf180286 100644 --- a/narwhals/expression.py +++ b/narwhals/expression.py @@ -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. @@ -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`: @@ -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: diff --git a/narwhals/series.py b/narwhals/series.py index a776f1d93..63f42ff25 100644 --- a/narwhals/series.py +++ b/narwhals/series.py @@ -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: """