Skip to content

Commit

Permalink
rename may_apply to try_apply (#2351)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Jan 12, 2022
1 parent 41f63cb commit 73416c4
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 83 deletions.
6 changes: 3 additions & 3 deletions polars/benches/groupby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ lazy_static! {
.with_n_rows(Some(1000000))
.finish()
.unwrap();
df.may_apply("id1", |s| s.cast(&DataType::Categorical))
df.try_apply("id1", |s| s.cast(&DataType::Categorical))
.unwrap();
df.may_apply("id2", |s| s.cast(&DataType::Categorical))
df.try_apply("id2", |s| s.cast(&DataType::Categorical))
.unwrap();
df.may_apply("id3", |s| s.cast(&DataType::Categorical))
df.try_apply("id3", |s| s.cast(&DataType::Categorical))
.unwrap();
df
};
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-core/src/doc/changelog/v0_5.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! # Changelog v0.5
//!
//! * `DataFrame.column` returns `Result<_>` **breaking change**.
//! * Define idiomatic way to do inplace operations on a `DataFrame` with `apply`, `may_apply` and `ChunkSet`
//! * Define idiomatic way to do inplace operations on a `DataFrame` with `apply`, `try_apply` and `ChunkSet`
//! * `ChunkSet` Trait.
//! * `Groupby` aggregations can be done on a selection of multiple columns.
//! * `Groupby` operation can be done on multiple keys.
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-core/src/frame/groupby/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1594,7 +1594,7 @@ mod test {
"int" => [1, 2, 3, 1, 1]
]?;

df.may_apply("g", |s| s.cast(&DataType::Categorical))?;
df.try_apply("g", |s| s.cast(&DataType::Categorical))?;

let out = df.groupby("g")?.sum()?;
dbg!(out);
Expand Down
8 changes: 4 additions & 4 deletions polars/polars-core/src/frame/hash_join/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1637,9 +1637,9 @@ mod test {

let (mut df_a, mut df_b) = get_dfs();

df_a.may_apply("b", |s| s.cast(&DataType::Categorical))
df_a.try_apply("b", |s| s.cast(&DataType::Categorical))
.unwrap();
df_b.may_apply("bar", |s| s.cast(&DataType::Categorical))
df_b.try_apply("bar", |s| s.cast(&DataType::Categorical))
.unwrap();

let out = df_a.join(&df_b, "b", "bar", JoinType::Left, None).unwrap();
Expand All @@ -1659,13 +1659,13 @@ mod test {

// Test an error when joining on different string cache
let (mut df_a, mut df_b) = get_dfs();
df_a.may_apply("b", |s| s.cast(&DataType::Categorical))
df_a.try_apply("b", |s| s.cast(&DataType::Categorical))
.unwrap();
// create a new cache
toggle_string_cache(false);
toggle_string_cache(true);

df_b.may_apply("bar", |s| s.cast(&DataType::Categorical))
df_b.try_apply("bar", |s| s.cast(&DataType::Categorical))
.unwrap();
let out = df_a.join(&df_b, "b", "bar", JoinType::Left, None);
assert!(out.is_err())
Expand Down
10 changes: 5 additions & 5 deletions polars/polars-core/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1833,7 +1833,7 @@ impl DataFrame {
///
/// let idx = vec![0, 1, 4];
///
/// df.may_apply("foo", |s| {
/// df.try_apply("foo", |s| {
/// s.utf8()?
/// .set_at_idx_with(idx, |opt_val| opt_val.map(|string| format!("{}-is-modified", string)))
/// });
Expand All @@ -1858,7 +1858,7 @@ impl DataFrame {
/// | "quack-is-modified" | 5 |
/// +---------------------+--------+
/// ```
pub fn may_apply_at_idx<F, S>(&mut self, idx: usize, f: F) -> Result<&mut Self>
pub fn try_apply_at_idx<F, S>(&mut self, idx: usize, f: F) -> Result<&mut Self>
where
F: FnOnce(&Series) -> Result<S>,
S: IntoSeries,
Expand Down Expand Up @@ -1902,7 +1902,7 @@ impl DataFrame {
/// let values = df.column("values")?;
/// let mask = values.lt_eq(1) | values.gt_eq(5_i32);
///
/// df.may_apply("foo", |s| {
/// df.try_apply("foo", |s| {
/// s.utf8()?
/// .set(&mask, Some("not_within_bounds"))
/// });
Expand All @@ -1927,15 +1927,15 @@ impl DataFrame {
/// | "not_within_bounds" | 5 |
/// +---------------------+--------+
/// ```
pub fn may_apply<F, S>(&mut self, column: &str, f: F) -> Result<&mut Self>
pub fn try_apply<F, S>(&mut self, column: &str, f: F) -> Result<&mut Self>
where
F: FnOnce(&Series) -> Result<S>,
S: IntoSeries,
{
let idx = self
.find_idx_by_name(column)
.ok_or_else(|| PolarsError::NotFound(column.to_string()))?;
self.may_apply_at_idx(idx, f)
self.try_apply_at_idx(idx, f)
}

/// Slice the `DataFrame` along the rows.
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-io/src/csv_core/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub(crate) fn cast_columns(df: &mut DataFrame, to_cast: &[&Field], parallel: boo
} else {
// cast to the original dtypes in the schema
for fld in to_cast {
df.may_apply(fld.name(), |s| cast_fn(s, fld))?;
df.try_apply(fld.name(), |s| cast_fn(s, fld))?;
}
}
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-io/src/parquet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ mod test {
"datetime" => [Some(191845729i64), Some(89107598), None, Some(3158971092)]
]?;

df.may_apply("datetime", |s| {
df.try_apply("datetime", |s| {
s.cast(&DataType::Datetime(TimeUnit::Nanoseconds, None))
})?;

Expand Down
4 changes: 2 additions & 2 deletions polars/polars-lazy/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(feature = "parquet")]
mod io;
mod predicate_pushdown;
mod projection_pushdown;
mod optimization_checks;
mod projection_queries;
mod queries;

use polars_core::prelude::*;
Expand Down
96 changes: 96 additions & 0 deletions polars/polars-lazy/src/tests/optimization_checks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use super::*;

fn projection_at_scan(lp_arena: &Arena<ALogicalPlan>, lp: Node) -> bool {
(&lp_arena).iter(lp).any(|(_, lp)| {
use ALogicalPlan::*;
match lp {
DataFrameScan {
selection: Some(_), ..
}
| CsvScan {
predicate: Some(_), ..
}
| ParquetScan {
predicate: Some(_), ..
}
| IpcScan {
predicate: Some(_), ..
} => true,
_ => false,
}
})
}

fn slice_at_scan(lp_arena: &Arena<ALogicalPlan>, lp: Node) -> bool {
(&lp_arena).iter(lp).any(|(_, lp)| {
use ALogicalPlan::*;
match lp {
CsvScan { options, .. } => options.n_rows.is_some(),
ParquetScan { options, .. } => options.n_rows.is_some(),
IpcScan { options, .. } => options.n_rows.is_some(),
_ => false,
}
})
}

#[test]
fn test_pred_pd_1() -> Result<()> {
let df = fruits_cars();

let mut expr_arena = Arena::with_capacity(16);
let mut lp_arena = Arena::with_capacity(8);
let lp = df
.clone()
.lazy()
.select([col("A"), col("B")])
.filter(col("A").gt(lit(1)))
.optimize(&mut lp_arena, &mut expr_arena)?;

assert!(projection_at_scan(&lp_arena, lp));

// check if we understand that we can unwrap the alias
let lp = df
.lazy()
.select([col("A").alias("C"), col("B")])
.filter(col("C").gt(lit(1)))
.optimize(&mut lp_arena, &mut expr_arena)?;

assert!(projection_at_scan(&lp_arena, lp));

Ok(())
}

#[test]
fn test_no_left_join_pass() -> Result<()> {
let df1 = df![
"foo" => ["abc", "def", "ghi"],
"idx1" => [0, 0, 1],
]?;
let df2 = df![
"bar" => [5, 6],
"idx2" => [0, 1],
]?;

let out = df1
.lazy()
.join(df2.lazy(), [col("idx1")], [col("idx2")], JoinType::Left)
.filter(col("bar").eq(lit(5i32)))
.collect()?;

dbg!(out);
Ok(())
}

#[test]
pub fn test_simple_slice() -> Result<()> {
let mut expr_arena = Arena::with_capacity(16);
let mut lp_arena = Arena::with_capacity(8);
let q = scan_foods_parquet(false).limit(3);

let root = q.clone().optimize(&mut lp_arena, &mut expr_arena)?;
assert!(slice_at_scan(&lp_arena, root));
let out = q.collect()?;
assert_eq!(out.height(), 3);

Ok(())
}
59 changes: 0 additions & 59 deletions polars/polars-lazy/src/tests/predicate_pushdown.rs

This file was deleted.

6 changes: 3 additions & 3 deletions polars/src/docs/eager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,10 @@
//!
//!
//! // coerce numbers to floats
//! df.may_apply("number", |s: &Series| s.cast::<Float64Type>())?;
//! df.try_apply("number", |s: &Series| s.cast::<Float64Type>())?;
//!
//! // transform letters to uppercase letters
//! df.may_apply("letters", |s: &Series| {
//! df.try_apply("letters", |s: &Series| {
//! Ok(s.utf8()?.to_uppercase())
//! });
//!
Expand Down Expand Up @@ -632,7 +632,7 @@
//! .unwrap();
//!
//! for idx in 0..df.width() {
//! df.may_apply_at_idx(idx, |series| {
//! df.try_apply_at_idx(idx, |series| {
//! let mask = series.is_nan()?;
//! let ca = series.f64()?;
//! ca.set(&mask, None)
Expand Down
6 changes: 3 additions & 3 deletions polars/src/docs/performance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
//! let mut df = CsvReader::from_path(path)?
//! .finish()?;
//!
//! df.may_apply("utf8-column", |s| s.cast::<CategoricalType>())?;
//! df.try_apply("utf8-column", |s| s.cast::<CategoricalType>())?;
//! Ok(df)
//! }
//!
Expand All @@ -62,8 +62,8 @@
//! // Set a global string cache
//! toggle_string_cache(true);
//!
//! df_a.may_apply("a", |s| s.cast::<CategoricalType>())?;
//! df_b.may_apply("b", |s| s.cast::<CategoricalType>())?;
//! df_a.try_apply("a", |s| s.cast::<CategoricalType>())?;
//! df_b.try_apply("b", |s| s.cast::<CategoricalType>())?;
//! df_a.join(&df_b, "a", "b", JoinType::Inner)
//! }
//! ```
Expand Down

0 comments on commit 73416c4

Please sign in to comment.