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
  2.025 ns (0 allocations: 0 bytes)
@btime $x.x500;  ## Slower
  10.158 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`
  91.196 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();
  6.399 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();
  5.701 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();
  6.438 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();
  23.482 ms (1949728 allocations: 37.40 MiB)
  25.847 ms (1950028 allocations: 45.03 MiB)
  1.514 ms (728 allocations: 7.66 MiB)
  2.204 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
  5.250 ms (213 allocations: 38.16 MiB)
  1.101 μs (30 allocations: 1.52 KiB)
  383.557 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:
  2.337 ms (8 allocations: 7.63 MiB)
 categorical:
  9.388 ms (4 allocations: 576 bytes)
String
 raw:
  21.371 ms (4 allocations: 448 bytes)
 categorical:
  24.977 ms (4 allocations: 576 bytes)
Union{Missing, Int64}
 raw:
  7.312 ms (4 allocations: 464 bytes)
 categorical:
  22.333 ms (1000004 allocations: 30.52 MiB)
Union{Missing, String}
 raw:
  27.243 ms (4 allocations: 448 bytes)
 categorical:
  46.391 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 (2500973 rows): x = 'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
2500948 rows omitted
Rowxy
CharInt64
1a1
2a1
3a1
4a1
5a1
6a1
7a1
8a1
9a1
10a1
11a1
12a1
13a1
2500962a1
2500963a1
2500964a1
2500965a1
2500966a1
2500967a1
2500968a1
2500969a1
2500970a1
2500971a1
2500972a1
2500973a1

Last Group (2500723 rows): x = 'd': ASCII/Unicode U+0064 (category Ll: Letter, lowercase)
2500698 rows omitted
Rowxy
CharInt64
1d1
2d1
3d1
4d1
5d1
6d1
7d1
8d1
9d1
10d1
11d1
12d1
13d1
2500712d1
2500713d1
2500714d1
2500715d1
2500716d1
2500717d1
2500718d1
2500719d1
2500720d1
2500721d1
2500722d1
2500723d1

traditional syntax, slow

@btime combine(v -> sum(v.y), $gdf)
  30.670 ms (333 allocations: 19.09 MiB)
4×2 DataFrame
Rowxx1
CharInt64
1a2500973
2b2499799
3c2498505
4d2500723

use column selector

@btime combine($gdf, :y => sum)
  11.341 ms (199 allocations: 9.41 KiB)
4×2 DataFrame
Rowxy_sum
CharInt64
1a2500973
2b2499799
3c2498505
4d2500723
transform!(df, :x => categorical => :x);
gdf = groupby(df, :x)

GroupedDataFrame with 4 groups based on key: x

First Group (2500973 rows): x = CategoricalArrays.CategoricalValue{Char, UInt32} 'a'
2500948 rows omitted
Rowxy
Cat…Int64
1a1
2a1
3a1
4a1
5a1
6a1
7a1
8a1
9a1
10a1
11a1
12a1
13a1
2500962a1
2500963a1
2500964a1
2500965a1
2500966a1
2500967a1
2500968a1
2500969a1
2500970a1
2500971a1
2500972a1
2500973a1

Last Group (2500723 rows): x = CategoricalArrays.CategoricalValue{Char, UInt32} 'd'
2500698 rows omitted
Rowxy
Cat…Int64
1d1
2d1
3d1
4d1
5d1
6d1
7d1
8d1
9d1
10d1
11d1
12d1
13d1
2500712d1
2500713d1
2500714d1
2500715d1
2500716d1
2500717d1
2500718d1
2500719d1
2500720d1
2500721d1
2500722d1
2500723d1
@btime combine($gdf, :y => sum)
  11.298 ms (209 allocations: 9.98 KiB)
4×2 DataFrame
Rowxy_sum
Cat…Int64
1a2500973
2b2499799
3c2498505
4d2500723
transform!(df, :x => PooledArray{Char} => :x)
10000000×2 DataFrame
9999975 rows omitted
Rowxy
CharInt64
1a1
2a1
3b1
4b1
5c1
6a1
7b1
8d1
9a1
10d1
11c1
12a1
13c1
9999989d1
9999990b1
9999991a1
9999992d1
9999993b1
9999994a1
9999995c1
9999996d1
9999997b1
9999998a1
9999999c1
10000000a1
gdf = groupby(df, :x)

GroupedDataFrame with 4 groups based on key: x

First Group (2500973 rows): x = 'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
2500948 rows omitted
Rowxy
CharInt64
1a1
2a1
3a1
4a1
5a1
6a1
7a1
8a1
9a1
10a1
11a1
12a1
13a1
2500962a1
2500963a1
2500964a1
2500965a1
2500966a1
2500967a1
2500968a1
2500969a1
2500970a1
2500971a1
2500972a1
2500973a1

Last Group (2500723 rows): x = 'd': ASCII/Unicode U+0064 (category Ll: Letter, lowercase)
2500698 rows omitted
Rowxy
CharInt64
1d1
2d1
3d1
4d1
5d1
6d1
7d1
8d1
9d1
10d1
11d1
12d1
13d1
2500712d1
2500713d1
2500714d1
2500715d1
2500716d1
2500717d1
2500718d1
2500719d1
2500720d1
2500721d1
2500722d1
2500723d1
@btime combine($gdf, :y => sum)
  11.448 ms (201 allocations: 9.47 KiB)
4×2 DataFrame
Rowxy_sum
CharInt64
1a2500973
2b2499799
3c2498505
4d2500723

Use views instead of materializing a new DataFrame#

x = DataFrame(rand(100, 1000), :auto)
@btime $x[1:1, :]
  416.103 μs (3015 allocations: 143.79 KiB)
1×1000 DataFrame
900 columns omitted
Rowx1x2x3x4x5x6x7x8x9x10x11x12x13x14x15x16x17x18x19x20x21x22x23x24x25x26x27x28x29x30x31x32x33x34x35x36x37x38x39x40x41x42x43x44x45x46x47x48x49x50x51x52x53x54x55x56x57x58x59x60x61x62x63x64x65x66x67x68x69x70x71x72x73x74x75x76x77x78x79x80x81x82x83x84x85x86x87x88x89x90x91x92x93x94x95x96x97x98x99x100
Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64
10.9271910.9251470.8602320.3670420.7930310.2710710.6592310.6030750.8348240.2568020.7671160.6114230.1428120.5504030.7333920.50640.7977490.7771340.03419150.3454160.2556110.429720.6050440.5513890.3061610.5479190.470350.2773870.2586540.01619370.4953120.1096940.1155070.8827180.09009650.5511320.9012280.671870.7802740.08171410.9133720.9120950.4464140.06838160.528050.9812850.8907960.067330.4583820.4620610.4091880.2935760.2073610.9579210.8668040.4692450.5847370.8518470.2637340.7679930.7215840.8835680.403730.616150.6649860.1701560.2637280.3008010.7622330.9537210.4149860.4564280.1895940.2965960.1946980.3055150.7836460.1857510.605420.3505350.2171340.1748450.9994560.7076540.377260.4785490.6990220.3316790.5105210.4985650.8187610.7150340.9915230.912230.7597740.8784930.6093210.3541860.8683550.946109
@btime $x[1, :]
  15.762 ns (0 allocations: 0 bytes)
DataFrameRow (1000 columns)
900 columns omitted
Rowx1x2x3x4x5x6x7x8x9x10x11x12x13x14x15x16x17x18x19x20x21x22x23x24x25x26x27x28x29x30x31x32x33x34x35x36x37x38x39x40x41x42x43x44x45x46x47x48x49x50x51x52x53x54x55x56x57x58x59x60x61x62x63x64x65x66x67x68x69x70x71x72x73x74x75x76x77x78x79x80x81x82x83x84x85x86x87x88x89x90x91x92x93x94x95x96x97x98x99x100
Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64
10.9271910.9251470.8602320.3670420.7930310.2710710.6592310.6030750.8348240.2568020.7671160.6114230.1428120.5504030.7333920.50640.7977490.7771340.03419150.3454160.2556110.429720.6050440.5513890.3061610.5479190.470350.2773870.2586540.01619370.4953120.1096940.1155070.8827180.09009650.5511320.9012280.671870.7802740.08171410.9133720.9120950.4464140.06838160.528050.9812850.8907960.067330.4583820.4620610.4091880.2935760.2073610.9579210.8668040.4692450.5847370.8518470.2637340.7679930.7215840.8835680.403730.616150.6649860.1701560.2637280.3008010.7622330.9537210.4149860.4564280.1895940.2965960.1946980.3055150.7836460.1857510.605420.3505350.2171340.1748450.9994560.7076540.377260.4785490.6990220.3316790.5105210.4985650.8187610.7150340.9915230.912230.7597740.8784930.6093210.3541860.8683550.946109
@btime view($x, 1:1, :)
  16.860 ns (0 allocations: 0 bytes)
1×1000 SubDataFrame
900 columns omitted
Rowx1x2x3x4x5x6x7x8x9x10x11x12x13x14x15x16x17x18x19x20x21x22x23x24x25x26x27x28x29x30x31x32x33x34x35x36x37x38x39x40x41x42x43x44x45x46x47x48x49x50x51x52x53x54x55x56x57x58x59x60x61x62x63x64x65x66x67x68x69x70x71x72x73x74x75x76x77x78x79x80x81x82x83x84x85x86x87x88x89x90x91x92x93x94x95x96x97x98x99x100
Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64
10.9271910.9251470.8602320.3670420.7930310.2710710.6592310.6030750.8348240.2568020.7671160.6114230.1428120.5504030.7333920.50640.7977490.7771340.03419150.3454160.2556110.429720.6050440.5513890.3061610.5479190.470350.2773870.2586540.01619370.4953120.1096940.1155070.8827180.09009650.5511320.9012280.671870.7802740.08171410.9133720.9120950.4464140.06838160.528050.9812850.8907960.067330.4583820.4620610.4091880.2935760.2073610.9579210.8668040.4692450.5847370.8518470.2637340.7679930.7215840.8835680.403730.616150.6649860.1701560.2637280.3008010.7622330.9537210.4149860.4564280.1895940.2965960.1946980.3055150.7836460.1857510.605420.3505350.2171340.1748450.9994560.7076540.377260.4785490.6990220.3316790.5105210.4985650.8187610.7150340.9915230.912230.7597740.8784930.6093210.3541860.8683550.946109
@btime $x[1:1, 1:20]
  8.795 μs (70 allocations: 3.09 KiB)
1×20 DataFrame
Rowx1x2x3x4x5x6x7x8x9x10x11x12x13x14x15x16x17x18x19x20
Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64
10.9271910.9251470.8602320.3670420.7930310.2710710.6592310.6030750.8348240.2568020.7671160.6114230.1428120.5504030.7333920.50640.7977490.7771340.03419150.345416
@btime $x[1, 1:20]
  19.222 ns (0 allocations: 0 bytes)
DataFrameRow (20 columns)
Rowx1x2x3x4x5x6x7x8x9x10x11x12x13x14x15x16x17x18x19x20
Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64
10.9271910.9251470.8602320.3670420.7930310.2710710.6592310.6030750.8348240.2568020.7671160.6114230.1428120.5504030.7333920.50640.7977490.7771340.03419150.345416
@btime view($x, 1:1, 1:20)
  19.501 ns (0 allocations: 0 bytes)
1×20 SubDataFrame
Rowx1x2x3x4x5x6x7x8x9x10x11x12x13x14x15x16x17x18x19x20
Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64
10.9271910.9251470.8602320.3670420.7930310.2710710.6592310.6030750.8348240.2568020.7671160.6114230.1428120.5504030.7333920.50640.7977490.7771340.03419150.345416

This notebook was generated using Literate.jl.