-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adds .summarize() to compute statistics (#3810)
- Loading branch information
Showing
8 changed files
with
128 additions
and
0 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use common_error::DaftResult; | ||
use daft_core::prelude::CountMode; | ||
use daft_dsl::{Expr, ExprRef, Literal}; | ||
use daft_schema::dtype::DataType; | ||
|
||
use crate::LogicalPlanBuilder; | ||
|
||
/// Creates a DataFrame summary by aggregating column stats into lists then exploding. | ||
pub fn summarize(input: &LogicalPlanBuilder) -> DaftResult<LogicalPlanBuilder> { | ||
// create the agg lists (avg is blocked on try_cast) | ||
let mut cols: Vec<ExprRef> = vec![]; // column :: utf8 | ||
let mut typs: Vec<ExprRef> = vec![]; // type :: utf8 | ||
let mut mins: Vec<ExprRef> = vec![]; // min :: utf8 | ||
let mut maxs: Vec<ExprRef> = vec![]; // max :: utf8 | ||
let mut cnts: Vec<ExprRef> = vec![]; // count :: int64 | ||
let mut nuls: Vec<ExprRef> = vec![]; // nulls :: int64 | ||
let mut unqs: Vec<ExprRef> = vec![]; // approx_distinct :: int64 | ||
for (_, field) in &input.schema().fields { | ||
let col = daft_dsl::col(field.name.as_str()); | ||
cols.push(field.name.to_string().lit()); | ||
typs.push(field.dtype.to_string().lit()); | ||
mins.push(col.clone().min().cast(&DataType::Utf8)); | ||
maxs.push(col.clone().max().cast(&DataType::Utf8)); | ||
cnts.push(col.clone().count(CountMode::Valid)); | ||
nuls.push(col.clone().count(CountMode::Null)); | ||
unqs.push(col.clone().approx_count_distinct()); | ||
} | ||
// apply aggregations lists | ||
let input = input.aggregate( | ||
vec![ | ||
list_(cols, "column"), | ||
list_(typs, "type"), | ||
list_(mins, "min"), | ||
list_(maxs, "max"), | ||
list_(cnts, "count"), | ||
list_(nuls, "count_nulls"), | ||
list_(unqs, "approx_count_distinct"), | ||
], | ||
vec![], | ||
)?; | ||
// apply explode for all columns | ||
input.explode(input.columns()) | ||
} | ||
|
||
/// Creates a list constructor for the given items. | ||
fn list_(items: Vec<ExprRef>, alias: &str) -> ExprRef { | ||
Expr::List(items).arced().alias(alias) | ||
} |
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,36 @@ | ||
from __future__ import annotations | ||
|
||
import daft | ||
|
||
|
||
def test_summarize_sanity(): | ||
df = daft.from_pydict( | ||
{ | ||
"A": [1, 2, 3, 4, 5], | ||
"B": [1.5, 2.5, 3.5, 4.5, 5.5], | ||
"C": [True, True, False, False, None], | ||
"D": [None, None, None, None, None], | ||
} | ||
) | ||
# row for each column of input | ||
assert df.summarize().count_rows() == 4 | ||
assert df.select("A", "B").summarize().count_rows() == 2 | ||
|
||
|
||
def test_summarize_dataframe(make_df, valid_data: list[dict[str, float]]) -> None: | ||
df = daft.from_pydict( | ||
{ | ||
"a": [1, 2, 3, 3], | ||
"b": [None, "a", "b", "c"], | ||
} | ||
) | ||
expected = { | ||
"column": ["a", "b"], | ||
"type": ["Int64", "Utf8"], | ||
"min": ["1", "a"], | ||
"max": ["3", "c"], | ||
"count": [4, 3], | ||
"count_nulls": [0, 1], | ||
"approx_count_distinct": [3, 3], | ||
} | ||
assert df.summarize().to_pydict() == expected |