Skip to content

Commit

Permalink
deleteat! where drop is a column (JuliaData#3304)
Browse files Browse the repository at this point in the history
  • Loading branch information
gustafsson committed May 12, 2023
1 parent 3aa0067 commit 153c038
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
* `describe` now has `:sum` available as a descriptive statistic.
([#3303](https://github.com/JuliaData/DataFrames.jl/pull/3303))

## 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
Expand Down
4 changes: 4 additions & 0 deletions src/dataframe/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions test/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 153c038

Please sign in to comment.