DataFrame

C++23 Build status Codacy Badge
GitHub tag (latest by date) Conan Center VCPKG package

DataFrame Lion

DataFrame documentation with code samples

This is a C++ analytical library designed for data analysis similar to libraries in Python and R. For example, you would compare this to Pandas or R data.frame
You can slice the data in many different ways. You can join, merge, group-by the data. You can run various statistical, summarization, financial, and ML algorithms on the data. You can add your custom algorithms easily. You can multi-column sort, custom pick and delete the data. And more …
DataFrame also includes a large collection of analytical algorithms in form of visitors. These are from basic stats such as Mean, Std Deviation, Return, … to more involved analysis such as Affinity Propagation, Polynomial Fit, Fast Fourier transform of arbitrary length … including a good collection of trading indicators. You can also easily add your own algorithms.
DataFrame also employs extensive multithreading in almost all its API’s, for large datasets. That makes DataFrame especially suitable for analyzing large datasets.
For basic operations to start you off, see Hello World. For a complete list of features with code samples, see documentation.

I have followed a few principles in this library:

  1. Support any type either built-in or user defined without needing new code
  2. Never chase pointers ala linked lists, std::any, pointer to base, …
  3. Have all column data in contiguous memory space
  4. Never use more space than you need ala unions, std::variant, …
  5. Avoid copying data as much as possible
  6. Use multi-threading but only when it makes sense
  7. Do not attempt to protect the user against garbage in, garbage out
  8. Keep DataFrame library self-contained, meaning DataFrame must only depend on C++ language and its standard library

Performance

You have probably heard of Polars DataFrame. It is implemented in Rust and ported with zero-overhead to Python (as long as you don’t have a loop). I have been asked by many people to write a comparison for DataFrame vs. Polars. So, I finally found some time to learn a bit about Polars and write a very simple benchmark.
I wrote the following identical programs for both Polars and C++ DataFrame (and Pandas). I used Polars version: 0.19.14 (Pandas version: 1.5.3, Numpy version: 1.24.2). And I used C++20 clang compiler with -O3 option. I ran both on my, somewhat outdated, MacBook Pro (Intel chip, 96GB RAM).
In both cases, I created a dataframe with 3 random columns. The C++ DataFrame also required an additional index column of the same size. Polars doesn’t believe in index columns (that has its own pros and cons. I am not going through it here). Each program has three identical parts. First it generates and populates 3 columns with 300m random numbers each (in case of C++ DataFrame, it must also generate a sequential index column of the same size). That is the part I am not interested in. In the second part, it calculates the mean of the first column, the variance of the second column, and the Pearson correlation of the second and third columns. In the third part, it does a select (or filter as Polars calls it) on one of the columns.

Results:
The maximum dataset I could load into Polars was 300m rows per column. Any bigger dataset blew up the memory and caused OS to kill it. I ran C++ DataFrame with 10b rows per column and I am sure it would have run with bigger datasets too. So, I was forced to run both with 300m rows to compare. I ran each test 4 times and took the best time. Polars numbers varied a lot from one run to another, especially calculation and selection times. C++ DataFrame numbers were significantly more consistent.

  C++ DataFrame Polars Pandas
Data generation/load time 26.945900 secs 28.468640 secs 36.678976 secs
Calculation time    1.260150 secs    4.876561 secs 40.326350 secs
Selection time    0.742493 secs    3.876561 secs    8.326350 secs
Overall time: 28.948600 secs 36.876345 secs 85.845114 secs

Please consider sponsoring DataFrame, especially if you are using it in production capacity. It is the strongest form of appreciation