Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check duplicate keys dataframe rename column #850

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/explorer/data_frame.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3610,6 +3610,10 @@ defmodule Explorer.DataFrame do

pairs ->
pairs_map = Map.new(pairs)

if Enum.count(pairs) != map_size(pairs_map),
do: raise(ArgumentError, "duplicate source column for rename")

old_dtypes = df.dtypes

for {name, _} <- pairs do
Expand Down
2 changes: 1 addition & 1 deletion native/explorer/src/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ pub fn df_rename_columns(
) -> Result<ExDataFrame, ExplorerError> {
let mut df = df.clone();
for (original, new_name) in renames {
df.rename(original, new_name).expect("should rename");
df.rename(original, new_name)?;
}

Ok(ExDataFrame::new(df))
Expand Down
32 changes: 32 additions & 0 deletions test/explorer/data_frame_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2763,6 +2763,38 @@ defmodule Explorer.DataFrameTest do
end
end

test "with keyword and a column that is duplicated" do
df = DF.new(a: [1, 2, 3], b: ["a", "b", "c"])

assert_raise ArgumentError, ~r"duplicate source column for rename", fn ->
DF.rename(df, a: "first", a: "second")
end
end

test "with mix of column name and index that is duplicated" do
df = DF.new(a: [1, 2, 3], b: ["a", "b", "c"])

assert_raise ArgumentError, ~r"duplicate source column for rename", fn ->
DF.rename(df, [{"a", "first"}, {0, "g"}])
end
end

test "with string column names that are duplicated" do
df = DF.new(a: [1, 2, 3], b: ["a", "b", "c"])

assert_raise ArgumentError, ~r"duplicate source column for rename", fn ->
DF.rename(df, [{"a", "first"}, {"a", "second"}])
end
end

test "with string column names and a target that is duplicated" do
df = DF.new(a: [1, 2, 3], b: ["a", "b", "c"])

assert_raise RuntimeError, ~r"duplicate column names found", fn ->
DF.rename(df, [{"a", "first"}, {"b", "first"}])
end
end

test "with a map and a column that doesn't exist" do
df = DF.new(a: [1, 2, 3], b: ["a", "b", "c"])

Expand Down
Loading