From 7f7ffc86c8c00bf8353f20d331425362c0ed9d2f Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Tue, 21 Jan 2025 11:46:51 +0000 Subject: [PATCH] chore: simplify _pandas_like/series.py renaming --- narwhals/_pandas_like/namespace.py | 27 +-- narwhals/_pandas_like/series.py | 286 ++++++--------------------- narwhals/_pandas_like/series_list.py | 12 +- 3 files changed, 75 insertions(+), 250 deletions(-) diff --git a/narwhals/_pandas_like/namespace.py b/narwhals/_pandas_like/namespace.py index 84efef836..3b6f9efa3 100644 --- a/narwhals/_pandas_like/namespace.py +++ b/narwhals/_pandas_like/namespace.py @@ -18,7 +18,6 @@ from narwhals._pandas_like.utils import create_compliant_series from narwhals._pandas_like.utils import diagonal_concat from narwhals._pandas_like.utils import horizontal_concat -from narwhals._pandas_like.utils import rename from narwhals._pandas_like.utils import vertical_concat from narwhals.typing import CompliantNamespace from narwhals.utils import import_dtypes_module @@ -267,18 +266,13 @@ def func(df: PandasLikeDataFrame) -> list[PandasLikeSeries]: return [ PandasLikeSeries( - native_series=rename( - self.concat( - (s.to_frame() for s in series), how="horizontal" - )._native_frame.min(axis=1), - series[0].name, - implementation=self._implementation, - backend_version=self._backend_version, - ), + self.concat( + (s.to_frame() for s in series), how="horizontal" + )._native_frame.min(axis=1), implementation=self._implementation, backend_version=self._backend_version, version=self._version, - ) + ).alias(series[0].name) ] return self._create_expr_from_callable( @@ -298,18 +292,13 @@ def func(df: PandasLikeDataFrame) -> list[PandasLikeSeries]: return [ PandasLikeSeries( - rename( - self.concat( - (s.to_frame() for s in series), how="horizontal" - )._native_frame.max(axis=1), - series[0].name, - implementation=self._implementation, - backend_version=self._backend_version, - ), + self.concat( + (s.to_frame() for s in series), how="horizontal" + )._native_frame.max(axis=1), implementation=self._implementation, backend_version=self._backend_version, version=self._version, - ) + ).alias(series[0].name) ] return self._create_expr_from_callable( diff --git a/narwhals/_pandas_like/series.py b/narwhals/_pandas_like/series.py index 71969444e..c7702fb85 100644 --- a/narwhals/_pandas_like/series.py +++ b/narwhals/_pandas_like/series.py @@ -291,14 +291,7 @@ def is_between( res = ser.ge(lower_bound) & ser.le(upper_bound) else: # pragma: no cover raise AssertionError - return self._from_native_series( - rename( - res, - ser.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + return self._from_native_series(res).alias(ser.name) def is_in(self, other: Any) -> PandasLikeSeries: ser = self._native_series @@ -329,278 +322,151 @@ def filter(self, other: Any) -> PandasLikeSeries: ser, other = broadcast_align_and_extract_native(self, other) else: ser = self._native_series - return self._from_native_series( - rename( - ser.loc[other], - ser.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + return self._from_native_series(ser.loc[other]).alias(ser.name) def __eq__(self, other: object) -> PandasLikeSeries: # type: ignore[override] ser, other = broadcast_align_and_extract_native(self, other) return self._from_native_series( - rename( - ser.__eq__(other), - ser.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + ser.__eq__(other), + ).alias(ser.name) def __ne__(self, other: object) -> PandasLikeSeries: # type: ignore[override] ser, other = broadcast_align_and_extract_native(self, other) return self._from_native_series( - rename( - ser.__ne__(other), - ser.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + ser.__ne__(other), + ).alias(ser.name) def __ge__(self, other: Any) -> PandasLikeSeries: ser, other = broadcast_align_and_extract_native(self, other) return self._from_native_series( - rename( - ser.__ge__(other), - ser.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + ser.__ge__(other), + ).alias(ser.name) def __gt__(self, other: Any) -> PandasLikeSeries: ser, other = broadcast_align_and_extract_native(self, other) return self._from_native_series( - rename( - ser.__gt__(other), - ser.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + ser.__gt__(other), + ).alias(ser.name) def __le__(self, other: Any) -> PandasLikeSeries: ser, other = broadcast_align_and_extract_native(self, other) return self._from_native_series( - rename( - ser.__le__(other), - ser.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + ser.__le__(other), + ).alias(ser.name) def __lt__(self, other: Any) -> PandasLikeSeries: ser, other = broadcast_align_and_extract_native(self, other) return self._from_native_series( - rename( - ser.__lt__(other), - ser.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + ser.__lt__(other), + ).alias(ser.name) def __and__(self, other: Any) -> PandasLikeSeries: ser, other = broadcast_align_and_extract_native(self, other) return self._from_native_series( - rename( - ser.__and__(other), - ser.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + ser.__and__(other), + ).alias(ser.name) def __rand__(self, other: Any) -> PandasLikeSeries: ser, other = broadcast_align_and_extract_native(self, other) return self._from_native_series( - rename( - ser.__and__(other), - ser.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + ser.__and__(other), + ).alias(ser.name) def __or__(self, other: Any) -> PandasLikeSeries: ser, other = broadcast_align_and_extract_native(self, other) return self._from_native_series( - rename( - ser.__or__(other), - ser.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + ser.__or__(other), + ).alias(ser.name) def __ror__(self, other: Any) -> PandasLikeSeries: ser, other = broadcast_align_and_extract_native(self, other) return self._from_native_series( - rename( - ser.__or__(other), - ser.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + ser.__or__(other), + ).alias(ser.name) def __add__(self, other: Any) -> PandasLikeSeries: ser, other = broadcast_align_and_extract_native(self, other) return self._from_native_series( - rename( - ser.__add__(other), - ser.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + ser.__add__(other), + ).alias(ser.name) def __radd__(self, other: Any) -> PandasLikeSeries: ser, other = broadcast_align_and_extract_native(self, other) return self._from_native_series( - rename( - ser.__radd__(other), - ser.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + ser.__radd__(other), + ).alias(ser.name) def __sub__(self, other: Any) -> PandasLikeSeries: ser, other = broadcast_align_and_extract_native(self, other) return self._from_native_series( - rename( - ser.__sub__(other), - ser.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + ser.__sub__(other), + ).alias(ser.name) def __rsub__(self, other: Any) -> PandasLikeSeries: ser, other = broadcast_align_and_extract_native(self, other) return self._from_native_series( - rename( - ser.__rsub__(other), - ser.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + ser.__rsub__(other), + ).alias(ser.name) def __mul__(self, other: Any) -> PandasLikeSeries: ser, other = broadcast_align_and_extract_native(self, other) return self._from_native_series( - rename( - ser.__mul__(other), - ser.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + ser.__mul__(other), + ).alias(ser.name) def __rmul__(self, other: Any) -> PandasLikeSeries: ser, other = broadcast_align_and_extract_native(self, other) return self._from_native_series( - rename( - ser.__rmul__(other), - ser.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + ser.__rmul__(other), + ).alias(ser.name) def __truediv__(self, other: Any) -> PandasLikeSeries: ser, other = broadcast_align_and_extract_native(self, other) return self._from_native_series( - rename( - ser.__truediv__(other), - ser.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + ser.__truediv__(other), + ).alias(ser.name) def __rtruediv__(self, other: Any) -> PandasLikeSeries: ser, other = broadcast_align_and_extract_native(self, other) return self._from_native_series( - rename( - ser.__rtruediv__(other), - ser.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + ser.__rtruediv__(other), + ).alias(ser.name) def __floordiv__(self, other: Any) -> PandasLikeSeries: ser, other = broadcast_align_and_extract_native(self, other) return self._from_native_series( - rename( - ser.__floordiv__(other), - ser.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + ser.__floordiv__(other), + ).alias(ser.name) def __rfloordiv__(self, other: Any) -> PandasLikeSeries: ser, other = broadcast_align_and_extract_native(self, other) return self._from_native_series( - rename( - ser.__rfloordiv__(other), - ser.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + ser.__rfloordiv__(other), + ).alias(ser.name) def __pow__(self, other: Any) -> PandasLikeSeries: ser, other = broadcast_align_and_extract_native(self, other) return self._from_native_series( - rename( - ser.__pow__(other), - ser.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + ser.__pow__(other), + ).alias(ser.name) def __rpow__(self, other: Any) -> PandasLikeSeries: ser, other = broadcast_align_and_extract_native(self, other) return self._from_native_series( - rename( - ser.__rpow__(other), - ser.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + ser.__rpow__(other), + ).alias(ser.name) def __mod__(self, other: Any) -> PandasLikeSeries: ser, other = broadcast_align_and_extract_native(self, other) return self._from_native_series( - rename( - ser.__mod__(other), - ser.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + ser.__mod__(other), + ).alias(ser.name) def __rmod__(self, other: Any) -> PandasLikeSeries: ser, other = broadcast_align_and_extract_native(self, other) return self._from_native_series( - rename( - ser.__rmod__(other), - ser.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + ser.__rmod__(other), + ).alias(ser.name) # Unary @@ -772,10 +638,10 @@ def replace_strict( } ) result = self._from_native_series( - self._native_series.to_frame() - .merge(other, on=self.name, how="left")[tmp_name] - .rename(self.name) - ) + self._native_series.to_frame().merge(other, on=self.name, how="left")[ + tmp_name + ] + ).alias(self.name) if result.is_null().sum() != self.is_null().sum(): msg = ( "replace_strict did not replace all non-null values.\n\n" @@ -790,13 +656,8 @@ def sort( ser = self._native_series na_position = "last" if nulls_last else "first" return self._from_native_series( - rename( - ser.sort_values(ascending=not descending, na_position=na_position), - self.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - ) + ser.sort_values(ascending=not descending, na_position=na_position) + ).alias(self.name) def alias(self, name: str) -> Self: if name != self.name: @@ -872,46 +733,25 @@ def to_polars(self: Self) -> pl.DataFrame: # --- descriptive --- def is_duplicated(self: Self) -> Self: res = self._native_series.duplicated(keep=False) - res = rename( - res, - self.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - return self._from_native_series(res) + return self._from_native_series(res).alias(self.name) def is_empty(self: Self) -> bool: return self._native_series.empty # type: ignore[no-any-return] def is_unique(self: Self) -> Self: - res = rename( - ~self._native_series.duplicated(keep=False), - self.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - return self._from_native_series(res) + res = ~self._native_series.duplicated(keep=False) + return self._from_native_series(res).alias(self.name) def null_count(self: Self) -> int: return self._native_series.isna().sum() # type: ignore[no-any-return] def is_first_distinct(self: Self) -> Self: - res = rename( - ~self._native_series.duplicated(keep="first"), - self.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - return self._from_native_series(res) + res = ~self._native_series.duplicated(keep="first") + return self._from_native_series(res).alias(self.name) def is_last_distinct(self: Self) -> Self: - res = rename( - ~self._native_series.duplicated(keep="last"), - self.name, - implementation=self._implementation, - backend_version=self._backend_version, - ) - return self._from_native_series(res) + res = ~self._native_series.duplicated(keep="last") + return self._from_native_series(res).alias(self.name) def is_sorted(self: Self, *, descending: bool = False) -> bool: if not isinstance(descending, bool): diff --git a/narwhals/_pandas_like/series_list.py b/narwhals/_pandas_like/series_list.py index 9d3b136b9..b8a2d20b5 100644 --- a/narwhals/_pandas_like/series_list.py +++ b/narwhals/_pandas_like/series_list.py @@ -4,7 +4,6 @@ from narwhals._pandas_like.utils import get_dtype_backend from narwhals._pandas_like.utils import narwhals_to_native_dtype -from narwhals._pandas_like.utils import rename from narwhals._pandas_like.utils import set_index from narwhals.utils import Implementation from narwhals.utils import import_dtypes_module @@ -28,12 +27,7 @@ def len(self: Self) -> PandasLikeSeries: and self._compliant_series._backend_version < (3, 0) ): # pragma: no cover native_result = set_index( - rename( - native_result, - native_series.name, - implementation=self._compliant_series._implementation, - backend_version=self._compliant_series._backend_version, - ), + native_result, index=native_series.index, implementation=self._compliant_series._implementation, backend_version=self._compliant_series._backend_version, @@ -50,4 +44,6 @@ def len(self: Self) -> PandasLikeSeries: backend_version=self._compliant_series._backend_version, version=self._compliant_series._version, ) - return self._compliant_series._from_native_series(native_result.astype(dtype)) + return self._compliant_series._from_native_series( + native_result.astype(dtype) + ).alias(native_series.name)