Skip to content

Commit

Permalink
compare expand_cols by variant not exact datatype (#3050)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Apr 3, 2022
1 parent a5a5570 commit de794e0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
7 changes: 6 additions & 1 deletion polars/polars-lazy/src/logical_plan/projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,12 @@ fn expand_columns(expr: &Expr, result: &mut Vec<Expr>, names: &[String]) {
/// replace `DtypeColumn` with `col("foo")..col("bar")`
fn expand_dtypes(expr: &Expr, result: &mut Vec<Expr>, schema: &Schema, dtypes: &[DataType]) {
for dtype in dtypes {
for field in schema.iter_fields().filter(|f| f.data_type() == dtype) {
// we compare by variant not by exact datatype as units/ refmaps etc may differ.
let variant = std::mem::discriminant(dtype);
for field in schema
.iter_fields()
.filter(|f| std::mem::discriminant(f.data_type()) == variant)
{
let name = field.name();

let mut new_expr = expr.clone();
Expand Down
39 changes: 39 additions & 0 deletions polars/tests/it/lazy/expressions/expand.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use super::*;
use polars::export::chrono::{NaiveDate, NaiveDateTime};

#[test]
fn test_expand_datetimes_3042() -> Result<()> {
let low = NaiveDate::from_ymd(2020, 1, 1).and_hms(0, 0, 0);
let high = NaiveDate::from_ymd(2020, 2, 1).and_hms(0, 0, 0);
let date_range = date_range(
"dt1",
low,
high,
Duration::parse("1w"),
ClosedWindow::Left,
TimeUnit::Milliseconds,
)
.into_series();

let out = df![
"dt1" => date_range.clone(),
"dt2" => date_range,
]?
.lazy()
// this tests if we expand datetimes even though the units differ
.with_column(
dtype_col(&DataType::Datetime(TimeUnit::Microseconds, None))
.dt()
.strftime("%m/%d/%Y"),
)
.limit(3)
.collect()?;

let expected = df![
"dt1" => ["01/01/2020", "01/08/2020", "01/15/2020"],
"dt2" => ["01/01/2020", "01/08/2020", "01/15/2020"],
]?;
assert!(out.frame_equal(&expected));

Ok(())
}
1 change: 1 addition & 0 deletions polars/tests/it/lazy/expressions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod apply;
mod arity;
mod expand;
mod is_in;
mod slice;
mod window;
Expand Down

0 comments on commit de794e0

Please sign in to comment.