Performance tips#

using DataFrames
using BenchmarkTools
using CategoricalArrays
using PooledArrays
using Random

Access by column number is faster than by name#

x = DataFrame(rand(5, 1000), :auto)
@btime $x[!, 500]; ## Faster
  4.147 ns (0 allocations: 0 bytes)
@btime $x.x500;  ## Slower
  15.320 ns (0 allocations: 0 bytes)

When working with data DataFrame use barrier functions or type annotation#

function f_bad() ## this function will be slow
    Random.seed!(1)
    x = DataFrame(rand(1000000, 2), :auto)
    y, z = x[!, 1], x[!, 2]
    p = 0.0
    for i in 1:nrow(x)
        p += y[i] * z[i]
    end
    p
end

@btime f_bad();
# if you run @code_warntype f_bad() then you notice
# that Julia does not know column types of `DataFrame`
  187.119 ms (5999022 allocations: 122.06 MiB)

solution 1 is to use barrier function (it should be possible to use it in almost any code) for the calculation. You will notice much less memopry allocations and faster performance.

function f_inner(y, z)
    p = 0.0
    for i in eachindex(y, z)
        p += y[i] * z[i]
    end
    p
end

function f_barrier()
    Random.seed!(1)
    x = DataFrame(rand(1000000, 2), :auto)
    f_inner(x[!, 1], x[!, 2])
end

@btime f_barrier();
  4.258 ms (44 allocations: 30.52 MiB)

or use inbuilt function if possible

using LinearAlgebra

function f_inbuilt()
    Random.seed!(1)
    x = DataFrame(rand(1000000, 2), :auto)
    dot(x[!, 1], x[!, 2])
end

@btime f_inbuilt();
  3.201 ms (44 allocations: 30.52 MiB)

solution 2 is to provide the types of extracted columns. However, there are cases in which you will not know these types.

function f_typed()
    Random.seed!(1)
    x = DataFrame(rand(1000000, 2), :auto)
    y::Vector{Float64}, z::Vector{Float64} = x[!, 1], x[!, 2]
    p = 0.0
    for i in 1:nrow(x)
        p += y[i] * z[i]
    end
    p
end

@btime f_typed();
  3.802 ms (44 allocations: 30.52 MiB)

In general for tall and narrow tables it is often useful to use Tables.rowtable, Tables.columntable or Tables.namedtupleiterator for intermediate processing of data in a type-stable way.

Consider using delayed DataFrame creation technique#

also notice the difference in performance between copying vs non-copying data frame creation

function f1()
    x = DataFrame([Vector{Float64}(undef, 10^4) for i in 1:100], :auto, copycols=false) ## we work with a DataFrame directly
    for c in 1:ncol(x)
        d = x[!, c]
        for r in 1:nrow(x)
            d[r] = rand()
        end
    end
    x
end

function f1a()
    x = DataFrame([Vector{Float64}(undef, 10^4) for i in 1:100], :auto) ## we work with a DataFrame directly
    for c in 1:ncol(x)
        d = x[!, c]
        for r in 1:nrow(x)
            d[r] = rand()
        end
    end
    x
end

function f2()
    x = Vector{Any}(undef, 100)
    for c in 1:length(x)
        d = Vector{Float64}(undef, 10^4)
        for r in eachindex(d)
            d[r] = rand()
        end
        x[c] = d
    end
    DataFrame(x, :auto, copycols=false) ## we delay creation of DataFrame after we have our job done
end

function f2a()
    x = Vector{Any}(undef, 100)
    for c in eachindex(x)
        d = Vector{Float64}(undef, 10^4)
        for r in eachindex(d)
            d[r] = rand()
        end
        x[c] = d
    end
    DataFrame(x, :auto) ## we delay creation of DataFrame after we have our job done
end

@btime f1();
@btime f1a();
@btime f2();
@btime f2a();
  27.688 ms (1949728 allocations: 37.40 MiB)
  28.602 ms (1950028 allocations: 45.03 MiB)
  1.078 ms (728 allocations: 7.66 MiB)
  1.518 ms (1028 allocations: 15.29 MiB)

You can add rows to a DataFrame in place and it is fast#

x = DataFrame(rand(10^6, 5), :auto)
y = DataFrame(transpose(1.0:5.0), :auto)
z = [1.0:5.0;]

@btime vcat($x, $y); ## creates a new DataFrame - slow
@btime append!($x, $y); ## in place - fast

x = DataFrame(rand(10^6, 5), :auto) ## reset to the same starting point
@btime push!($x, $z); ## add a single row in place - fast
  2.363 ms (212 allocations: 38.16 MiB)
  1.135 μs (29 allocations: 1.50 KiB)
  397.607 ns (16 allocations: 256 bytes)

Allowing missing as well as categorical slows down computations#

using StatsBase

function test(data) ## uses countmap function to test performance
    println(eltype(data))
    x = rand(data, 10^6)
    y = categorical(x)
    println(" raw:")
    @btime countmap($x)
    println(" categorical:")
    @btime countmap($y)
    nothing
end

test(1:10)
test([randstring() for i in 1:10])
test(allowmissing(1:10))
test(allowmissing([randstring() for i in 1:10]))
Int64
 raw:
  1.519 ms (8 allocations: 7.63 MiB)
 categorical:
  14.711 ms (1000004 allocations: 30.52 MiB)
String
 raw:
  20.785 ms (4 allocations: 448 bytes)
 categorical:
  31.675 ms (1000004 allocations: 30.52 MiB)
Union{Missing, Int64}
 raw:
  5.560 ms (4 allocations: 464 bytes)
 categorical:
  14.751 ms (1000004 allocations: 30.52 MiB)
Union{Missing, String}
 raw:
  20.356 ms (4 allocations: 448 bytes)
 categorical:
  30.319 ms (1000004 allocations: 30.52 MiB)

When aggregating use column selector and prefer integer, categorical, or pooled array grouping variable#

df = DataFrame(x=rand('a':'d', 10^7), y=1);
gdf = groupby(df, :x)

GroupedDataFrame with 4 groups based on key: x

First Group (2500230 rows): x = 'd': ASCII/Unicode U+0064 (category Ll: Letter, lowercase)
2500205 rows omitted
Rowxy
CharInt64
1d1
2d1
3d1
4d1
5d1
6d1
7d1
8d1
9d1
10d1
11d1
12d1
13d1
2500219d1
2500220d1
2500221d1
2500222d1
2500223d1
2500224d1
2500225d1
2500226d1
2500227d1
2500228d1
2500229d1
2500230d1

Last Group (2499639 rows): x = 'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
2499614 rows omitted
Rowxy
CharInt64
1b1
2b1
3b1
4b1
5b1
6b1
7b1
8b1
9b1
10b1
11b1
12b1
13b1
2499628b1
2499629b1
2499630b1
2499631b1
2499632b1
2499633b1
2499634b1
2499635b1
2499636b1
2499637b1
2499638b1
2499639b1

traditional syntax, slow

@btime combine(v -> sum(v.y), $gdf)
  18.085 ms (322 allocations: 19.09 MiB)
4×2 DataFrame
Rowxx1
CharInt64
1d2500230
2a2502381
3c2497750
4b2499639

use column selector

@btime combine($gdf, :y => sum)
  6.758 ms (198 allocations: 10.14 KiB)
4×2 DataFrame
Rowxy_sum
CharInt64
1d2500230
2a2502381
3c2497750
4b2499639
transform!(df, :x => categorical => :x);
gdf = groupby(df, :x)

GroupedDataFrame with 4 groups based on key: x

First Group (2502381 rows): x = CategoricalArrays.CategoricalValue{Char, UInt32} 'a'
2502356 rows omitted
Rowxy
Cat…Int64
1a1
2a1
3a1
4a1
5a1
6a1
7a1
8a1
9a1
10a1
11a1
12a1
13a1
2502370a1
2502371a1
2502372a1
2502373a1
2502374a1
2502375a1
2502376a1
2502377a1
2502378a1
2502379a1
2502380a1
2502381a1

Last Group (2500230 rows): x = CategoricalArrays.CategoricalValue{Char, UInt32} 'd'
2500205 rows omitted
Rowxy
Cat…Int64
1d1
2d1
3d1
4d1
5d1
6d1
7d1
8d1
9d1
10d1
11d1
12d1
13d1
2500219d1
2500220d1
2500221d1
2500222d1
2500223d1
2500224d1
2500225d1
2500226d1
2500227d1
2500228d1
2500229d1
2500230d1
@btime combine($gdf, :y => sum)
  6.450 ms (206 allocations: 10.62 KiB)
4×2 DataFrame
Rowxy_sum
Cat…Int64
1a2502381
2b2499639
3c2497750
4d2500230
transform!(df, :x => PooledArray{Char} => :x)
10000000×2 DataFrame
9999975 rows omitted
Rowxy
CharInt64
1d1
2a1
3c1
4b1
5a1
6b1
7b1
8a1
9d1
10b1
11c1
12d1
13c1
9999989b1
9999990a1
9999991d1
9999992d1
9999993a1
9999994c1
9999995b1
9999996c1
9999997d1
9999998a1
9999999d1
10000000a1
gdf = groupby(df, :x)

GroupedDataFrame with 4 groups based on key: x

First Group (2500230 rows): x = 'd': ASCII/Unicode U+0064 (category Ll: Letter, lowercase)
2500205 rows omitted
Rowxy
CharInt64
1d1
2d1
3d1
4d1
5d1
6d1
7d1
8d1
9d1
10d1
11d1
12d1
13d1
2500219d1
2500220d1
2500221d1
2500222d1
2500223d1
2500224d1
2500225d1
2500226d1
2500227d1
2500228d1
2500229d1
2500230d1

Last Group (2499639 rows): x = 'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
2499614 rows omitted
Rowxy
CharInt64
1b1
2b1
3b1
4b1
5b1
6b1
7b1
8b1
9b1
10b1
11b1
12b1
13b1
2499628b1
2499629b1
2499630b1
2499631b1
2499632b1
2499633b1
2499634b1
2499635b1
2499636b1
2499637b1
2499638b1
2499639b1
@btime combine($gdf, :y => sum)
  6.504 ms (200 allocations: 10.20 KiB)
4×2 DataFrame
Rowxy_sum
CharInt64
1d2500230
2a2502381
3c2497750
4b2499639

Use views instead of materializing a new DataFrame#

x = DataFrame(rand(100, 1000), :auto)
@btime $x[1:1, :]
  157.715 μs (3993 allocations: 159.03 KiB)
1×1000 DataFrame
900 columns omitted
Rowx1x2x3x4x5x6x7x8x9x10x11x12x13x14x15x16x17x18x19x20x21x22x23x24x25x26x27x28x29x30x31x32x33x34x35x36x37x38x39x40x41x42x43x44x45x46x47x48x49x50x51x52x53x54x55x56x57x58x59x60x61x62x63x64x65x66x67x68x69x70x71x72x73x74x75x76x77x78x79x80x81x82x83x84x85x86x87x88x89x90x91x92x93x94x95x96x97x98x99x100
Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64
10.8577090.8688820.3876810.4880230.8346450.8663470.554410.08243490.2139830.9182950.4651220.6668020.2190030.8331220.3324780.8916960.7987610.3286780.8729070.169320.7312640.04981520.04529860.861390.2244540.6760930.7020680.6300110.2129970.546920.9532150.2467590.2855610.8309330.2084190.2983920.8561820.03638830.9636360.9810620.5123050.7529760.3542390.8180940.1936170.9416410.6031150.1468020.5435130.4946390.3481840.2829770.7559990.3551260.4408870.3064590.7732690.8182250.1951170.5327330.6300130.3480140.01221880.9800420.3234570.2508440.979990.8539180.2270020.2066620.3154330.9491480.383650.4496280.06491620.2889040.0471820.05645740.08316070.8593130.6919220.3866130.3541050.2112950.1328310.1748550.7548090.2689240.5519610.2187010.3281550.7798580.9856880.6299670.5206660.6342120.8319470.5633280.1368190.954159
@btime $x[1, :]
  19.132 ns (0 allocations: 0 bytes)
DataFrameRow (1000 columns)
900 columns omitted
Rowx1x2x3x4x5x6x7x8x9x10x11x12x13x14x15x16x17x18x19x20x21x22x23x24x25x26x27x28x29x30x31x32x33x34x35x36x37x38x39x40x41x42x43x44x45x46x47x48x49x50x51x52x53x54x55x56x57x58x59x60x61x62x63x64x65x66x67x68x69x70x71x72x73x74x75x76x77x78x79x80x81x82x83x84x85x86x87x88x89x90x91x92x93x94x95x96x97x98x99x100
Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64
10.8577090.8688820.3876810.4880230.8346450.8663470.554410.08243490.2139830.9182950.4651220.6668020.2190030.8331220.3324780.8916960.7987610.3286780.8729070.169320.7312640.04981520.04529860.861390.2244540.6760930.7020680.6300110.2129970.546920.9532150.2467590.2855610.8309330.2084190.2983920.8561820.03638830.9636360.9810620.5123050.7529760.3542390.8180940.1936170.9416410.6031150.1468020.5435130.4946390.3481840.2829770.7559990.3551260.4408870.3064590.7732690.8182250.1951170.5327330.6300130.3480140.01221880.9800420.3234570.2508440.979990.8539180.2270020.2066620.3154330.9491480.383650.4496280.06491620.2889040.0471820.05645740.08316070.8593130.6919220.3866130.3541050.2112950.1328310.1748550.7548090.2689240.5519610.2187010.3281550.7798580.9856880.6299670.5206660.6342120.8319470.5633280.1368190.954159
@btime view($x, 1:1, :)
  19.132 ns (0 allocations: 0 bytes)
1×1000 SubDataFrame
900 columns omitted
Rowx1x2x3x4x5x6x7x8x9x10x11x12x13x14x15x16x17x18x19x20x21x22x23x24x25x26x27x28x29x30x31x32x33x34x35x36x37x38x39x40x41x42x43x44x45x46x47x48x49x50x51x52x53x54x55x56x57x58x59x60x61x62x63x64x65x66x67x68x69x70x71x72x73x74x75x76x77x78x79x80x81x82x83x84x85x86x87x88x89x90x91x92x93x94x95x96x97x98x99x100
Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64
10.8577090.8688820.3876810.4880230.8346450.8663470.554410.08243490.2139830.9182950.4651220.6668020.2190030.8331220.3324780.8916960.7987610.3286780.8729070.169320.7312640.04981520.04529860.861390.2244540.6760930.7020680.6300110.2129970.546920.9532150.2467590.2855610.8309330.2084190.2983920.8561820.03638830.9636360.9810620.5123050.7529760.3542390.8180940.1936170.9416410.6031150.1468020.5435130.4946390.3481840.2829770.7559990.3551260.4408870.3064590.7732690.8182250.1951170.5327330.6300130.3480140.01221880.9800420.3234570.2508440.979990.8539180.2270020.2066620.3154330.9491480.383650.4496280.06491620.2889040.0471820.05645740.08316070.8593130.6919220.3866130.3541050.2112950.1328310.1748550.7548090.2689240.5519610.2187010.3281550.7798580.9856880.6299670.5206660.6342120.8319470.5633280.1368190.954159
@btime $x[1:1, 1:20]
  3.517 μs (70 allocations: 3.09 KiB)
1×20 DataFrame
Rowx1x2x3x4x5x6x7x8x9x10x11x12x13x14x15x16x17x18x19x20
Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64
10.8577090.8688820.3876810.4880230.8346450.8663470.554410.08243490.2139830.9182950.4651220.6668020.2190030.8331220.3324780.8916960.7987610.3286780.8729070.16932
@btime $x[1, 1:20]
  19.745 ns (0 allocations: 0 bytes)
DataFrameRow (20 columns)
Rowx1x2x3x4x5x6x7x8x9x10x11x12x13x14x15x16x17x18x19x20
Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64
10.8577090.8688820.3876810.4880230.8346450.8663470.554410.08243490.2139830.9182950.4651220.6668020.2190030.8331220.3324780.8916960.7987610.3286780.8729070.16932
@btime view($x, 1:1, 1:20)
  18.199 ns (0 allocations: 0 bytes)
1×20 SubDataFrame
Rowx1x2x3x4x5x6x7x8x9x10x11x12x13x14x15x16x17x18x19x20
Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64
10.8577090.8688820.3876810.4880230.8346450.8663470.554410.08243490.2139830.9182950.4651220.6668020.2190030.8331220.3324780.8916960.7987610.3286780.8729070.16932

This notebook was generated using Literate.jl.