Skip to content

Commit

Permalink
fix: fix skew for duckdb with fewer than 3 elements
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Jan 10, 2025
1 parent 339683c commit a0f7753
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
22 changes: 17 additions & 5 deletions narwhals/_duckdb/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,25 @@ def mean(self) -> Self:
)

def skew(self) -> Self:
from duckdb import CaseExpression
from duckdb import ConstantExpression
from duckdb import FunctionExpression

return self._from_call(
lambda _input: FunctionExpression("skewness", _input),
"skew",
returns_scalar=True,
)
def func(_input: duckdb.Expression) -> duckdb.Expression:
count = FunctionExpression("count", _input)
return CaseExpression(
condition=count == 0, value=ConstantExpression(None)
).otherwise(
CaseExpression(
condition=count == 1, value=ConstantExpression(float("nan"))
).otherwise(
CaseExpression(
condition=count == 2, value=ConstantExpression(0.0)
).otherwise(FunctionExpression("skewness", _input))
)
)

return self._from_call(func, "skew", returns_scalar=True)

def median(self) -> Self:
from duckdb import FunctionExpression
Expand Down
6 changes: 1 addition & 5 deletions tests/expr_and_series/unary_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ def test_unary_series(constructor_eager: ConstructorEager) -> None:
assert_equal_data(result, expected)


def test_unary_two_elements(
constructor: Constructor, request: pytest.FixtureRequest
) -> None:
if "duckdb" in str(constructor):
request.applymarker(pytest.mark.xfail)
def test_unary_two_elements(constructor: Constructor) -> None:
data = {"a": [1, 2], "b": [2, 10], "c": [2.0, None]}
result = nw.from_native(constructor(data)).select(
a_nunique=nw.col("a").n_unique(),
Expand Down

0 comments on commit a0f7753

Please sign in to comment.