From 38cd081411eaf4b396e74f7fb4debc13f851616c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Ekl=C3=B6f?= <184952+gustafsson@users.noreply.github.com> Date: Sat, 18 Mar 2023 10:37:35 +0100 Subject: [PATCH 1/5] deleteat! where drop is a column --- src/dataframe/dataframe.jl | 5 +++++ test/dataframe.jl | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/src/dataframe/dataframe.jl b/src/dataframe/dataframe.jl index 5e59a2d068..9986c8442a 100755 --- a/src/dataframe/dataframe.jl +++ b/src/dataframe/dataframe.jl @@ -904,6 +904,11 @@ function _deleteat!_helper(df::DataFrame, drop) return df end + selfdrop = findfirst(c -> c === drop, cols) + if !isnothing(selfdrop) && selfdrop < length(cols) + drop = copy(drop) + end + n = nrow(df) col1 = cols[1] deleteat!(col1, drop) diff --git a/test/dataframe.jl b/test/dataframe.jl index febfe4cd35..128ad53591 100644 --- a/test/dataframe.jl +++ b/test/dataframe.jl @@ -518,6 +518,13 @@ end df = DataFrame(a=1, b=3.0) @test isempty(deleteat!(df, true:true)) + df = DataFrame(a=[false, true, true], b=1:3, c=4:6) + @test deleteat!(df, df.a) == DataFrame(a=false, b=1, c=4) + df = DataFrame(a=1:3, b=[false, true, true], c=4:6) + @test deleteat!(df, df.b) == DataFrame(a=1, b=false, c=4) + df = DataFrame(a=1:3, b=4:6, c=[false, true, true]) + @test deleteat!(df, df.c) == DataFrame(a=1, b=4, c=false) + Random.seed!(1234) for t in 0:0.005:1.0 # two columns are needed as the second column is affected From dd5a17fe9ea6863204bda6fa58d41a386ddd444c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Wed, 3 May 2023 20:09:48 +0200 Subject: [PATCH 2/5] Update dataframe.jl --- src/dataframe/dataframe.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dataframe/dataframe.jl b/src/dataframe/dataframe.jl index 9986c8442a..fabac13c1d 100755 --- a/src/dataframe/dataframe.jl +++ b/src/dataframe/dataframe.jl @@ -904,7 +904,7 @@ function _deleteat!_helper(df::DataFrame, drop) return df end - selfdrop = findfirst(c -> c === drop, cols) + selfdrop = findfirst(c -> c === drop || Base.mightalias(c, drop), cols) if !isnothing(selfdrop) && selfdrop < length(cols) drop = copy(drop) end From 69791dbb49075372008c6c95d9e0ac838b3673fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Wed, 3 May 2023 20:18:41 +0200 Subject: [PATCH 3/5] Update NEWS.md --- NEWS.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/NEWS.md b/NEWS.md index f41fb8f307..953c54894b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -14,6 +14,12 @@ when a vector of duplicate indices is passed when doing column selection ([#3302](https://github.com/JuliaData/DataFrames.jl/pull/3302)) +## Bug fixes + +* `deleteat!` correctly handles the situation when vector of rows to be dropped + from a data frame is its column or might alias with some of its columns + ([#3304](https://github.com/JuliaData/DataFrames.jl/pull/3304)) + # DataFrames.jl v1.5 Release Notes ## New functionalities From d43280bfe8411eabfe92e297b39f09d01c079ae4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Fri, 12 May 2023 14:07:07 +0200 Subject: [PATCH 4/5] Update src/dataframe/dataframe.jl --- src/dataframe/dataframe.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dataframe/dataframe.jl b/src/dataframe/dataframe.jl index fabac13c1d..7361a61904 100755 --- a/src/dataframe/dataframe.jl +++ b/src/dataframe/dataframe.jl @@ -905,7 +905,7 @@ function _deleteat!_helper(df::DataFrame, drop) end selfdrop = findfirst(c -> c === drop || Base.mightalias(c, drop), cols) - if !isnothing(selfdrop) && selfdrop < length(cols) + if !isnothing(selfdrop) drop = copy(drop) end From 900718a32dc4d69335b5c6f0456d595c1a63f7cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Fri, 12 May 2023 14:08:19 +0200 Subject: [PATCH 5/5] Update src/dataframe/dataframe.jl --- src/dataframe/dataframe.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/dataframe/dataframe.jl b/src/dataframe/dataframe.jl index 7361a61904..0c63fb14de 100755 --- a/src/dataframe/dataframe.jl +++ b/src/dataframe/dataframe.jl @@ -904,8 +904,7 @@ function _deleteat!_helper(df::DataFrame, drop) return df end - selfdrop = findfirst(c -> c === drop || Base.mightalias(c, drop), cols) - if !isnothing(selfdrop) + if any(c -> c === drop || Base.mightalias(c, drop), cols) drop = copy(drop) end