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

deleteat! where drop is a column #3304

Merged
merged 6 commits into from
May 12, 2023
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
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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