Skip to content

Commit

Permalink
docs(rust): Fix incorrect column name in LazyFrame.sort doc example (
Browse files Browse the repository at this point in the history
…#15658)

Co-authored-by: Stijn de Gooijer <[email protected]>
  • Loading branch information
wsyxbcl and stinodego authored Apr 17, 2024
1 parent e218203 commit e39ba62
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 51 deletions.
10 changes: 5 additions & 5 deletions crates/polars-core/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1666,7 +1666,7 @@ impl DataFrame {
/// ```
/// # use polars_core::prelude::*;
/// fn example(df: &DataFrame) -> PolarsResult<DataFrame> {
/// let mask = df.column("sepal.width")?.is_not_null();
/// let mask = df.column("sepal_width")?.is_not_null();
/// df.filter(&mask)
/// }
/// ```
Expand Down Expand Up @@ -1884,16 +1884,16 @@ impl DataFrame {
/// Sort by a single column with default options:
/// ```
/// # use polars_core::prelude::*;
/// fn sort_by_a(df: &DataFrame) -> PolarsResult<DataFrame> {
/// df.sort(["a"], Default::default())
/// fn sort_by_sepal_width(df: &DataFrame) -> PolarsResult<DataFrame> {
/// df.sort(["sepal_width"], Default::default())
/// }
/// ```
/// Sort by a single column with specific order:
/// ```
/// # use polars_core::prelude::*;
/// fn sort_with_specific_order(df: &DataFrame, descending: bool) -> PolarsResult<DataFrame> {
/// df.sort(
/// ["a"],
/// ["sepal_width"],
/// SortMultipleOptions::new()
/// .with_order_descending(descending)
/// )
Expand All @@ -1904,7 +1904,7 @@ impl DataFrame {
/// # use polars_core::prelude::*;
/// fn sort_by_multiple_columns_with_specific_order(df: &DataFrame) -> PolarsResult<DataFrame> {
/// df.sort(
/// &["a", "b"],
/// &["sepal_width", "sepal_length"],
/// SortMultipleOptions::new()
/// .with_order_descendings([false, true])
/// )
Expand Down
18 changes: 9 additions & 9 deletions crates/polars-lazy/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,12 @@ impl LazyFrame {
///
/// # Example
///
/// Sort DataFrame by 'sepal.width' column:
/// Sort DataFrame by 'sepal_width' column:
/// ```rust
/// # use polars_core::prelude::*;
/// # use polars_lazy::prelude::*;
/// fn sort_by_a(df: DataFrame) -> LazyFrame {
/// df.lazy().sort(["a"], Default::default())
/// df.lazy().sort(["sepal_width"], Default::default())
/// }
/// ```
/// Sort by a single column with specific order:
Expand All @@ -273,7 +273,7 @@ impl LazyFrame {
/// # use polars_lazy::prelude::*;
/// fn sort_with_specific_order(df: DataFrame, descending: bool) -> LazyFrame {
/// df.lazy().sort(
/// ["a"],
/// ["sepal_width"],
/// SortMultipleOptions::new()
/// .with_order_descending(descending)
/// )
Expand All @@ -285,7 +285,7 @@ impl LazyFrame {
/// # use polars_lazy::prelude::*;
/// fn sort_by_multiple_columns_with_specific_order(df: DataFrame) -> LazyFrame {
/// df.lazy().sort(
/// &["a", "b"],
/// &["sepal_width", "sepal_length"],
/// SortMultipleOptions::new()
/// .with_order_descendings([false, true])
/// )
Expand Down Expand Up @@ -317,10 +317,10 @@ impl LazyFrame {
/// use polars_core::prelude::*;
/// use polars_lazy::prelude::*;
///
/// /// Sort DataFrame by 'sepal.width' column
/// /// Sort DataFrame by 'sepal_width' column
/// fn example(df: DataFrame) -> LazyFrame {
/// df.lazy()
/// .sort_by_exprs(vec![col("sepal.width")], Default::default())
/// .sort_by_exprs(vec![col("sepal_width")], Default::default())
/// }
/// ```
pub fn sort_by_exprs<E: AsRef<[Expr]>>(
Expand Down Expand Up @@ -830,8 +830,8 @@ impl LazyFrame {
///
/// fn example(df: DataFrame) -> LazyFrame {
/// df.lazy()
/// .filter(col("sepal.width").is_not_null())
/// .select(&[col("sepal.width"), col("sepal.length")])
/// .filter(col("sepal_width").is_not_null())
/// .select(&[col("sepal_width"), col("sepal_length")])
/// }
/// ```
pub fn filter(self, predicate: Expr) -> Self {
Expand Down Expand Up @@ -1263,7 +1263,7 @@ impl LazyFrame {
/// fn add_column(df: DataFrame) -> LazyFrame {
/// df.lazy()
/// .with_column(
/// when(col("sepal.length").lt(lit(5.0)))
/// when(col("sepal_length").lt(lit(5.0)))
/// .then(lit(10))
/// .otherwise(lit(1))
/// .alias("new_column_name"),
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-lazy/src/tests/arity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn test_lazy_ternary() {
let df = get_df()
.lazy()
.with_column(
when(col("sepal.length").lt(lit(5.0)))
when(col("sepal_length").lt(lit(5.0)))
.then(lit(10))
.otherwise(lit(1))
.alias("new"),
Expand Down
8 changes: 4 additions & 4 deletions crates/polars-lazy/src/tests/logical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn test_lazy_arithmetic() {
let df = get_df();
let lf = df
.lazy()
.select(&[((col("sepal.width") * lit(100)).alias("super_wide"))])
.select(&[((col("sepal_width") * lit(100)).alias("super_wide"))])
.sort(["super_wide"], SortMultipleOptions::default());

print_plans(&lf);
Expand All @@ -83,7 +83,7 @@ fn test_lazy_logical_plan_filter_and_alias_combined() {
let df = get_df();
let lf = df
.lazy()
.filter(col("sepal.width").lt(lit(3.5)))
.filter(col("sepal_width").lt(lit(3.5)))
.select(&[col("variety").alias("foo")]);

print_plans(&lf);
Expand All @@ -105,9 +105,9 @@ fn test_lazy_logical_plan_schema() {
let lp = df
.lazy()
.group_by([col("variety")])
.agg([col("sepal.width").min()])
.agg([col("sepal_width").min()])
.logical_plan;
assert!(lp.schema().unwrap().get("sepal.width").is_some());
assert!(lp.schema().unwrap().get("sepal_width").is_some());
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-lazy/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ pub(crate) fn fruits_cars() -> DataFrame {

pub(crate) fn get_df() -> DataFrame {
let s = r#"
"sepal.length","sepal.width","petal.length","petal.width","variety"
"sepal_length","sepal_width","petal_length","petal_width","variety"
5.1,3.5,1.4,.2,"Setosa"
4.9,3,1.4,.2,"Setosa"
4.7,3.2,1.3,.2,"Setosa"
Expand Down
46 changes: 23 additions & 23 deletions crates/polars-lazy/src/tests/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ fn test_lazy_exec() {
let _new = df
.clone()
.lazy()
.select([col("sepal.width"), col("variety")])
.sort(["sepal.width"], Default::default())
.select([col("sepal_width"), col("variety")])
.sort(["sepal_width"], Default::default())
.collect();

let new = df
.lazy()
.filter(not(col("sepal.width").lt(lit(3.5))))
.filter(not(col("sepal_width").lt(lit(3.5))))
.collect()
.unwrap();

let check = new.column("sepal.width").unwrap().f64().unwrap().gt(3.4);
let check = new.column("sepal_width").unwrap().f64().unwrap().gt(3.4);
assert!(check.all())
}

Expand All @@ -39,27 +39,27 @@ fn test_lazy_alias() {
let df = get_df();
let new = df
.lazy()
.select([col("sepal.width").alias("petals"), col("sepal.width")])
.select([col("sepal_width").alias("petals"), col("sepal_width")])
.collect()
.unwrap();
assert_eq!(new.get_column_names(), &["petals", "sepal.width"]);
assert_eq!(new.get_column_names(), &["petals", "sepal_width"]);
}

#[test]
fn test_lazy_melt() {
let df = get_df();

let args = MeltArgs {
id_vars: vec!["petal.width".into(), "petal.length".into()],
value_vars: vec!["sepal.length".into(), "sepal.width".into()],
id_vars: vec!["petal_width".into(), "petal_length".into()],
value_vars: vec!["sepal_length".into(), "sepal_width".into()],
..Default::default()
};

let out = df
.lazy()
.melt(args)
.filter(col("variable").eq(lit("sepal.length")))
.select([col("variable"), col("petal.width"), col("value")])
.filter(col("variable").eq(lit("sepal_length")))
.select([col("variable"), col("petal_width"), col("value")])
.collect()
.unwrap();
assert_eq!(out.shape(), (7, 3));
Expand Down Expand Up @@ -87,11 +87,11 @@ fn test_lazy_udf() {
let df = get_df();
let new = df
.lazy()
.select([col("sepal.width").map(|s| Ok(Some(s * 200.0)), GetOutput::same_type())])
.select([col("sepal_width").map(|s| Ok(Some(s * 200.0)), GetOutput::same_type())])
.collect()
.unwrap();
assert_eq!(
new.column("sepal.width").unwrap().f64().unwrap().get(0),
new.column("sepal_width").unwrap().f64().unwrap().get(0),
Some(700.0)
);
}
Expand All @@ -102,7 +102,7 @@ fn test_lazy_is_null() {
let new = df
.clone()
.lazy()
.filter(col("sepal.width").is_null())
.filter(col("sepal_width").is_null())
.collect()
.unwrap();

Expand All @@ -111,15 +111,15 @@ fn test_lazy_is_null() {
let new = df
.clone()
.lazy()
.filter(col("sepal.width").is_not_null())
.filter(col("sepal_width").is_not_null())
.collect()
.unwrap();
assert_eq!(new.height(), df.height());

let new = df
.lazy()
.group_by([col("variety")])
.agg([col("sepal.width").min()])
.agg([col("sepal_width").min()])
.collect()
.unwrap();

Expand All @@ -134,8 +134,8 @@ fn test_lazy_pushdown_through_agg() {
.lazy()
.group_by([col("variety")])
.agg([
col("sepal.length").min(),
col("petal.length").min().alias("foo"),
col("sepal_length").min(),
col("petal_length").min().alias("foo"),
])
.select([col("foo")])
// second selection is to test if optimizer can handle that
Expand All @@ -153,7 +153,7 @@ fn test_lazy_shift() {
let df = get_df();
let new = df
.lazy()
.select([col("sepal.width").alias("foo").shift(lit(2))])
.select([col("sepal_width").alias("foo").shift(lit(2))])
.collect()
.unwrap();
assert_eq!(new.column("foo").unwrap().f64().unwrap().get(0), None);
Expand Down Expand Up @@ -205,18 +205,18 @@ fn test_lazy_ternary_and_predicates() {
let ldf = df
.lazy()
.with_column(
when(col("sepal.length").lt(lit(5.0)))
when(col("sepal_length").lt(lit(5.0)))
.then(
lit(3), // is another type on purpose to check type coercion
)
.otherwise(col("sepal.width"))
.otherwise(col("sepal_width"))
.alias("foo"),
)
.filter(col("foo").gt(lit(3.0)));

let new = ldf.collect().unwrap();
let length = new.column("sepal.length").unwrap();
assert_eq!(length, &Series::new("sepal.length", &[5.1f64, 5.0, 5.4]));
let length = new.column("sepal_length").unwrap();
assert_eq!(length, &Series::new("sepal_length", &[5.1f64, 5.0, 5.4]));
assert_eq!(new.shape(), (3, 6));
}

Expand Down Expand Up @@ -550,7 +550,7 @@ fn test_simplify_expr() {

let plan = df
.lazy()
.select(&[lit(1.0f32) + lit(1.0f32) + col("sepal.width")])
.select(&[lit(1.0f32) + lit(1.0f32) + col("sepal_width")])
.logical_plan;

let mut expr_arena = Arena::new();
Expand Down
4 changes: 2 additions & 2 deletions crates/polars/src/docs/eager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,10 @@
//!
//! # fn example(df: &DataFrame) -> PolarsResult<()> {
//! // create a mask to filter out null values
//! let mask = df.column("sepal.width")?.is_not_null();
//! let mask = df.column("sepal_width")?.is_not_null();
//!
//! // select column
//! let s = df.column("sepal.length")?;
//! let s = df.column("sepal_length")?;
//!
//! // apply filter on a Series
//! let filtered_series = s.filter(&mask);
Expand Down
12 changes: 6 additions & 6 deletions crates/polars/tests/it/io/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fn test_read_csv_file() {
#[test]
fn test_parser() -> PolarsResult<()> {
let s = r#"
"sepal.length","sepal.width","petal.length","petal.width","variety"
"sepal_length","sepal_width","petal_length","petal_width","variety"
5.1,3.5,1.4,.2,"Setosa"
4.9,3,1.4,.2,"Setosa"
4.7,3.2,1.3,.2,"Setosa"
Expand All @@ -157,7 +157,7 @@ fn test_parser() -> PolarsResult<()> {
.unwrap();

let s = r#"
"sepal.length","sepal.width","petal.length","petal.width","variety"
"sepal_length","sepal_width","petal_length","petal_width","variety"
5.1,3.5,1.4,.2,"Setosa"
5.1,3.5,1.4,.2,"Setosa"
"#;
Expand All @@ -173,7 +173,7 @@ fn test_parser() -> PolarsResult<()> {
.finish()
.unwrap();

let s = r#""sepal.length","sepal.width","petal.length","petal.width","variety"
let s = r#""sepal_length","sepal_width","petal_length","petal_width","variety"
5.1,3.5,1.4,.2,"Setosa"
4.9,3,1.4,.2,"Setosa"
4.7,3.2,1.3,.2,"Setosa"
Expand All @@ -194,8 +194,8 @@ fn test_parser() -> PolarsResult<()> {
assert_eq!(col.get(0)?, AnyValue::String("Setosa"));
assert_eq!(col.get(2)?, AnyValue::String("Setosa"));

assert_eq!("sepal.length", df.get_columns()[0].name());
assert_eq!(1, df.column("sepal.length").unwrap().chunks().len());
assert_eq!("sepal_length", df.get_columns()[0].name());
assert_eq!(1, df.column("sepal_length").unwrap().chunks().len());
assert_eq!(df.height(), 7);

// test windows line endings
Expand Down Expand Up @@ -427,7 +427,7 @@ id090,id048,id0000067778,24,2,51862,4,9,

#[test]
fn test_new_line_escape() {
let s = r#""sepal.length","sepal.width","petal.length","petal.width","variety"
let s = r#""sepal_length","sepal_width","petal_length","petal_width","variety"
5.1,3.5,1.4,.2,"Setosa
texts after new line character"
4.9,3,1.4,.2,"Setosa"
Expand Down

0 comments on commit e39ba62

Please sign in to comment.