Empty result when comparing incompatible types

Often I am filtering a vector or joining dataframes and I use the wrong type and get an empty result.

For example with Base:

rows = [
 (month = 01, x = 15.4),
 (month = 02, x = 38.3),
]
filter(x->x.month == "January", rows)
# NamedTuple{(:month, :x),Tuple{Int64,Float64}}[]

and with DataFrames:

a = DataFrame(month=["January", "February"], x=[15.4, 38.3])
b = DataFrame(month=[01, 02], y=[100, 200])
DataFrames.innerjoin(a, b, on=:month)
# 0×3 DataFrame

This can be confusing and I would like to get an error when comparing for equality across incompatible types. How can I avoid this problem?

You could define your own equality operator that only calls == for the same types, and would throw a method error otherwise. But that could get restrictive very fast, like 1 == 1.0 wouldn’t work anymore etc.

1 Like

Perhaps check that one field can be converted into the other before comparing:

julia> compare_field(dfValue, tgValue) = isequal(convert(typeof(tgValue), dfValue), tgValue)
compare_field (generic function with 1 method)

julia> compare_field(UInt8(1), 1)
true

julia> compare_field(1, "January")
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type String
[...]
2 Likes