Skip to content

Commit 0e80656

Browse files
chore: factor out log from SparkLike, DuckDB, and Ibis (#2915)
--------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 277881a commit 0e80656

File tree

4 files changed

+14
-32
lines changed

4 files changed

+14
-32
lines changed

narwhals/_duckdb/expr.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -292,17 +292,6 @@ def window_f(df: DuckDBLazyFrame, inputs: DuckDBWindowInputs) -> list[Expression
292292
version=self._version,
293293
)
294294

295-
def log(self, base: float) -> Self:
296-
def _log(expr: Expression) -> Expression:
297-
log = F("log", expr)
298-
return (
299-
when(expr < lit(0), lit(float("nan")))
300-
.when(expr == lit(0), lit(float("-inf")))
301-
.otherwise(log / F("log", lit(base)))
302-
)
303-
304-
return self._with_elementwise(_log)
305-
306295
@property
307296
def str(self) -> DuckDBExprStringNamespace:
308297
return DuckDBExprStringNamespace(self)

narwhals/_ibis/expr.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -324,17 +324,6 @@ def _rank(expr: ir.Column) -> ir.Column:
324324

325325
return self._with_callable(_rank)
326326

327-
def log(self, base: float) -> Self:
328-
def _log(expr: ir.NumericColumn) -> ir.Value:
329-
otherwise = expr.log(cast("ir.NumericValue", lit(base)))
330-
return ibis.cases(
331-
(expr < lit(0), lit(float("nan"))),
332-
(expr == lit(0), lit(float("-inf"))),
333-
else_=otherwise,
334-
)
335-
336-
return self._with_callable(_log)
337-
338327
@property
339328
def str(self) -> IbisExprStringNamespace:
340329
return IbisExprStringNamespace(self)

narwhals/_spark_like/expr.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -377,16 +377,6 @@ def _fill_constant(expr: Column, value: Column) -> Column:
377377

378378
return self._with_elementwise(_fill_constant, value=value)
379379

380-
def log(self, base: float) -> Self:
381-
def _log(expr: Column) -> Column:
382-
return (
383-
self._F.when(expr < 0, self._F.lit(float("nan")))
384-
.when(expr == 0, self._F.lit(float("-inf")))
385-
.otherwise(self._F.log(float(base), expr))
386-
)
387-
388-
return self._with_elementwise(_log)
389-
390380
@property
391381
def str(self) -> SparkLikeExprStringNamespace:
392382
return SparkLikeExprStringNamespace(self)

narwhals/_sql/expr.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,20 @@ def _sqrt(expr: NativeExprT) -> NativeExprT:
510510
def exp(self) -> Self:
511511
return self._with_elementwise(lambda expr: self._function("exp", expr))
512512

513+
def log(self, base: float) -> Self:
514+
def _log(expr: NativeExprT) -> NativeExprT:
515+
return self._when(
516+
expr < self._lit(0), # type: ignore[operator]
517+
self._lit(float("nan")),
518+
self._when(
519+
cast("NativeExprT", expr == self._lit(0)),
520+
self._lit(float("-inf")),
521+
self._function("log", expr) / self._function("log", self._lit(base)), # type: ignore[operator]
522+
),
523+
)
524+
525+
return self._with_elementwise(_log)
526+
513527
# Cumulative
514528
def cum_sum(self, *, reverse: bool) -> Self:
515529
return self._with_window_function(self._cum_window_func("sum", reverse=reverse))

0 commit comments

Comments
 (0)