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

better disallowmissing error message #2966

Merged
merged 5 commits into from
Dec 18, 2021
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
24 changes: 18 additions & 6 deletions src/abstractdataframe/abstractdataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ Get a data frame with the `n` first rows of `df`.
If `view=false` a freshly allocated `DataFrame` is returned.
If `view=true` then a `SubDataFrame` view into `df` is returned.
"""
@inline Base.first(df::AbstractDataFrame, n::Integer; view::Bool=false) =
@inline Base.first(df::AbstractDataFrame, n::Integer; view::Bool=false) =
view ? Base.view(df, 1:min(n ,nrow(df)), :) : df[1:min(n, nrow(df)), :]

"""
Expand All @@ -511,7 +511,7 @@ Get a data frame with the `n` last rows of `df`.
If `view=false` a freshly allocated `DataFrame` is returned.
If `view=true` then a `SubDataFrame` view into `df` is returned.
"""
@inline Base.last(df::AbstractDataFrame, n::Integer; view::Bool=false) =
@inline Base.last(df::AbstractDataFrame, n::Integer; view::Bool=false) =
view ? Base.view(df, max(1, nrow(df)-n+1):nrow(df), :) : df[max(1, nrow(df)-n+1):nrow(df), :]


Expand Down Expand Up @@ -2060,10 +2060,22 @@ function Missings.disallowmissing(df::AbstractDataFrame,
for i in axes(df, 2)
x = df[!, i]
if i in idxcols
if !error && Missing <: eltype(x) && any(ismissing, x)
y = x
else
y = disallowmissing(x)
y = x
if Missing <: eltype(x)
try
y = disallowmissing(x)
catch e
row = findfirst(ismissing, x)
if row !== nothing
if error
col = _names(df)[i]
throw(ArgumentError("Missing value found in column " *
":$col in row $row"))
end
else
rethrow(e)
end
end
end
push!(newcols, y === x ? copy(y) : y)
else
Expand Down
17 changes: 15 additions & 2 deletions src/dataframe/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1190,8 +1190,21 @@ function disallowmissing! end

function disallowmissing!(df::DataFrame, col::ColumnIndex; error::Bool=true)
x = df[!, col]
if !(!error && Missing <: eltype(x) && any(ismissing, x))
df[!, col] = disallowmissing(x)
if Missing <: eltype(x)
try
df[!, col] = disallowmissing(x)
catch e
row = findfirst(ismissing, x)
if row !== nothing
if error
col_name = only(names(df, col))
throw(ArgumentError("Missing value found in column " *
":$col_name in row $row"))
end
else
rethrow(e)
end
end
end
return df
end
Expand Down
10 changes: 5 additions & 5 deletions test/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ end
@test isa(df[!, 1], Vector{Union{Int, Missing}})
@test !isa(df[!, 2], Vector{Union{Int, Missing}})
df[1, 1] = missing
@test_throws MethodError disallowmissing!(df, 1)
@test_throws ArgumentError disallowmissing!(df, 1)
tmpcol = df[!, 1]
disallowmissing!(df, 1, error=false)
@test df[!, 1] === tmpcol
Expand All @@ -1145,7 +1145,7 @@ end
@test isa(df[!, 1], Vector{Union{Int, Missing}})
@test !isa(df[!, 2], Vector{Union{Int, Missing}})
df[1, 1] = missing
@test_throws MethodError disallowmissing!(df, Not(Not(1)))
@test_throws ArgumentError disallowmissing!(df, Not(Not(1)))
tmpcol = df[!, 1]
disallowmissing!(df, Not(Not(1)), error=false)
@test df[!, 1] === tmpcol
Expand Down Expand Up @@ -1206,7 +1206,7 @@ end
@test eltype(df[!, 1]) <: Union{CategoricalValue{Int}, Missing}
@test eltype(df[!, 2]) <: Union{CategoricalValue{String}, Missing}
df[1, 2] = missing
@test_throws MissingException disallowmissing!(df)
@test_throws ArgumentError disallowmissing!(df)
tmpcol =df[!, 2]
disallowmissing!(df, error=false)
@test df[!, 2] === tmpcol
Expand Down Expand Up @@ -1331,9 +1331,9 @@ end
end
end

@test_throws MethodError disallowmissing(DataFrame(x=[missing]))
@test_throws ArgumentError disallowmissing(DataFrame(x=[missing]))
@test disallowmissing(DataFrame(x=[missing]), error=false) ≅ DataFrame(x=[missing])
@test_throws MethodError disallowmissing(DataFrame(x=[1, missing]))
@test_throws ArgumentError disallowmissing(DataFrame(x=[1, missing]))
@test disallowmissing(DataFrame(x=[1, missing]), error=false) ≅ DataFrame(x=[1, missing])

df = DataFrame(x=[1], y=Union{Int, Missing}[1], z=[missing])
Expand Down