Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Mar 16, 2024
1 parent 18af3fe commit 3043ef9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 20 deletions.
12 changes: 6 additions & 6 deletions narwhals/pandas_like/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from narwhals.dtypes import DType
from narwhals.pandas_like.group_by import PandasGroupBy
from narwhals.pandas_like.series import PandasSeries
from narwhals.pandas_like.typing import IntoExpr
from narwhals.pandas_like.typing import IntoPandasExpr


class PandasDataFrame:
Expand Down Expand Up @@ -92,8 +92,8 @@ def schema(self) -> dict[str, DType]:
# --- reshape ---
def select(
self,
*exprs: IntoExpr | Iterable[IntoExpr],
**named_exprs: IntoExpr,
*exprs: IntoPandasExpr | Iterable[IntoPandasExpr],
**named_exprs: IntoPandasExpr,
) -> Self:
new_series = evaluate_into_exprs(self, *exprs, **named_exprs)
df = horizontal_concat(
Expand All @@ -104,7 +104,7 @@ def select(

def filter(
self,
*predicates: IntoExpr | Iterable[IntoExpr],
*predicates: IntoPandasExpr | Iterable[IntoPandasExpr],
) -> Self:
from narwhals.pandas_like.namespace import Namespace

Expand All @@ -117,8 +117,8 @@ def filter(

def with_columns(
self,
*exprs: IntoExpr | Iterable[IntoExpr],
**named_exprs: IntoExpr,
*exprs: IntoPandasExpr | Iterable[IntoPandasExpr],
**named_exprs: IntoPandasExpr,
) -> Self:
new_series = evaluate_into_exprs(self, *exprs, **named_exprs)
df = self._dataframe.assign(
Expand Down
6 changes: 3 additions & 3 deletions narwhals/pandas_like/group_by.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
if TYPE_CHECKING:
from narwhals.pandas_like.dataframe import PandasDataFrame
from narwhals.pandas_like.expr import PandasExpr
from narwhals.pandas_like.typing import IntoExpr
from narwhals.pandas_like.typing import IntoPandasExpr


class PandasGroupBy:
Expand All @@ -33,8 +33,8 @@ def __init__(

def agg(
self,
*aggs: IntoExpr | Iterable[IntoExpr],
**named_aggs: IntoExpr,
*aggs: IntoPandasExpr | Iterable[IntoPandasExpr],
**named_aggs: IntoPandasExpr,
) -> PandasDataFrame:
df = self._df._dataframe
exprs = parse_into_exprs(
Expand Down
14 changes: 10 additions & 4 deletions narwhals/pandas_like/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from narwhals.utils import flatten_str

if TYPE_CHECKING:
from narwhals.pandas_like.typing import IntoExpr
from narwhals.pandas_like.typing import IntoPandasExpr


class Namespace:
Expand Down Expand Up @@ -153,13 +153,19 @@ def len(self) -> PandasExpr:
)

# --- horizontal ---
def sum_horizontal(self, *exprs: IntoExpr | Iterable[IntoExpr]) -> PandasExpr:
def sum_horizontal(
self, *exprs: IntoPandasExpr | Iterable[IntoPandasExpr]
) -> PandasExpr:
return reduce(lambda x, y: x + y, parse_into_exprs(self._implementation, *exprs))

def all_horizontal(self, *exprs: IntoExpr | Iterable[IntoExpr]) -> PandasExpr:
def all_horizontal(
self, *exprs: IntoPandasExpr | Iterable[IntoPandasExpr]
) -> PandasExpr:
return reduce(lambda x, y: x & y, parse_into_exprs(self._implementation, *exprs))

def any_horizontal(self, *exprs: IntoExpr | Iterable[IntoExpr]) -> PandasExpr:
def any_horizontal(
self, *exprs: IntoPandasExpr | Iterable[IntoPandasExpr]
) -> PandasExpr:
return reduce(lambda x, y: x | y, parse_into_exprs(self._implementation, *exprs))

def concat(
Expand Down
2 changes: 1 addition & 1 deletion narwhals/pandas_like/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
from narwhals.pandas_like.expr import PandasExpr
from narwhals.pandas_like.series import PandasSeries

IntoExpr: TypeAlias = PandasExpr | str | int | float | PandasSeries
IntoPandasExpr: TypeAlias = PandasExpr | str | int | float | PandasSeries
16 changes: 10 additions & 6 deletions narwhals/pandas_like/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

ExprT = TypeVar("ExprT", bound=PandasExpr)

from narwhals.pandas_like.typing import IntoExpr
from narwhals.pandas_like.typing import IntoPandasExpr


def validate_column_comparand(other: Any) -> Any:
Expand Down Expand Up @@ -85,15 +85,17 @@ def maybe_evaluate_expr(df: PandasDataFrame, arg: Any) -> Any:


def parse_into_exprs(
implementation: str, *exprs: IntoExpr | Iterable[IntoExpr], **named_exprs: IntoExpr
implementation: str,
*exprs: IntoPandasExpr | Iterable[IntoPandasExpr],
**named_exprs: IntoPandasExpr,
) -> list[PandasExpr]:
out = [parse_into_expr(implementation, into_expr) for into_expr in flatten(*exprs)] # type: ignore[arg-type]
for name, expr in named_exprs.items():
out.append(parse_into_expr(implementation, expr).alias(name))
return out


def parse_into_expr(implementation: str, into_expr: IntoExpr) -> PandasExpr:
def parse_into_expr(implementation: str, into_expr: IntoPandasExpr) -> PandasExpr:
from narwhals.expression import Expr
from narwhals.pandas_like.namespace import Namespace

Expand All @@ -107,7 +109,9 @@ def parse_into_expr(implementation: str, into_expr: IntoExpr) -> PandasExpr:
raise TypeError(msg)


def evaluate_into_expr(df: PandasDataFrame, into_expr: IntoExpr) -> list[PandasSeries]:
def evaluate_into_expr(
df: PandasDataFrame, into_expr: IntoPandasExpr
) -> list[PandasSeries]:
"""
Return list of raw columns.
"""
Expand All @@ -117,8 +121,8 @@ def evaluate_into_expr(df: PandasDataFrame, into_expr: IntoExpr) -> list[PandasS

def evaluate_into_exprs(
df: PandasDataFrame,
*exprs: IntoExpr | Iterable[IntoExpr],
**named_exprs: IntoExpr,
*exprs: IntoPandasExpr | Iterable[IntoPandasExpr],
**named_exprs: IntoPandasExpr,
) -> list[PandasSeries]:
"""Evaluate each expr into Series."""
series: list[PandasSeries] = [
Expand Down

0 comments on commit 3043ef9

Please sign in to comment.