Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 12 additions & 47 deletions narwhals_daft/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,10 @@ def _rolling_window_func(
func_name: Literal["sum", "mean", "std", "var"],
window_size: int,
min_samples: int,
ddof: int | None = None,
ddof: int = 1,
*,
center: bool,
) -> WindowFunction:
supported_funcs = ["sum", "mean", "std", "var"]
if center:
half = (window_size - 1) // 2
remainder = (window_size - 1) % 2
Expand All @@ -162,23 +161,14 @@ def _rolling_window_func(
start = -(window_size - 1)
end = 0

def agg(expr: Expression) -> Expression:
if func_name == "std":
return expr.stddev(ddof=ddof)
if func_name == "var":
return expr.var(ddof=ddof)
return getattr(F, func_name)(expr)

def func(df: DaftLazyFrame, inputs: WindowInputs) -> Sequence[Expression]:
if func_name in {"sum", "mean"}:
func_: str = func_name
elif func_name == "var" and ddof == 0:
func_ = "var_pop"
elif func_name in "var" and ddof == 1:
func_ = "var_samp"
elif func_name == "std" and ddof == 0:
func_ = "stddev_pop"
elif func_name == "std" and ddof == 1:
func_ = "stddev_samp"
elif func_name in {"var", "std"}: # pragma: no cover
msg = f"Only ddof=0 and ddof=1 are currently supported for rolling_{func_name}."
raise ValueError(msg)
else: # pragma: no cover
msg = f"Only the following functions are supported: {supported_funcs}.\nGot: {func_name}."
raise ValueError(msg)
window_kwargs: Any = {
"partition_by": inputs.partition_by,
"order_by": inputs.order_by,
Expand All @@ -189,7 +179,7 @@ def func(df: DaftLazyFrame, inputs: WindowInputs) -> Sequence[Expression]:
F.when(
self._window_expression(expr.count(), **window_kwargs)
>= lit(min_samples),
self._window_expression(getattr(F, func_)(expr), **window_kwargs),
self._window_expression(agg(expr), **window_kwargs),
)
for expr in self(df)
]
Expand Down Expand Up @@ -567,35 +557,10 @@ def len(self) -> DaftExpr:
return self._with_callable(lambda _input: _input.count("all"))

def std(self, ddof: int) -> DaftExpr:
def func(expr: Expression) -> Expression:
std_pop = expr.stddev()
if ddof == 0:
return std_pop
n_samples = expr.count(mode="valid")
return std_pop * n_samples.sqrt() / (n_samples - ddof).sqrt()

def window_func(df: DaftLazyFrame, inputs: WindowInputs) -> list[Expression]:
assert not inputs.order_by # noqa: S101
w = Window().partition_by(*inputs.partition_by)
return [
expr.stddev().over(w)
* expr.count().over(w).sqrt()
/ (expr.count().over(w) - lit(ddof)).sqrt()
for expr in self(df)
]

return self._with_callable(func, window_func)
return self._with_callable(lambda expr: expr.stddev(ddof=ddof))

def var(self, ddof: int) -> DaftExpr:
def func(expr: Expression) -> Expression:
std_pop = expr.stddev()
var_pop = std_pop * std_pop
if ddof == 0:
return var_pop
n_samples = expr.count(mode="valid")
return var_pop * n_samples / (n_samples - ddof)

return self._with_callable(func)
return self._with_callable(lambda expr: expr.var(ddof=ddof))

def max(self) -> DaftExpr:
return self._with_callable(lambda _input: _input.max())
Expand All @@ -610,7 +575,7 @@ def is_null(self) -> DaftExpr:
return self._with_elementwise(lambda _input: _input.is_null())

def is_nan(self) -> DaftExpr:
return self._with_elementwise(lambda _input: _input.float.is_nan())
return self._with_elementwise(F.is_nan)

def is_finite(self) -> DaftExpr:
return self._with_elementwise(
Expand Down
3 changes: 2 additions & 1 deletion narwhals_daft/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ def func(cols: list[Expression]) -> Expression:
return F.when(cols[1], cols[0])

def func_with_otherwise(cols: list[Expression]) -> Expression:
return F.when(cols[1], cols[0]).otherwise(cols[2])
# A null predicate must fall through to `otherwise`, Daft yields null.
return F.when(cols[1].fill_null(lit(False)), cols[0]).otherwise(cols[2])

if otherwise is None:
return self._expr._from_elementwise_horizontal_op(func, then, predicate)
Expand Down
16 changes: 0 additions & 16 deletions run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import sys

TESTS_THAT_NEED_FIX = [
"test_add",
"test_all_kind_of_aggs",
"test_arithmetic_expr_left_literal",
"test_cast",
"test_cast_binary",
Expand Down Expand Up @@ -33,7 +31,6 @@
"test_expr_is_in_with_null_in_other",
"test_expr_is_in_with_only_null_in_other",
"test_expr_rfloordiv_by_zero",
"test_fancy_functions",
"test_fill_null_limits",
"test_fill_null_strategies_with_limit_as_none",
"test_fill_null_strategies_with_partition_by",
Expand All @@ -50,12 +47,7 @@
"test_floordiv_unsigned_dtype",
"test_get_field_expr",
"test_group_by_categorical",
"test_group_by_depth_1_agg",
"test_group_by_depth_1_std_var",
"test_is_close_expr_with_expr",
"test_is_close_expr_with_scalar",
"test_is_finite_expr",
"test_issue_3474_expr_decimal",
"test_joinasof_by",
"test_joinasof_numeric",
"test_joinasof_suffix",
Expand All @@ -75,7 +67,6 @@
"test_mode_over_nulls",
"test_n_unique_over",
"test_nan",
"test_nan_non_float",
"test_native_namespace_frame",
"test_nested_structures",
"test_numeric_reduction_keeps_dtype_when_all_null",
Expand All @@ -85,7 +76,6 @@
"test_offset_by_invalid_interval",
"test_offset_by_tz",
"test_over_pushdown",
"test_over_std_var",
"test_over_without_partition_by_categorical",
"test_package_version",
"test_parse_weight",
Expand All @@ -104,16 +94,12 @@
"test_replace_time_zone_none",
"test_right_arithmetic_expr",
"test_rmod_negative_operands",
"test_rolling_std_expr_lazy_grouped",
"test_rolling_std_expr_lazy_ungrouped",
"test_scalar_operand_broadcasting",
"test_select_duplicates",
"test_set_ops",
"test_skew_expr",
"test_sort_categorical",
"test_sort_categorical_empty",
"test_std",
"test_std_broadcating",
"test_str_replace_value_edge_expr_scalar",
"test_str_slice",
"test_str_slice_negative_offset",
Expand Down Expand Up @@ -141,8 +127,6 @@
"test_unique_full_subset",
"test_unique_maintain_order_expr",
"test_unique_none",
"test_var",
"test_when_chain_with_nulls",
"test_with_columns_dtypes_single_row",
"test_with_columns_missing_column",
"test_with_row_index_order_by_categorical",
Expand Down
Loading