Using variable names to parse DataFrame

Hi everyone! I’m brand new to Julia (and fairly new to coding in general). I’m currently trying to write a function that will edit an object of type DataFrame to make it workable for the Ripserer package. The function works when I just pass in the data frame… however, I want it to also be able to take in optional arguments for how much of the original data frame I want to keep… and that’s where I am encountering issues. I think it has to do with the variable types I declare at the beginning of the function, but I’m not sure. Here’s my function:

function prep_data(df, starting_row::Int64 = -1, ending_row::Int64 = -1, starting_col::Int64 = -1, ending_col::Int64 = -1)
    
    if (starting_row != -1 && ending_row != -1)
        row_temp = df[:starting_row:ending_row, :]

    elseif (starting_row == -1 && ending_row == -1)
        row_temp = df
    
    else print("Invalid input. If a starting_row val is given, an ending_row val must be given as well.")
    end 
    
    if (starting_col != -1 && ending_col != -1)
        col_temp = row_temp[:, :starting_col:ending_col]
        
    elseif (starting_col == -1 && ending_col == -1)
        col_temp = row_temp
    
    else print("Invalid input. If a starting_col val is given, an ending_col val must be given as well")
    end
    
    
    y = transpose(Array(col_temp))
    
    z = [tuple(y[:,c]...) for c in 1:size(y, 2)]
    
    return z
end

From what I’ve gathered, using starting_row (or the other variable names for that matter) inside of the brackets for parsing the DataFrame is causing the error. Here’s the error message I’m getting:

MethodError: no method matching (::Colon)(::Symbol, ::Int64)
Closest candidates are:
(::Colon)(::T, ::Any, ::T) where T at range.jl:40
(::Colon)(::T, ::Real, ::T) where T<:AbstractFloat at range.jl:18
(::Colon)(::T, ::T) where T<:Real at range.jl:5

Stacktrace:
[1] prep_data(df::DataFrame, starting_row::Int64, ending_row::Int64, starting_col::Int64, ending_col::Int64)
@ Main .\In[49]:4
[2] top-level scope
@ In[50]:1
[3] eval
@ .\boot.jl:360 [inlined]
[4] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base .\loading.jl:1116

Hi @cwallj there’s something wrong with the formatting in your post so it’s hard to read. You can make a code block by using triple-backticks like this:

```
function foo()
1 + 1
end
```

Sorry about that! I just fixed it.

1 Like

It would also be helpful to show what problems you’re having by including the code you’re using to run it, some sample input data, the desired output, and the current output. Some more info in Please read: make it easier to help you

1 Like

Thanks for the tip. I just read through that and edited my post accordingly.

I think you mean starting_row, not :starting_row in the brackets. Likewise starting_col.

That worked! I thought you had to put a colon before the first variable in the brackets to modify the dataframe, but I guess not.

Thanks for your help.

1 Like