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 for data frame corruption in delete! #2690

Merged
merged 4 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 20 additions & 2 deletions src/dataframe/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -966,9 +966,18 @@ function Base.delete!(df::DataFrame, inds)
if !isempty(inds) && size(df, 2) == 0
throw(BoundsError(df, (inds, :)))
end

# we require ind to be stored and unique like in Base
# otherwise an error will be thrown and the data frame will get corrupted
foreach(col -> deleteat!(col, inds), _columns(df))
n = nrow(df)
for col in _columns(df)
if length(col) != n
throw(ArgumentError("Unequal lengths of columns detected. Most likely the " *
"same column was stored in the data frame several times.
The source data frame is now corrupted."))
end
deleteat!(col, inds)
end
return df
end

Expand All @@ -977,7 +986,16 @@ function Base.delete!(df::DataFrame, inds::AbstractVector{Bool})
throw(BoundsError(df, (inds, :)))
end
drop = findall(inds)
foreach(col -> deleteat!(col, drop), _columns(df))

n = nrow(df)
for col in _columns(df)
if length(col) != n
throw(ArgumentError("Unequal lengths of columns detected. Most likely the " *
"same column was stored in the data frame several times.
The source data frame is now corrupted."))
end
deleteat!(col, inds)
end
return df
end

Expand Down
6 changes: 6 additions & 0 deletions test/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,12 @@ end
@test delete!(df, v) == DataFrame(x=[2, 3])
@test x == [2, 3]
end

for inds in (1, [1], [true, false])
df = DataFrame(x1=[1, 2])
df.x2 = df.x1
@test_throws ArgumentError delete!(df, inds)
end
end

@testset "describe" begin
Expand Down