Skip to content

Commit

Permalink
it gets better
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Feb 21, 2024
1 parent 4ab01a8 commit 5739a03
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
40 changes: 20 additions & 20 deletions narwhals/pandas_like/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -139,7 +139,7 @@ def join(

def lazy(self) -> LazyFrame:
return LazyFrame(
self.dataframe,
self._dataframe,
api_version=self._api_version,
implementation=self._implementation,
)
Expand All @@ -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)

Expand All @@ -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}) "
Expand All @@ -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(
Expand Down Expand Up @@ -261,15 +261,15 @@ 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,
*exprs: IntoExpr | Iterable[IntoExpr],
**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)
Expand All @@ -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:
Expand Down Expand Up @@ -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,
Expand All @@ -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,
)
Expand All @@ -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))
2 changes: 1 addition & 1 deletion narwhals/pandas_like/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
2 changes: 1 addition & 1 deletion narwhals/pandas_like/group_by_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions narwhals/pandas_like/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
)
Expand Down
2 changes: 1 addition & 1 deletion narwhals/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 5739a03

Please sign in to comment.