Skip to content

Commit

Permalink
Add native expression for Series.split/2 (#875)
Browse files Browse the repository at this point in the history
  • Loading branch information
H12 committed Mar 4, 2024
1 parent 95340e8 commit acf7455
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions native/explorer/src/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,12 @@ pub fn expr_substring(expr: ExExpr, offset: i64, length: Option<u64>) -> ExExpr
ExExpr::new(expr.str().slice(offset.lit(), length))
}

#[rustler::nif]
pub fn expr_split(expr: ExExpr, substring: String) -> ExExpr {
let expr = expr.clone_inner();
ExExpr::new(expr.str().split(substring.lit()))
}

#[rustler::nif]
pub fn expr_replace(expr: ExExpr, pat: String, value: String) -> ExExpr {
let expr = expr.clone_inner();
Expand Down
1 change: 1 addition & 0 deletions native/explorer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ rustler::init!(
expr_lstrip,
expr_rstrip,
expr_substring,
expr_split,
expr_replace,
expr_json_path_match,
// float round expressions
Expand Down
11 changes: 11 additions & 0 deletions test/explorer/data_frame_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1733,6 +1733,17 @@ defmodule Explorer.DataFrameTest do
}
end

test "split a string by a substring" do
df = DF.new(a: ["foo,bar", "bar,baz"])

df1 = DF.mutate(df, b: split(a, ","))

assert DF.to_columns(df1, atom_keys: true) == %{
a: ["foo,bar", "bar,baz"],
b: [["foo", "bar"], ["bar", "baz"]]
}
end

test "replace characters in a string" do
df = DF.new(a: ["2,000", "2,000,000", ","])

Expand Down

0 comments on commit acf7455

Please sign in to comment.