xiaodai
xiaodai

Reputation: 16054

Julia: How to make colwise return an array instead of an array of arrays?

I wanted to make a function that looks at every column of a DataFrame and return a boolean, so I end up with an array of booleans. Here is the code

# some random dataframe
df = DataFrame([1:3, 4:6])

# a function that returns an array of boolean
function some_bool_fn(df)::Array{Bool}
    array_of_arrays = colwise(df) do sdd3
        # for illustration only
        return true
    end
    array = [a[1] for a in array_of_arrays]
    return array
end

# calling the function
some_bool_fn(dd3)

This works except I find the line

        array = [a[1] for a in array_of_arrays]

a bit wasteful. Basically I get an array of arrays as the output of colwise, so I then had to put the array of arrays into a simple array of bools. Is there a way to write the code so I can avoid this line of code?

Upvotes: 1

Views: 269

Answers (1)

Liso
Liso

Reputation: 2260

As @Gnimuc commented this behaviour is changing.

If you look at master branch: https://github.com/JuliaData/DataFrames.jl/blob/master/src/groupeddataframe/grouping.jl#L241 you'll see another version. You could probably copy it:

mycolwise(f, d::AbstractDataFrame) = [f(d[i]) for i in 1:ncol(d)]

Upvotes: 3

Related Questions