# Dispatch on DataFrame columns

**URL:** https://discourse.julialang.org/t/dispatch-on-dataframe-columns/40689
**Category:** Data
**Created:** [June 3, 2020, 6:32pm UTC](https://discourse.julialang.org/t/dispatch-on-dataframe-columns/40689 "2020-06-03T18:32:15Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)
#### Post date: [June 3, 2020, 6:32pm UTC](https://discourse.julialang.org/t/dispatch-on-dataframe-columns/40689/1 "2020-06-03T18:32:16Z")

</div>

Hi,

I have a function on a DataFrame which should behave differently depending on the names / eltypes of the DataFrame columns. Is this possible using a multiple-dispatch like pattern?

Example:

```julia
using DataFrames
function myfunc(df:: AbstractDataFrame) # call this method if columns a + b exist and are numeric
    df[!, :c] = df.a .* df.b
    return df
end
function myfunc(df:: AbstractDataFrame) # call this method if columns a + c exist and are numeric
    df[!, :b] = df.c ./ df.a
    return df
end

foo = DataFrame(a=1:4, b=2:5)
bar = DataFrame(a=1:4, c=3:6)
myfunc(foo) # should call 1st definition
myfunc(bar) # should call 2nd definition

```

It would be possible to check the column names and eltypes inside the function, but I wonder if there is a more elegant / “Julian” way of doing this.

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [June 3, 2020, 6:36pm UTC](https://discourse.julialang.org/t/dispatch-on-dataframe-columns/40689/2 "2020-06-03T18:36:55Z")

</div>

No, this is not possible with dispatch. Unlike `NamedTuples`, for example, the names of columns in a DataFrame are not stored in the type information of the data frame.

You will need to use `if...else` constructs to solve this problem.

---

<div class="post-metadata">

### Author: ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)
#### Post date: [June 3, 2020, 10:12pm UTC](https://discourse.julialang.org/t/dispatch-on-dataframe-columns/40689/3 "2020-06-03T22:12:08Z")

</div>

Yeah, DataFrames specifically _doesn’t_ encode column name/type information as type parameters, due to the desire to avoid having to recompile lots of methods.

For “typed” tables, take a look at [IndexedTables.jl](https://github.com/JuliaData/IndexedTables.jl), [TypedTables.jl](https://github.com/JuliaData/TypedTables.jl/), or just using a “[columntable](https://juliadata.github.io/Tables.jl/stable/#Tables.columntable)” (a NamedTuple of vectors).

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [June 4, 2020, 2:17am UTC](https://discourse.julialang.org/t/dispatch-on-dataframe-columns/40689/4 "2020-06-04T02:17:32Z")

</div>

If statement should suffice. It’s gonna be fast.

---

<div class="post-metadata">

### Author: ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)
#### Post date: [June 4, 2020, 1:45pm UTC](https://discourse.julialang.org/t/dispatch-on-dataframe-columns/40689/5 "2020-06-04T13:45:00Z")

</div>

Maybe like this?

```julia
getcolumn(df::AbstractDataFrame, name, default) = name in names(df) ? df[!,name] : default
function myfunc(df::AbstractDataFrame)
    a,b,c = (getcolumn(df,name,nothing) for name in ("a", "b", "c"))
    _myfunc(df,a,b,c)
end
function _myfunc(df::AbstractDataFrame, a::AbstractVector{<:Number}, b::AbstractVector{<:Number}, c::Nothing)
    df[!, :c] = a .* b
    return df
end
# etc.

```

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [June 4, 2020, 1:53pm UTC](https://discourse.julialang.org/t/dispatch-on-dataframe-columns/40689/6 "2020-06-04T13:53:36Z")

</div>

This is probably a scenario where you don’t need dispatch. And because `myfunc` can’t determint the types of `a, b, c`, you won’t get a performance benefit from this structure compared to more readable `if...else` code.

---

<div class="post-metadata">

### Author: ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)
#### Post date: [June 4, 2020, 5:26pm UTC](https://discourse.julialang.org/t/dispatch-on-dataframe-columns/40689/7 "2020-06-04T17:26:23Z")

</div>

Thanks for your responses!  
I will stick to the “classical” if - else pattern. Just wanted to make sure that I don’t miss something.
