Skip to content

Commit 3099bec

Browse files
authored
Better error messages for totally empty columns (#248)
* nicer error message for empty columns * tests * bump patch
1 parent dee41c2 commit 3099bec

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "StatsModels"
22
uuid = "3eaba693-59b7-5ba5-a881-562e759f1c8d"
3-
version = "0.6.27"
3+
version = "0.6.28"
44

55
[deps]
66
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"

src/errormessages.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ function checkcol(table, name::Symbol)
2323
nearestnames = join(fuzzymatch(names, name),", " )
2424
return "There isn't a variable called '$name' in your data; the nearest names appear to be: $nearestnames"
2525
end
26+
27+
nrows = length(Tables.getcolumn(table, name))
28+
if nrows == 0
29+
return "Column '$name' is empty."
30+
end
31+
2632
return ""
2733
end
2834

@@ -41,4 +47,4 @@ function checknamesexist(f::FormulaTerm, t)
4147
end
4248
end
4349
return ""
44-
end
50+
end

src/schema.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ schema(ts::AbstractVector{<:AbstractTerm}, data, hints::Dict{Symbol}) =
120120

121121
# handle hints:
122122
schema(ts::AbstractVector{<:AbstractTerm}, dt::ColumnTable,
123-
hints::Dict{Symbol}=Dict{Symbol,Any}()) =
123+
hints::Dict{Symbol}=Dict{Symbol,Any}()) =
124124
sch = Schema(t=>concrete_term(t, dt, hints) for t in ts)
125125

126126
schema(f::TermOrTerms, data, hints::Dict{Symbol}) =
@@ -169,15 +169,15 @@ a(continuous)
169169
concrete_term(t::Term, d, hints::Dict{Symbol}) = concrete_term(t, d, get(hints, t.sym, nothing))
170170

171171
function concrete_term(t::Term, dt::ColumnTable, hint)
172-
msg::String = checkcol( dt, t.sym )
172+
msg = checkcol(dt, t.sym)
173173
if msg != ""
174174
throw(ArgumentError(msg))
175175
end
176176
return concrete_term(t, getproperty(dt, t.sym), hint)
177177
end
178178

179179
function concrete_term(t::Term, dt::ColumnTable, hints::Dict{Symbol})
180-
msg::String = checkcol( dt, t.sym )
180+
msg = checkcol(dt, t.sym)
181181
if msg != ""
182182
throw(ArgumentError(msg))
183183
end

test/schema.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,15 @@
7070

7171
end
7272

73+
@testset "nice errors" begin
74+
d = (yyy = rand(10), aaa = rand(10), bbb = repeat([:a, :b], 5))
75+
@test_throws ArgumentError("There isn't a variable called 'aa' in your data; the nearest names appear to be: aaa") concrete_term(Term(:aa), d, nothing)
76+
@test_throws ArgumentError("There isn't a variable called 'aab' in your data; the nearest names appear to be: aaa, bbb") concrete_term(Term(:aab), d, nothing)
77+
78+
@test_throws ArgumentError("Column 'a' is empty.") concrete_term(Term(:a), (; a=[]), nothing)
79+
@test_throws ArgumentError("Column 'a' is empty.") schema((; b=[1,2], a=[]))
80+
81+
end
82+
83+
7384
end

0 commit comments

Comments
 (0)