Skip to content

Commit

Permalink
Fix Rust examples.
Browse files Browse the repository at this point in the history
Translate all the Python examples to Rust.
  • Loading branch information
rodrigogiraoserrao committed Oct 1, 2024
1 parent 6343606 commit 9caf128
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 102 deletions.
17 changes: 16 additions & 1 deletion docs/source/_build/API_REFERENCE_LINKS.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ python:
Expr.list:
name: "list namespace"
link: https://docs.pola.rs/api/python/stable/reference/expressions/list.html
Expr.str:
name: "str namespace"
link: https://docs.pola.rs/api/python/stable/reference/expressions/string.html
element: https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.element.html
all: https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.all.html
exclude: https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.exclude.html
Expand Down Expand Up @@ -156,6 +159,7 @@ python:
Expr.name:
name: "name namespace"
link: https://docs.pola.rs/api/python/stable/reference/expressions/name.html
round: https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.Expr.round.html#polars.Expr.round

rust:
DataFrame: https://docs.pola.rs/api/rust/dev/polars/frame/struct.DataFrame.html
Expand Down Expand Up @@ -293,7 +297,10 @@ rust:
n_unique: https://docs.pola.rs/api/rust/dev/polars_lazy/dsl/enum.Expr.html#method.n_unique
null_count: https://docs.pola.rs/api/rust/dev/polars_lazy/dsl/enum.Expr.html#method.null_count
interpolate: https://docs.pola.rs/api/rust/dev/polars_lazy/dsl/enum.Expr.html#method.interpolate
is_between: https://github.com/pola-rs/polars/issues/11285
is_between:
name: is_between
link: https://docs.pola.rs/api/rust/dev/polars/prelude/enum.Expr.html#method.is_between
feature_flags: [is_between]
is_duplicated: https://docs.pola.rs/api/rust/dev/polars_lazy/dsl/enum.Expr.html#method.is_duplicated
is_null: https://docs.pola.rs/api/rust/dev/polars/prelude/enum.Expr.html#method.is_null
value_counts:
Expand All @@ -304,6 +311,10 @@ rust:
Expr.list:
name: "list namespace"
link: https://docs.pola.rs/api/rust/dev/polars_lazy/dsl/struct.ListNameSpace.html
Expr.str:
name: "str namespace"
link: https://docs.pola.rs/api/rust/dev/polars/prelude/trait.StringNameSpaceImpl.html
feature_flags: [strings]
Series.arr: https://docs.pola.rs/api/rust/dev/polars_lazy/dsl/struct.ArrayNameSpace.html

date_range:
Expand Down Expand Up @@ -395,3 +406,7 @@ rust:
name: "dt namespace"
link: https://docs.pola.rs/api/rust/dev/polars_lazy/dsl/dt/struct.DateLikeNameSpace.html
feature_flags: [temporal]
round:
name: "round"
link: https://docs.pola.rs/api/rust/dev/polars/prelude/enum.Expr.html#method.round
feature_flags: [round_series]
16 changes: 8 additions & 8 deletions docs/source/src/python/user-guide/getting-started/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# --8<-- [start:select]
result = df.select(
pl.col("name"),
pl.col("birthdate").dt.year().alias("birth year"),
pl.col("birthdate").dt.year().alias("birth_year"),
(pl.col("weight") / (pl.col("height") ** 2)).alias("bmi"),
)
print(result)
Expand All @@ -37,15 +37,15 @@
# --8<-- [start:expression-expansion]
result = df.select(
pl.col("name"),
(pl.col("weight", "height") * 0.95).round(2).name.suffix(" - 5%"),
(pl.col("weight", "height") * 0.95).round(2).name.suffix("-5%"),
)
print(result)
# --8<-- [end:expression-expansion]

# --8<-- [start:with_columns]
result = df.with_columns(
pl.col("birthdate").dt.year().alias("birth year"),
(pl.col("weight") / (pl.col("height") ** 2)).alias("bmi"),
birth_year=pl.col("birthdate").dt.year(),
bmi=pl.col("weight") / (pl.col("height") ** 2),
)
print(result)
# --8<-- [end:with_columns]
Expand Down Expand Up @@ -76,8 +76,8 @@
(pl.col("birthdate").dt.year() // 10 * 10).alias("decade"),
maintain_order=True,
).agg(
pl.len().alias("sample size"),
pl.col("weight").mean().round(2).alias("avg weight"),
pl.len().alias("sample_size"),
pl.col("weight").mean().round(2).alias("avg_weight"),
pl.col("height").max().alias("tallest"),
)
print(result)
Expand All @@ -98,7 +98,7 @@
)
.agg(
pl.col("name"),
pl.col("weight", "height").mean().round(2).name.prefix("avg "),
pl.col("weight", "height").mean().round(2).name.prefix("avg_"),
)
)
print(result)
Expand Down Expand Up @@ -131,5 +131,5 @@
}
)

print(df.vstack(df3))
print(pl.concat([df, df3], how="vertical"))
# --8<-- [end:concat]
3 changes: 2 additions & 1 deletion docs/source/src/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ required-features = ["polars/lazy", "polars/csv"]
[[bin]]
name = "user-guide-getting-started-expressions"
path = "user-guide/getting-started/expressions.rs"
required-features = ["polars/lazy"]
required-features = ["polars/lazy", "polars/temporal", "polars/round_series", "polars/strings"]
[[bin]]
name = "user-guide-getting-started-joins"
path = "user-guide/getting-started/joins.rs"
required-features = ["polars/polars-ops"]
[[bin]]
name = "user-guide-getting-started-reading-writing"
path = "user-guide/getting-started/reading-writing.rs"
Expand Down
Loading

0 comments on commit 9caf128

Please sign in to comment.