Skip to main content

DataFusion in Python

Python test

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
from datafusion import functions as f
from datafusion import col
import pyarrow

# 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(
    col("a") + col("b"),
    col("a") - 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

from datafusion import udf

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

is_null_arr = udf(is_null, [pyarrow.int64()], pyarrow.bool_(), 'stable')

df = df.select(is_null_arr(col("a")))

result = df.collect()

assert result.column(0) == pyarrow.array([False] * 3)

UDAF

import pyarrow
import pyarrow.compute
from datafusion import udaf, Accumulator


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

    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 state(self) -> pyarrow.Array:
        return pyarrow.array([self._sum.as_py()])

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


df = ctx.create_dataframe([[batch]])

my_udaf = udaf(MyAccumulator, pyarrow.float64(), pyarrow.float64(), [pyarrow.float64()], 'stable')

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

result = df.collect()[0]

assert result.column(0) == pyarrow.array([6.0])

How to install (from pip)

pip install datafusion
# or
python -m pip install datafusion

You can verify the installation by running:

>>> import datafusion
>>> datafusion.__version__
'0.5.1'

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:datafusion-contrib/datafusion-python.git
# prepare development environment (used to build wheel / install in development)
python3 -m venv venv
# activate the venv
source venv/bin/activate
# update pip itself if necessary
python -m pip install -U pip
# install dependencies (for Python 3.8+)
python -m pip install -r requirements-310.txt

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

# make sure you activate the venv using "source venv/bin/activate" first
maturin develop
python -m pytest

How to update dependencies

To change test dependencies, change the requirements.in and run

# install pip-tools (this can be done only once), also consider running in venv
python -m pip install pip-tools
python -m piptools compile --generate-hashes -o requirements-310.txt

To update dependencies, run with -U

python -m piptools compile -U --generate-hashes -o requirements-310.txt

More details here

Metadata

Release files for datafusion 0.5.1

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.5.1
File Size Uploaded Provenance Actions
datafusion-0.5.1.tar.gz 80.9 kB Without provenance Details for datafusion-0.5.1.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for datafusion 0.5.1
File Actions
datafusion-0.5.1-cp36-abi3-win_amd64.whl CPython 3.6 abi3 Windows x86-64 Details for datafusion-0.5.1-cp36-abi3-win_amd64.whl
datafusion-0.5.1-cp36-abi3-manylinux_2_12_x86_64.manylinux2010_x86_64.whl CPython 3.6 abi3 Linux glibc 2.12+ x86-64 Details for datafusion-0.5.1-cp36-abi3-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
datafusion-0.5.1-cp36-abi3-macosx_11_0_arm64.whl CPython 3.6 abi3 macOS 11.0+ ARM64 Details for datafusion-0.5.1-cp36-abi3-macosx_11_0_arm64.whl
datafusion-0.5.1-cp36-abi3-macosx_10_7_x86_64.whl CPython 3.6 abi3 macOS 10.7+ x86-64 Details for datafusion-0.5.1-cp36-abi3-macosx_10_7_x86_64.whl

Total release size: 24.0 MB

Release files / datafusion-0.5.1.tar.gz

Download URL datafusion-0.5.1.tar.gz
Size 80.9 kB
Tags Source
SHA-256 checksum
How to use checksums
bb2989684cdfa487662985a4ea63567020a95d9f1a0191b37efef0db73513597
BLAKE2b-256 checksum
How to use checksums
9e55c070f0c15299af0e34dd696044ed8a893fe31a6edf635f9f380c26bbbe2d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

Release files / datafusion-0.5.1-cp36-abi3-win_amd64.whl

Download URL datafusion-0.5.1-cp36-abi3-win_amd64.whl
Size 5.9 MB
Tags CPython 3.6 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
70de3ec90989703ace9c4df40f5bd1e4d4a793dfd08cfc2004f61a60dd01a207
BLAKE2b-256 checksum
How to use checksums
fa345daf001a50225385cd97f69658a1fefd59c52d1f2b1d5f6d6492321f6661
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

Release files / datafusion-0.5.1-cp36-abi3-manylinux_2_12_x86_64.manylinux2010_x86_64.whl

Download URL datafusion-0.5.1-cp36-abi3-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Size 7.0 MB
Tags CPython 3.6 Linux glibc 2.12+ x86-64 abi3
SHA-256 checksum
How to use checksums
03d869fa21118bfae3e370e64718b90ec9f42ea537bfb83e222804cd58aeec4b
BLAKE2b-256 checksum
How to use checksums
039b2c689da56f53f13a521987e2da75da27b091fc35f73cc87e0d6ff152b798
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

Release files / datafusion-0.5.1-cp36-abi3-macosx_11_0_arm64.whl

Download URL datafusion-0.5.1-cp36-abi3-macosx_11_0_arm64.whl
Size 5.1 MB
Tags CPython 3.6 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
da2cd30a23c4f5a804c924671279691066a8f4f668e22b7d2b2859e5baded76e
BLAKE2b-256 checksum
How to use checksums
7804bf94e11d0d88bad5e093d001169044287f5fda9f0820dbd83e8a77314675
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

Release files / datafusion-0.5.1-cp36-abi3-macosx_10_7_x86_64.whl

Download URL datafusion-0.5.1-cp36-abi3-macosx_10_7_x86_64.whl
Size 6.0 MB
Tags CPython 3.6 abi3 macOS 10.7+ x86-64
SHA-256 checksum
How to use checksums
fa4d8d8d23e65b06dd51406e4c207c5abc1cc222d9b20c789c07b2413ff99810
BLAKE2b-256 checksum
How to use checksums
8595b83753faf1a22d705e7e35676c0f49303cbdf8c4feab502bff70e8cd11f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

Security for datafusion 0.5.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.5.1 This release

5 release files No provenance

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