# Empty result when comparing incompatible types

**URL:** <https://discourse.julialang.org/t/empty-result-when-comparing-incompatible-types/54102>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 28, 2021, 8:08am UTC](https://discourse.julialang.org/t/empty-result-when-comparing-incompatible-types/54102 "2021-01-28T08:08:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [January 28, 2021, 8:08am UTC](https://discourse.julialang.org/t/empty-result-when-comparing-incompatible-types/54102/1 "2021-01-28T08:08:46Z")

</div>

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

For example with Base:

```julia
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:

```julia
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?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [January 28, 2021, 8:21am UTC](https://discourse.julialang.org/t/empty-result-when-comparing-incompatible-types/54102/2 "2021-01-28T08:21:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [January 28, 2021, 1:23pm UTC](https://discourse.julialang.org/t/empty-result-when-comparing-incompatible-types/54102/3 "2021-01-28T13:23:48Z")

</div>

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

```julia
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
[...]

```
