Skip to main content

DataFusion in Python

This is a Python library that binds to Apache Arrow in-memory query engine DataFusion.

Like pyspark, it allows you to build a plan through SQL or a DataFrame API against in-memory data, parquet or CSV files, run it in a multi-threaded environment, and obtain the result back in Python.

It also allows you to use UDFs and UDAFs for complex operations.

The major advantage of this library over other execution engines is that this library achieves zero-copy between Python and its execution engine: there is no cost in using UDFs, UDAFs, and collecting the results to Python apart from having to lock the GIL when running those operations.

Its query engine, DataFusion, is written in Rust, which makes strong assumptions about thread safety and lack of memory leaks.

Technically, zero-copy is achieved via the c data interface.

How to use it

Simple usage:

import datafusion
import pyarrow

# an alias
f = datafusion.functions

# create a context
ctx = datafusion.ExecutionContext()

# create a RecordBatch and a new DataFrame from it
batch = pyarrow.RecordBatch.from_arrays(
    [pyarrow.array([1, 2, 3]), pyarrow.array([4, 5, 6])],
    names=["a", "b"],
)
df = ctx.create_dataframe([[batch]])

# create a new statement
df = df.select(
    f.col("a") + f.col("b"),
    f.col("a") - f.col("b"),
)

# execute and collect the first (and only) batch
result = df.collect()[0]

assert result.column(0) == pyarrow.array([5, 7, 9])
assert result.column(1) == pyarrow.array([-3, -3, -3])

UDFs

def is_null(array: pyarrow.Array) -> pyarrow.Array:
    return array.is_null()

udf = f.udf(is_null, [pyarrow.int64()], pyarrow.bool_())

df = df.select(udf(f.col("a")))

UDAF

import pyarrow
import pyarrow.compute


class Accumulator:
    """
    Interface of a user-defined accumulation.
    """
    def __init__(self):
        self._sum = pyarrow.scalar(0.0)

    def to_scalars(self) -> [pyarrow.Scalar]:
        return [self._sum]

    def update(self, values: pyarrow.Array) -> None:
        # not nice since pyarrow scalars can't be summed yet. This breaks on `None`
        self._sum = pyarrow.scalar(self._sum.as_py() + pyarrow.compute.sum(values).as_py())

    def merge(self, states: pyarrow.Array) -> None:
        # not nice since pyarrow scalars can't be summed yet. This breaks on `None`
        self._sum = pyarrow.scalar(self._sum.as_py() + pyarrow.compute.sum(states).as_py())

    def evaluate(self) -> pyarrow.Scalar:
        return self._sum


df = ...

udaf = f.udaf(Accumulator, pyarrow.float64(), pyarrow.float64(), [pyarrow.float64()])

df = df.aggregate(
    [],
    [udaf(f.col("a"))]
)

How to install

pip install datafusion

How to develop

This assumes that you have rust and cargo installed. We use the workflow recommended by pyo3 and maturin.

Bootstrap:

# fetch this repo
git clone git@github.com:jorgecarleitao/datafusion-python.git

cd datafusion-python

# prepare development environment (used to build wheel / install in development)
python -m venv venv
venv/bin/pip install maturin==0.8.2 toml==0.10.1

Whenever rust code changes (your changes or via git pull):

venv/bin/maturin develop
venv/bin/python -m unittest discover tests

Release files for datafusion 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for datafusion 0.2.0
File Size Uploaded
datafusion-0.2.0.tar.gz 14.7 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for datafusion 0.2.0
File
datafusion-0.2.0-cp39-cp39-manylinux2010_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.12+ x86-64 Details
datafusion-0.2.0-cp38-none-win_amd64.whl CPython 3.8 none Windows x86-64 Details
datafusion-0.2.0-cp38-cp38-manylinux2010_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.12+ x86-64 Details
datafusion-0.2.0-cp38-cp38-macosx_10_7_x86_64.whl CPython 3.8 CPython 3.8 macOS 10.7+ x86-64 Details
datafusion-0.2.0-cp37-none-win_amd64.whl CPython 3.7 none Windows x86-64 Details
datafusion-0.2.0-cp37-cp37m-manylinux2010_x86_64.whl CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.12+ x86-64 Details
datafusion-0.2.0-cp37-cp37m-macosx_10_7_x86_64.whl CPython 3.7 CPython 3.7 pymalloc macOS 10.7+ x86-64 Details
datafusion-0.2.0-cp36-none-win_amd64.whl CPython 3.6 none Windows x86-64 Details
datafusion-0.2.0-cp36-cp36m-manylinux2010_x86_64.whl CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.12+ x86-64 Details
datafusion-0.2.0-cp36-cp36m-macosx_10_7_x86_64.whl CPython 3.6 CPython 3.6 pymalloc macOS 10.7+ x86-64 Details

Total release size: 41.6 MB

Release files / datafusion-0.2.0.tar.gz

Download URL datafusion-0.2.0.tar.gz
Size 14.7 kB
Tags Source
SHA-256 checksum
How to use checksums
3c1849319336771fffc4310f0cebeb6bce0937ffc4bb3e4a937943177bd0ab78
BLAKE2b-256 checksum
How to use checksums
50a7093c2785922dc16911702fea2d2e4dbdb66f800c61573ab5808d309a26b2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

Release files / datafusion-0.2.0-cp39-cp39-manylinux2010_x86_64.whl

Download URL datafusion-0.2.0-cp39-cp39-manylinux2010_x86_64.whl
Size 5.1 MB
Tags CPython 3.9 Linux glibc 2.12+ x86-64
SHA-256 checksum
How to use checksums
51ac8f97eb57855da00fa7c2796bee4fb260d438af0a8ebe891821adf2576932
BLAKE2b-256 checksum
How to use checksums
4b040a88a703a93532b8fab67197775263dba16044b00c71d8843de45c5020b2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

Release files / datafusion-0.2.0-cp38-none-win_amd64.whl

Download URL datafusion-0.2.0-cp38-none-win_amd64.whl
Size 3.5 MB
Tags CPython 3.8 Windows x86-64
SHA-256 checksum
How to use checksums
75547f95885a3c3b129a12899c4bb82947460e9ccc943c0b9e6d5ad883e4efbb
BLAKE2b-256 checksum
How to use checksums
b42c3b759e8a2528aa229490055198a81be8117356634c13ee6bd1872b389049
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

Release files / datafusion-0.2.0-cp38-cp38-manylinux2010_x86_64.whl

Download URL datafusion-0.2.0-cp38-cp38-manylinux2010_x86_64.whl
Size 5.1 MB
Tags CPython 3.8 Linux glibc 2.12+ x86-64
SHA-256 checksum
How to use checksums
9f48789bbacc5018143af054c437211078a65f96f03a9d6e4b8f7117e98e4bfe
BLAKE2b-256 checksum
How to use checksums
5a50b280ecd107c0a89811f4ba446c26dc8b37dbd19f3b13d2943e430bc572f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

Release files / datafusion-0.2.0-cp38-cp38-macosx_10_7_x86_64.whl

Download URL datafusion-0.2.0-cp38-cp38-macosx_10_7_x86_64.whl
Size 3.6 MB
Tags CPython 3.8 macOS 10.7+ x86-64
SHA-256 checksum
How to use checksums
59f34824be7daf4200420238340d6d40b26ce2187343c8edf279a00db94f9b27
BLAKE2b-256 checksum
How to use checksums
328a485b6e1092c4751c22c51e92a480184767315be9c702e9029c44093e1b92
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

Release files / datafusion-0.2.0-cp37-none-win_amd64.whl

Download URL datafusion-0.2.0-cp37-none-win_amd64.whl
Size 3.5 MB
Tags CPython 3.7 Windows x86-64
SHA-256 checksum
How to use checksums
1487750bd6325fbce85029257bb7bb942f58af34e68f64de3eef0cec54536c79
BLAKE2b-256 checksum
How to use checksums
b840b2a89b51a183f2babc412695d3e050ba638e70e7ba0e5849af29b7508979
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

Release files / datafusion-0.2.0-cp37-cp37m-manylinux2010_x86_64.whl

Download URL datafusion-0.2.0-cp37-cp37m-manylinux2010_x86_64.whl
Size 5.1 MB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.12+ x86-64
SHA-256 checksum
How to use checksums
4c80fff07ec8d7196ae67a9a43f36fea9529d608e560b84bc92abc439983048d
BLAKE2b-256 checksum
How to use checksums
9b5296a1d2b9524e4b3b16fccbe28b055ad48b2d27089fe486a2a41aec08ffa1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

Release files / datafusion-0.2.0-cp37-cp37m-macosx_10_7_x86_64.whl

Download URL datafusion-0.2.0-cp37-cp37m-macosx_10_7_x86_64.whl
Size 3.6 MB
Tags CPython 3.7 CPython 3.7 pymalloc macOS 10.7+ x86-64
SHA-256 checksum
How to use checksums
2d449c7e0ce995e2ae5d02de937f77034cfd25ef289025c4346f4a94b70efe28
BLAKE2b-256 checksum
How to use checksums
7c9f5f4eea7fc3ae87d5f71891619a48d79a4d39e966bab6a4bd530d0d78a600
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

Release files / datafusion-0.2.0-cp36-none-win_amd64.whl

Download URL datafusion-0.2.0-cp36-none-win_amd64.whl
Size 3.5 MB
Tags CPython 3.6 Windows x86-64
SHA-256 checksum
How to use checksums
fded7d46d26245a5f0474b43fd3c36262ad156f13ef4b79f4d851ea5fb5a2892
BLAKE2b-256 checksum
How to use checksums
9a3f91fcd37a31ed27df1bd49ee85593ebfb8d0ddad457ebc1358134b22acfa3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

Release files / datafusion-0.2.0-cp36-cp36m-manylinux2010_x86_64.whl

Download URL datafusion-0.2.0-cp36-cp36m-manylinux2010_x86_64.whl
Size 5.1 MB
Tags CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.12+ x86-64
SHA-256 checksum
How to use checksums
e17f0295bf6a9457e213da7ad7fba0938e33985723c160be33dc2d2d50fd9eec
BLAKE2b-256 checksum
How to use checksums
bbd993d865ca075f7e5792aae7bc360b72ceca3c3125d4b34b2a33525de1f8dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

Release files / datafusion-0.2.0-cp36-cp36m-macosx_10_7_x86_64.whl

Download URL datafusion-0.2.0-cp36-cp36m-macosx_10_7_x86_64.whl
Size 3.6 MB
Tags CPython 3.6 CPython 3.6 pymalloc macOS 10.7+ x86-64
SHA-256 checksum
How to use checksums
d47bfdb7e5e3bebf4b79904a230be70e16bef2c3b2245c9f986dfa46f03b9a85
BLAKE2b-256 checksum
How to use checksums
ee507d3d5e6e690b5843e8933ae2c09edfa642e05d04c3e614a311a7e5527747
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

Release history Release notifications | RSS feed

53.0.0

6 release files

52.3.0

6 release files

52.0.0

6 release files

50.1.0

6 release files

50.0.0

6 release files

49.0.0

6 release files

48.0.0

6 release files

47.0.0

6 release files

46.0.0

6 release files

45.2.0

6 release files

43.1.0

6 release files

42.0.0

6 release files

41.0.0

6 release files

40.1.0

5 release files

38.0.1

5 release files

37.1.0

5 release files

36.0.0

6 release files

33.0.0

6 release files

32.0.0

5 release files

31.0.0

6 release files

26.0.0

5 release files

25.0.0

5 release files

24.0.0

4 release files

23.0.0

5 release files

22.0.0

6 release files

20.0.0

4 release files

0.8.0

2 release files

0.7.0

4 release files

0.6.0

5 release files

0.5.2

5 release files

0.5.1

5 release files

0.5.0

5 release files

0.4.0

4 release files

This release

0.2.0 This release

11 release files

0.1.2

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page