# ERROR: BoundsError when using function taking dataframe as input

**URL:** <https://discourse.julialang.org/t/error-boundserror-when-using-function-taking-dataframe-as-input/80877>\
**Category:** New to Julia\
**Tags:** dataframes, function\
**Created:** [May 11, 2022, 11:05am UTC](https://discourse.julialang.org/t/error-boundserror-when-using-function-taking-dataframe-as-input/80877 "2022-05-11T11:05:33Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Skander1993](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skander1993/32/33081_2.png) [@Skander1993](https://discourse.julialang.org/u/Skander1993)\
**Post date:** [May 11, 2022, 11:05am UTC](https://discourse.julialang.org/t/error-boundserror-when-using-function-taking-dataframe-as-input/80877/1 "2022-05-11T11:05:33Z")

</div>

Hi there,

I have been experiencing a bound error while using a function taking as input a dataframe and returning one as well. I have tried different versions of the code hereafter

```julia
function Getdf(df_in)
    cert = convert(Vector, df_in[:,"cert"]);
    certu = unique(cert);
    cert_keep = certu[1:10];
    idx_keep = cert .== cert_keep[1];
    jt = 2;
    maxjt = length(cert_keep);
    while jt <= maxjt
        id = cert_keep[jt];
        idx_keep = idx_keep .| (cert .== id);
        jt = jt + 1;
    end
    df_out = df_in[vec(idx_keep),:];
    
    return df_out
end

df_in_new = Getdf(df_in_old);

```

however, I keep receiving the following message:

```julia
ERROR: BoundsError: attempt to access 0-element Vector{Base.StackTraces.StackFrame} at index [1]

```

There is something fundamental here that I do not seem to understand.

Most of the questions related to this topic suggest preallocating df\_in\_new. I am trying to define df\_in\_new by using the function Getdf. In the past, I was able to define variables in this way without prelocating them. So, I am not able to see what is different now.

I would greatly appreciate your help on this.

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [May 11, 2022, 12:00pm UTC](https://discourse.julialang.org/t/error-boundserror-when-using-function-taking-dataframe-as-input/80877/2 "2022-05-11T12:00:04Z")

</div>

Hi Skander, welcome.

Below is how I would write your function, but I’ve added some questions as comments. I think this is causing your error:

> [@Skander1993](#):
>
> `cert_keep = certu[1:10];`

```julia
using DataFrames

# function Getdf(df_in)
function Getdf(df_in::DataFrame) # Are you always expecting the input to be a dataframe?
    # cert = convert(Vector, df_in[:,"cert"]);
    cert = df_in[:,"cert"] # dataframe columns are already `Vector`s`

    # certu = unique(cert);
    certu = unique(cert) # terminating with a semicolon in functions makes no different

    # cert_keep = certu[1:10];
    # But how do you know there are at least 10 unique elements in cert?
    cert_keep = certu[1:10] # ? I think this is where your error is occuring ?

    # idx_keep = cert .== cert_keep[1];
    idx_keep = (cert .== cert_keep[1]) # bracket to help readability

    # jt = 2;
    # maxjt = length(cert_keep);
    # while jt <= maxjt
    for jt in 2:length(cert_keep)
        # id = cert_keep[jt];
        id = cert_keep[jt]
        # idx_keep = idx_keep .| (cert .== id);
        idx_keep = (idx_keep .|| (cert .== id)) # use the "boolean or" || (double) instead of the "bitwise or" | (single)
        # jt = jt + 1;
    end
    # df_out = df_in[vec(idx_keep),:];
    df_out = df_in[idx_keep,:] # I think `idx_keep` is always a Vector, so `vec` is redundant?
    
    return df_out
end

```

Can you provide a minimal input dataframe example to debug this? e.g.: something like

```julia
julia> df = DataFrame(A = 1:3, B = [2.0, -1.1, 2.8], cert = ["p","q","r"])
julia> Getdf(df)
ERROR: BoundsError: attempt to access 3-element Vector{String} at index [1:10]
Stacktrace:
 [1] throw_boundserror(A::Vector{String}, I::Tuple{UnitRange{Int64}})
   @ Base ./abstractarray.jl:691
 [2] checkbounds
   @ ./abstractarray.jl:656 [inlined]
 [3] getindex(A::Vector{String}, I::UnitRange{Int64})
   @ Base ./array.jl:867
 [4] Getdf(df_in::DataFrame)
   @ Main ~/julia/Examples/discourse/dataframe_err.jl:14

```

---

<div class="post-metadata">

**Author:** ![Skander1993](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skander1993/32/33081_2.png) [@Skander1993](https://discourse.julialang.org/u/Skander1993)\
**Post date:** [May 11, 2022, 12:37pm UTC](https://discourse.julialang.org/t/error-boundserror-when-using-function-taking-dataframe-as-input/80877/3 "2022-05-11T12:37:02Z")

</div>

Hi James,

Thanks for taking the time to write all those comments!

Here is an example that you can run and will give you the same error.

```julia
using DataFrames

df = DataFrame(A = 1:6, B = 1:6, cert = [1,1,2,2,3,3])

function Getdf(df_in::DataFrame) 
    cert = df_in[:,"cert"]
    certu = unique(cert)
    cert_keep = certu[1:2]
    idx_keep = (cert .== cert_keep[1])
    for jt in 2:length(cert_keep)
        id = cert_keep[jt]
        idx_keep = (idx_keep .| (cert .== id)) 
    end
    df_out = df_in[idx_keep,:]  
    return df_out
end

df_in_new = Getdf(df);

```

Notice that I have changed the line cert\_keep = certu[1:10] to cert\_keep = certu[1:2] to fit the example. My understanding from infiltrating the function is that the problem happens when I return the function’s output.

To answer your question, in this case, I am expecting a dataframe but I don’t know if that is generating the problem. I use the function to reduce the size of large dataset. The goal is to debug a set of functions on a small dataset to make the process faster. So, I know from the data that certu has more than 10 elements.

I hope this helps.

Skander

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 11, 2022, 12:39pm UTC](https://discourse.julialang.org/t/error-boundserror-when-using-function-taking-dataframe-as-input/80877/4 "2022-05-11T12:39:09Z")

</div>

That example runs fine for me and produces

```julia
julia> Getdf(df)
4×3 DataFrame
 Row │ A B cert
     │ Int64 Int64 Int64
─────┼─────────────────────
   1 │ 1 1 1
   2 │ 2 2 1
   3 │ 3 3 2
   4 │ 4 4 2

```

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [May 11, 2022, 12:40pm UTC](https://discourse.julialang.org/t/error-boundserror-when-using-function-taking-dataframe-as-input/80877/5 "2022-05-11T12:40:21Z")

</div>

same for me

---

<div class="post-metadata">

**Author:** ![Skander1993](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skander1993/32/33081_2.png) [@Skander1993](https://discourse.julialang.org/u/Skander1993)\
**Post date:** [May 11, 2022, 12:40pm UTC](https://discourse.julialang.org/t/error-boundserror-when-using-function-taking-dataframe-as-input/80877/6 "2022-05-11T12:40:51Z")

</div>

Really? This is what I get.

```julia
ERROR: BoundsError: attempt to access 0-element Vector{Base.StackTraces.StackFrame} at index [1]
Stacktrace:
 [1] getindex
   @ .\array.jl:801 [inlined]
 [2] start_prompt(mod::Module, locals::Dict{Symbol, Any}, file::String, fileline::Int64; terminal::Nothing, repl::Nothing, nostack::Bool)
   @ Infiltrator C:\Users\DELL\.julia\packages\Infiltrator\doHg1\src\Infiltrator.jl:212
 [3] start_prompt(mod::Module, locals::Dict{Symbol, Any}, file::String, fileline::Int64)
   @ Infiltrator C:\Users\DELL\.julia\packages\Infiltrator\doHg1\src\Infiltrator.jl:193
 [4] top-level scope
   @ C:\Users\DELL\.julia\packages\Infiltrator\doHg1\src\Infiltrator.jl:52

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 11, 2022, 12:49pm UTC](https://discourse.julialang.org/t/error-boundserror-when-using-function-taking-dataframe-as-input/80877/7 "2022-05-11T12:49:00Z")

</div>

Are you running this in a new Julia session?

---

<div class="post-metadata">

**Author:** ![Skander1993](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skander1993/32/33081_2.png) [@Skander1993](https://discourse.julialang.org/u/Skander1993)\
**Post date:** [May 11, 2022, 12:50pm UTC](https://discourse.julialang.org/t/error-boundserror-when-using-function-taking-dataframe-as-input/80877/8 "2022-05-11T12:50:45Z")

</div>

Yes, I am running it on my main script.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 11, 2022, 12:57pm UTC](https://discourse.julialang.org/t/error-boundserror-when-using-function-taking-dataframe-as-input/80877/9 "2022-05-11T12:57:00Z")

</div>

Sorry I don’t know what that means. Are you saying that if you start a fresh Julia REPL in your terminal and paste in the example you posted above you are getting the indexing error?

Bogumil and I are saying that we have tried this and did not see any errors, so if that’s still he case for you we’d need to know the ouptut of `versioninfo()` and `]st` to understand how you’re running Julia.

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [May 11, 2022, 12:58pm UTC](https://discourse.julialang.org/t/error-boundserror-when-using-function-taking-dataframe-as-input/80877/10 "2022-05-11T12:58:34Z")

</div>

From the error stacktrace, this is running out of an Infiltrator call.

---

<div class="post-metadata">

**Author:** ![Skander1993](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skander1993/32/33081_2.png) [@Skander1993](https://discourse.julialang.org/u/Skander1993)\
**Post date:** [May 11, 2022, 1:09pm UTC](https://discourse.julialang.org/t/error-boundserror-when-using-function-taking-dataframe-as-input/80877/11 "2022-05-11T13:09:14Z")

</div>

So, I have written the code on a script that I am running on VScode. I am not using the Julia REPL. As James mentioned, I am also using an infiltrator after the line

```julia
df_in_new = Getdf(df);

```

to inspect my variables. I should have probably mentioned that earlier.

Sorry for the confusion.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 11, 2022, 1:13pm UTC](https://discourse.julialang.org/t/error-boundserror-when-using-function-taking-dataframe-as-input/80877/12 "2022-05-11T13:13:37Z")

</div>

Ah, that clarifies things - I don’t know Infiltrator at all, but it appears that this error is entirely unrelated to your function which runs without error.

---

<div class="post-metadata">

**Author:** ![Skander1993](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skander1993/32/33081_2.png) [@Skander1993](https://discourse.julialang.org/u/Skander1993)\
**Post date:** [May 11, 2022, 1:20pm UTC](https://discourse.julialang.org/t/error-boundserror-when-using-function-taking-dataframe-as-input/80877/13 "2022-05-11T13:20:42Z")

</div>

Thanks for the answers. Somehow I thought that the problem was coming from the function because of the bound error. I just tried the regular debugger from VScode, and it seems to be working, but it’s way slower than the infiltrator. So, I will use the debugger until I figure out what is happening with `@infiltrate`.
