# Checking if function input is DataFrame

**URL:** <https://discourse.julialang.org/t/checking-if-function-input-is-dataframe/56707>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [March 7, 2021, 11:54pm UTC](https://discourse.julialang.org/t/checking-if-function-input-is-dataframe/56707 "2021-03-07T23:54:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jo-fleck](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@jo-fleck](https://discourse.julialang.org/u/jo-fleck)\
**Post date:** [March 7, 2021, 11:54pm UTC](https://discourse.julialang.org/t/checking-if-function-input-is-dataframe/56707/1 "2021-03-07T23:54:15Z")

</div>

I wrote a function which inspects a data frame and, if its content meets certain requirements, sends it to a server. To make sure (new) users can easily understand which aspect of the content needs to be changed in case the upload fails, I added an explicit error if the function input is no data frame.

```julia
function my_fun(df_in)

    if typeof(df_in) != DataFrame error("Input must be a data frame") end

...
end

```

However, when running the Juno profiler on the function (I am new to measuring performance - please tell me if this is a terrible approach), I noticed this line is very slow - it accounts for 50% of elapsed time.

I assume using `my_fun(df_in::DataFrame)` would improve performance but then the function would throw a method error as opposed to my specific error message.

Is there a performance friendlier way to check if an object is of type DataFrame?

Many thanks for hints and help!

---

<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:** [March 8, 2021, 12:03am UTC](https://discourse.julialang.org/t/checking-if-function-input-is-dataframe/56707/2 "2021-03-08T00:03:40Z")

</div>

You can do

```julia
function my_fun(df_in::DataFrame)
   # do stuff
end

function my_fun(df_in::Any)
    throw(ArgumentError("Must be a DataFrame"))
end

```

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [March 8, 2021, 12:28am UTC](https://discourse.julialang.org/t/checking-if-function-input-is-dataframe/56707/3 "2021-03-08T00:28:34Z")

</div>

```julia
@assert typeof(df_in) == DataFrame "Input must be a DataFrame"

```

I believe this is what you want.

---

<div class="post-metadata">

**Author:** ![Satvik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/satvik/32/20486_2.png) [@Satvik](https://discourse.julialang.org/u/Satvik)\
**Post date:** [March 8, 2021, 4:20am UTC](https://discourse.julialang.org/t/checking-if-function-input-is-dataframe/56707/4 "2021-03-08T04:20:34Z")

</div>

This seems like an issue with the profiler. When I tried the following:

```julia
function my_fun(df_in)
    if typeof(df_in) != DataFrame a = 1 end
end

@btime my_fun(test_data)
4.583 ns (0 allocations: 0 bytes)

@btime my_fun(5)
0.036 ns (0 allocations: 0 bytes)

```

There’s a few nanoseconds of overhead from the if statement, but that’s it. I couldn’t actually time the error function in the profiler, but from manual testing it was practically instant, so this check should be fast in all cases.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [March 8, 2021, 5:41am UTC](https://discourse.julialang.org/t/checking-if-function-input-is-dataframe/56707/5 "2021-03-08T05:41:42Z")

</div>

`if` may even get optimized away at the compile time:

```julia
julia> function f(x)
           if typeof(x) == Int64 throw(Error("Int")) end
           3
       end

julia> @code_native f(3.0)
	.text
; ┌ @ REPL[1]:1 within `f'
	movl	$3, %eax
	retq
	nopw	%cs:(%rax,%rax)
; └

julia> @code_native f("a")
	.text
; ┌ @ REPL[1]:1 within `f'
	movl	$3, %eax
	retq
	nopw	%cs:(%rax,%rax)
; └

```

---

<div class="post-metadata">

**Author:** ![jo-fleck](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@jo-fleck](https://discourse.julialang.org/u/jo-fleck)\
**Post date:** [March 8, 2021, 12:44pm UTC](https://discourse.julialang.org/t/checking-if-function-input-is-dataframe/56707/6 "2021-03-08T12:44:05Z")

</div>

> This seems like an issue with the profiler

You’re right. I just commented the type check and now the profiler shows the next line to account for 50% of elapsed time. (I might be misinterpreting the flame graph.) I then used `@btime` to measure performance before and after commenting all input checks - the difference is negligible.
