Getting started#

Installation#

The package is built with CMake and nanobind.

pip install data-cube

Or from the sources:

git clone https://github.com/xadupre/data-cube.git
cd data-cube
python -m pip install nanobind numpy pandas cmake
# builds the extension inside the source tree
python setup.py build_ext --inplace
export PYTHONPATH=$(pwd)
# or builds and runs the C++ unit tests as well
python setup.py build_ext --inplace --cpp-tests

Excel export requires openpyxl and matplotlib (pip install data-cube[excel]).

The C++ library alone (static library, headers, CMake package data_cube::lib_data_cube) is built and installed with CMake:

cmake -S . -B build/cpp -DDATA_CUBE_BUILD_PYTHON=OFF -DCMAKE_INSTALL_PREFIX=dist/data-cube-cpp
cmake --build build/cpp --parallel
cmake --install build/cpp

A first cube#

import pandas
from data_cube import DataCube, CubeViewDef

df = pandas.DataFrame(
    [
        dict(date="2025/01/01", model_name="phi3", model_exporter="export", time_latency=0.1),
        dict(date="2025/01/02", model_name="phi3", model_exporter="export", time_latency=0.11),
        dict(date="2025/01/01", model_name="phi4", model_exporter="export", time_latency=0.1),
        dict(date="2025/01/01", model_name="phi4", model_exporter="dynamo", time_latency=0.14),
    ]
)

# keys and values are regular expressions
cube = DataCube(df, time="date", keys=["model_.*"], values=["time_.*"]).load()
print(cube.describe())

# one row per model, one column per (metric, exporter, date)
view = cube.view(CubeViewDef(key_index=["model_name"], values=["time_latency"]))
print(view)

# aggregates over models
view = cube.view(
    CubeViewDef(
        key_index=["model_exporter"],
        values=["time_latency"],
        key_agg=["model_name"],
        agg_args="mean",
    )
)
print(view)

# Excel file with one sheet per view
cube.to_excel("cube.xlsx", views={"latency": CubeViewDef(["model_name"], ["time_latency"])})

The same in C++:

#include "cube_core/data_cube.h"

using namespace data_cube;

Table table = read_csv("logs.csv");
DataCube cube({table}, "date", {"model_.*"}, {"time_.*"});
cube.load();
ViewDef view_def;
view_def.key_index = {"model_name"};
view_def.values = {"time_latency"};
Frame view = cube.view(view_def);
std::cout << view.to_string() << std::endl;
find_package(data_cube CONFIG REQUIRED)
target_link_libraries(my_target PRIVATE data_cube::lib_data_cube)

Behaviour notes#

  • errors raise ValueError,

  • aggregations (agg_args) are defined by their names (sum, mean, median, min, max, count, first, last, std, var, nunique, size), a callable returning one of those names per column is still allowed, agg_kwargs is not supported,

  • formulas are a dictionary {name: callable}, every callable receives a pandas.DataFrame and returns a series, formulas are evaluated in order and see the columns added by the previous ones,

  • with recent=True, the cube keeps the row with the most recent date for every combination of keys (the last one in case of ties),

  • DataCube.data is a copy of the C++ data converted into a dataframe, modifying it has no impact on the cube,

  • the column levels of a view follow the order of the keys.