-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c380697
commit 5df15be
Showing
9 changed files
with
166 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
from __future__ import annotations | ||
from typing import Callable, Any | ||
|
||
def extract_native(expr, other: Any) -> Any: | ||
if isinstance(other, NarwhalsExpr): | ||
return other._call(expr) | ||
return other | ||
|
||
class NarwhalsExpr: | ||
def __init__(self, call: str): | ||
self._call = call | ||
|
||
# --- convert --- | ||
def alias(self, name: str) -> Self: | ||
return self.__class__(self._expr.alias(name)) | ||
|
||
def cast( | ||
self, | ||
dtype: DType, # type: ignore[override] | ||
) -> Self: | ||
return self.__class__(self._expr.cast(reverse_translate_dtype(dtype))) | ||
|
||
# --- binary --- | ||
def __eq__(self, other: object) -> Expr: # type: ignore[override] | ||
return self.__class__(self._expr.__eq__(extract_native(other))) | ||
|
||
def __and__(self, other: Any) -> Expr: | ||
return self.__class__(self._expr.__and__(extract_native(other))) | ||
|
||
def __or__(self, other: Any) -> Expr: | ||
return self.__class__(self._expr.__or__(extract_native(other))) | ||
|
||
def __add__(self, other): | ||
return self.__class__(lambda expr: self._call(expr).__add__(extract_native(expr, other))) | ||
|
||
def __radd__(self, other: Any) -> Expr: | ||
return self.__class__(self._expr.__radd__(extract_native(other))) | ||
|
||
def __sub__(self, other: Any) -> Expr: | ||
return self.__class__(self._expr.__sub__(extract_native(other))) | ||
|
||
def __rsub__(self, other: Any) -> Expr: | ||
return self.__class__(self._expr.__rsub__(extract_native(other))) | ||
|
||
def __mul__(self, other: Any) -> Expr: | ||
return self.__class__(self._expr.__mul__(extract_native(other))) | ||
|
||
def __rmul__(self, other: Any) -> Expr: | ||
return self.__class__(self._expr.__rmul__(extract_native(other))) | ||
|
||
def __le__(self, other: Any) -> Expr: | ||
return self.__class__(self._expr.__le__(extract_native(other))) | ||
|
||
def __lt__(self, other: Any) -> Expr: | ||
return self.__class__(self._expr.__lt__(extract_native(other))) | ||
|
||
def __gt__(self, other: Any) -> Expr: | ||
return self.__class__(self._expr.__gt__(extract_native(other))) | ||
|
||
def __ge__(self, other: Any) -> Expr: | ||
return self.__class__(self._expr.__ge__(extract_native(other))) | ||
|
||
# --- unary --- | ||
def mean(self) -> Expr: | ||
return self.__class__(self._expr.mean()) | ||
|
||
def sum(self) -> Expr: | ||
return self.__class__(self._expr.sum()) | ||
|
||
def min(self) -> Expr: | ||
return self.__class__(self._expr.min()) | ||
|
||
def max(self) -> Expr: | ||
return self.__class__(self._expr.max()) | ||
|
||
def n_unique(self) -> Expr: | ||
return self.__class__(self._expr.n_unique()) | ||
|
||
def unique(self) -> Expr: | ||
return self.__class__(self._expr.unique()) | ||
|
||
# --- transform --- | ||
def is_between( | ||
self, lower_bound: Any, upper_bound: Any, closed: str = "both" | ||
) -> Expr: | ||
return self.__class__(self._expr.is_between(lower_bound, upper_bound, closed)) # type: ignore[arg-type] | ||
|
||
def is_in(self, other: Any) -> Expr: | ||
return self.__class__(self._expr.is_in(other)) | ||
|
||
def is_null(self) -> Expr: | ||
return self.__class__(self._expr.is_null()) | ||
|
||
# --- partial reduction --- | ||
def drop_nulls(self) -> Expr: | ||
return self.__class__(self._expr.drop_nulls()) | ||
|
||
def sample(self, n: int, fraction: float, *, with_replacement: bool) -> Expr: | ||
return self.__class__( | ||
self._expr.sample(n, fraction=fraction, with_replacement=with_replacement) | ||
) | ||
|
||
|
||
def col(col_name: str): | ||
return NarwhalsExpr(lambda expr: expr(col_name)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.