Но мне нужно посчитать нулевые значения
Скажем, да
Код: Выделить всё
import duckdb
rel = duckdb.sql('select * from values (1, 4), (2, null), (null, null) df(a, b)')
rel
Код: Выделить всё
Out[3]:
┌───────┬───────┐
│ a │ b │
│ int32 │ int32 │
├───────┼───────┤
│ 1 │ 4 │
│ 2 │ NULL │
│ NULL │ NULL │
└───────┴───────┘
Решение предложено в связанном вопросе:
Код: Выделить всё
def n_unique(column_name: str) -> duckdb.Expression:
return duckdb.FunctionExpression(
'array_unique',
duckdb.FunctionExpression(
'array_agg',
duckdb.ColumnExpression(column_name)
)
)
Код: Выделить всё
In [39]: rel.aggregate([n_unique('a'), n_unique('b')])
Out[39]:
┌────────────────────────────┬────────────────────────────┐
│ array_unique(array_agg(a)) │ array_unique(array_agg(b)) │
│ uint64 │ uint64 │
├────────────────────────────┼────────────────────────────┤
│ 2 │ 1 │
└────────────────────────────┴────────────────────────────┘
Код: Выделить всё
In [39]: rel.aggregate([n_unique('a'), n_unique('b')])
Out[39]:
┌────────────────────────────┬────────────────────────────┐
│ array_unique(array_agg(a)) │ array_unique(array_agg(b)) │
│ uint64 │ uint64 │
├────────────────────────────┼────────────────────────────┤
│ 3 │ 2 │
└────────────────────────────┴────────────────────────────┘
Подробнее здесь: https://stackoverflow.com/questions/793 ... unts-nulls