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 diff --git a/src/dataframe/dataframe.jl b/src/dataframe/dataframe.jl index 5e59a2d068..0c63fb14de 100755 --- a/src/dataframe/dataframe.jl +++ b/src/dataframe/dataframe.jl @@ -904,6 +904,10 @@ function _deleteat!_helper(df::DataFrame, drop) return df end + if any(c -> c === drop || Base.mightalias(c, drop), 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 d9cce76828..0937e214a2 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