Skip to content

Commit 24bc695

Browse files
authored
code cleanup to improve error messages (#2868)
1 parent df1b417 commit 24bc695

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/abstractdataframe/abstractdataframe.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,7 @@ julia> filter!(AsTable(:) => nt -> nt.x == 1 || nt.y == "b", df)
11731173
3 │ 1 b
11741174
```
11751175
"""
1176-
Base.filter!(f, df::AbstractDataFrame) = delete!(df, findall(!f, eachrow(df)))
1176+
Base.filter!(f::Function, df::AbstractDataFrame) = delete!(df, findall(!f, eachrow(df)))
11771177
Base.filter!((col, f)::Pair{<:ColumnIndex}, df::AbstractDataFrame) =
11781178
_filter!_helper(df, f, df[!, col])
11791179
Base.filter!((cols, f)::Pair{<:AbstractVector{Symbol}}, df::AbstractDataFrame) =

src/other/index.jl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,13 @@ function rename!(x::Index, nms::AbstractVector{Pair{Symbol, Symbol}})
8484
if !haskey(xbackup, from)
8585
copy!(x.lookup, xbackup.lookup)
8686
x.names .= xbackup.names
87-
throw(ArgumentError("Tried renaming :$from to :$to, when :$from " *
88-
"does not exist in the Index."))
87+
if length(x) == 0
88+
throw(ArgumentError("Tried renaming :$from to :$to, when " *
89+
"data frame has no columns."))
90+
else
91+
throw(ArgumentError("Tried renaming :$from to :$to, when :$from " *
92+
"does not exist in the data frame."))
93+
end
8994
end
9095
if haskey(x, to)
9196
toholder[to] = x.lookup[to]
@@ -98,7 +103,7 @@ function rename!(x::Index, nms::AbstractVector{Pair{Symbol, Symbol}})
98103
copy!(x.lookup, xbackup.lookup)
99104
x.names .= xbackup.names
100105
throw(ArgumentError("Tried renaming to :$(first(keys(toholder))), " *
101-
"when it already exists in the Index."))
106+
"when it already exists in the data frame."))
102107
end
103108
return x
104109
end
@@ -115,7 +120,7 @@ Base.haskey(x::Index, key::Bool) =
115120
throw(ArgumentError("invalid key: $key of type Bool"))
116121

117122
function Base.push!(x::Index, nm::Symbol)
118-
haskey(x.lookup, nm) && throw(ArgumentError(":$nm already exists in Index"))
123+
haskey(x.lookup, nm) && throw(ArgumentError(":$nm already exists in the data frame"))
119124
x.lookup[nm] = length(x) + 1
120125
push!(x.names, nm)
121126
return x
@@ -288,6 +293,10 @@ end
288293
if i === nothing
289294
candidates = fuzzymatch(l, idx)
290295
if isempty(candidates)
296+
if isempty(l)
297+
throw(ArgumentError("column name :$idx not found in the " *
298+
"data frame since it has no columns"))
299+
end
291300
throw(ArgumentError("column name :$idx not found in the data frame"))
292301
end
293302
candidatesstr = join(string.(':', candidates), ", ", " and ")

0 commit comments

Comments
 (0)