From 5739a03c2ab8dc5b1f54240ec9c8f1d1fbb2ef71 Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Wed, 21 Feb 2024 15:03:56 +0000 Subject: [PATCH] it gets better --- narwhals/pandas_like/dataframe.py | 40 ++++++++++++------------- narwhals/pandas_like/expr.py | 2 +- narwhals/pandas_like/group_by_object.py | 2 +- narwhals/pandas_like/namespace.py | 6 ++-- narwhals/translate.py | 2 +- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/narwhals/pandas_like/dataframe.py b/narwhals/pandas_like/dataframe.py index 1678d8d94..752dc4f95 100644 --- a/narwhals/pandas_like/dataframe.py +++ b/narwhals/pandas_like/dataframe.py @@ -41,7 +41,7 @@ def __init__( @property def columns(self) -> list[str]: - return self.dataframe.columns.tolist() # type: ignore[no-any-return] + return self._dataframe.columns.tolist() # type: ignore[no-any-return] def _dispatch_to_lazy(self, method: str, *args: Any, **kwargs: Any) -> Self: return getattr(self.lazy(), method)(*args, **kwargs).collect() # type: ignore[no-any-return] @@ -71,7 +71,7 @@ def _validate_columns(self, columns: Sequence[str]) -> None: def _validate_booleanness(self) -> None: if not ( - (self.dataframe.dtypes == "bool") | (self.dataframe.dtypes == "boolean") + (self._dataframe.dtypes == "bool") | (self._dataframe.dtypes == "boolean") ).all(): msg = "'any' can only be called on DataFrame where all dtypes are 'bool'" raise TypeError( @@ -90,7 +90,7 @@ def __dataframe_namespace__( @property def shape(self) -> tuple[int, int]: - return self.dataframe.shape # type: ignore[no-any-return] + return self._dataframe.shape # type: ignore[no-any-return] def group_by(self, *keys: str | Iterable[str]) -> GroupBy: from narwhals.pandas_like.group_by_object import GroupBy @@ -139,7 +139,7 @@ def join( def lazy(self) -> LazyFrame: return LazyFrame( - self.dataframe, + self._dataframe, api_version=self._api_version, implementation=self._implementation, ) @@ -154,15 +154,15 @@ def rename(self, mapping: dict[str, str]) -> Self: return self._dispatch_to_lazy("rename", mapping) def to_numpy(self) -> Any: - return self.dataframe.to_numpy() + return self._dataframe.to_numpy() def to_pandas(self) -> Any: if self._implementation == "pandas": - return self.dataframe + return self._dataframe elif self._implementation == "cudf": - return self.dataframe.to_pandas() + return self._dataframe.to_pandas() elif self._implementation == "modin": - return self.dataframe._to_pandas() + return self._dataframe._to_pandas() msg = f"Unknown implementation: {self._implementation}" raise TypeError(msg) @@ -184,7 +184,7 @@ def __init__( @property def columns(self) -> list[str]: - return self.dataframe.columns.tolist() # type: ignore[no-any-return] + return self._dataframe.columns.tolist() # type: ignore[no-any-return] def __repr__(self) -> str: # pragma: no cover header = f" Standard DataFrame (api_version={self._api_version}) " @@ -211,7 +211,7 @@ def _validate_columns(self, columns: Sequence[str]) -> None: def _validate_booleanness(self) -> None: if not ( - (self.dataframe.dtypes == "bool") | (self.dataframe.dtypes == "boolean") + (self._dataframe.dtypes == "bool") | (self._dataframe.dtypes == "boolean") ).all(): msg = "'any' can only be called on DataFrame where all dtypes are 'bool'" raise TypeError( @@ -261,7 +261,7 @@ def filter( # Safety: all_horizontal's expression only returns a single column. mask = expr.call(self)[0] _mask = validate_dataframe_comparand(mask) - return self._from_dataframe(self.dataframe.loc[_mask]) + return self._from_dataframe(self._dataframe.loc[_mask]) def with_columns( self, @@ -269,7 +269,7 @@ def with_columns( **named_exprs: IntoExpr, ) -> Self: new_series = evaluate_into_exprs(self, *exprs, **named_exprs) - df = self.dataframe.assign( + df = self._dataframe.assign( **{series.name: series.series for series in new_series} ) return self._from_dataframe(df) @@ -282,8 +282,8 @@ def sort( ) -> Self: flat_keys = flatten_str([*flatten_str(by), *more_by]) if not flat_keys: - flat_keys = self.dataframe.columns.tolist() - df = self.dataframe + flat_keys = self._dataframe.columns.tolist() + df = self._dataframe if isinstance(descending, bool): ascending: bool | list[bool] = not descending else: @@ -317,8 +317,8 @@ def join( raise ValueError(msg) return self._from_dataframe( - self.dataframe.merge( - other.dataframe, + self._dataframe.merge( + other._dataframe, left_on=left_on, right_on=right_on, how=how, @@ -328,7 +328,7 @@ def join( # Conversion def collect(self) -> DataFrame: return DataFrame( - self.dataframe, + self._dataframe, api_version=self._api_version, implementation=self._implementation, ) @@ -337,10 +337,10 @@ def cache(self) -> Self: return self def head(self, n: int) -> Self: - return self._from_dataframe(self.dataframe.head(n)) + return self._from_dataframe(self._dataframe.head(n)) def unique(self, subset: list[str]) -> Self: - return self._from_dataframe(self.dataframe.drop_duplicates(subset=subset)) + return self._from_dataframe(self._dataframe.drop_duplicates(subset=subset)) def rename(self, mapping: dict[str, str]) -> Self: - return self._from_dataframe(self.dataframe.rename(columns=mapping)) + return self._from_dataframe(self._dataframe.rename(columns=mapping)) diff --git a/narwhals/pandas_like/expr.py b/narwhals/pandas_like/expr.py index fec4fa06a..54090188e 100644 --- a/narwhals/pandas_like/expr.py +++ b/narwhals/pandas_like/expr.py @@ -53,7 +53,7 @@ def from_column_names( return cls( lambda df: [ Series( - df.dataframe.loc[:, column_name], + df._dataframe.loc[:, column_name], api_version=df._api_version, implementation=implementation, ) diff --git a/narwhals/pandas_like/group_by_object.py b/narwhals/pandas_like/group_by_object.py index 4296aad46..7e0d4de1e 100644 --- a/narwhals/pandas_like/group_by_object.py +++ b/narwhals/pandas_like/group_by_object.py @@ -52,7 +52,7 @@ def agg( ) -> LazyFrame: from narwhals.pandas_like.dataframe import LazyFrame - df = self._df.dataframe + df = self._df._dataframe exprs = parse_into_exprs( get_namespace(self._df), *aggs, diff --git a/narwhals/pandas_like/namespace.py b/narwhals/pandas_like/namespace.py index 7f1cfca06..2691442d4 100644 --- a/narwhals/pandas_like/namespace.py +++ b/narwhals/pandas_like/namespace.py @@ -42,7 +42,7 @@ def concat(self, items: Iterable[AnyDataFrame], *, how: str) -> AnyDataFrame: # dfs: list[Any] = [] kind: Any = {} for df in items: - dfs.append(df.dataframe) + dfs.append(df._dataframe) kind.append(type(df)) if len(kind) > 1: msg = "Can only concat DataFrames or LazyFrames, not mixtures of the two" @@ -92,7 +92,7 @@ def len(self) -> Expr: lambda df: [ Series( series_from_iterable( - [len(df.dataframe)], + [len(df._dataframe)], name="len", index=[0], implementation=self._implementation, @@ -152,7 +152,7 @@ def all(self) -> Expr: return Expr( lambda df: [ Series( - df.dataframe.loc[:, column_name], + df._dataframe.loc[:, column_name], api_version=df._api_version, implementation=self._implementation, ) diff --git a/narwhals/translate.py b/narwhals/translate.py index 71c51c8a3..d95b15460 100644 --- a/narwhals/translate.py +++ b/narwhals/translate.py @@ -60,7 +60,7 @@ def to_original_object(df: DataFrame | LazyFrame) -> Any: else: if isinstance(df, (pl.DataFrame, pl.LazyFrame)): return df - return df.dataframe # type: ignore[union-attr] + return df._dataframe # type: ignore[union-attr] def get_namespace(obj: Any, implementation: str | None = None) -> Namespace: