# Enforcing Schema on Data Frame Passed as Function Argument

**URL:** <https://discourse.julialang.org/t/enforcing-schema-on-data-frame-passed-as-function-argument/49020>\
**Category:** General Usage\
**Tags:** question, dataframes, function\
**Created:** [October 26, 2020, 2:21am UTC](https://discourse.julialang.org/t/enforcing-schema-on-data-frame-passed-as-function-argument/49020 "2020-10-26T02:21:18Z")\
**Posts on this page:** 1\
**Showing post:** 5

<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:** [October 26, 2020, 8:17am UTC](https://discourse.julialang.org/t/enforcing-schema-on-data-frame-passed-as-function-argument/49020/5 "2020-10-26T08:17:39Z")

</div>

I had the same issue:

> [@Dispatch on DataFrame columns](https://discourse.julialang.org/t/dispatch-on-dataframe-columns/40689):
>
> 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: 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 …

My solution was to define a Dict with the required column names and types (abstract supertypes, where applicable) and a function to check if they are in the DataFrame (there may be more columns in the DataFrame, which is fine for me):

```julia

const INPUT_ELTYPES = Dict(
    :field_a=> AbstractString,
    :field_b => ItemTypes,
    :field_c=> AbstractString,
    :field_x=> Union{AbstractString, Nothing},
    :field_y=> Real,
    :value => Union{Real, Missing},
)

function check_input_data(df:: AbstractDataFrame)
    @assert COLUMN_NAMES ⊆ names(df)
    for (col_name, col_type) in INPUT_ELTYPES
        @assert eltype(df[!, col_name]) <: col_type
    end
end

```

Maybe something like this could be added to DataFrames.jl, or a typed data frame as alternative type. The latter could be just a thin wrapper over the standard DataFrame type (with untyped columns) to avoid recompilations, with the sole purpose of defining schemas.

---

_[View the full topic](https://discourse.julialang.org/t/enforcing-schema-on-data-frame-passed-as-function-argument/49020)._
