Skip to main content

Tools for converting Django QuerySets to Polars DataFrames with correct schema handling, nullable field support, and high-performance data extraction.

Project description

django-polars-tools

Tests codecov PyPI version Python versions License: MIT

Utilities for integrating Django and Polars, including safe QuerySet → DataFrame conversion, correct schema inference, and nullable field handling.

This package solves the common issue where Polars incorrectly infers nullable fields when converting Django QuerySets, especially when using infer_schema_length. django_polars_tools provides reliable schema handling and high-performance data extraction from Django models.

🚀 Features

  • Safe QuerySet → Polars DataFrame conversion
  • Correct handling of nullable fields
  • Improved schema inference compared to Polars defaults
  • Fast extraction path for large querysets
  • Simple API designed to “just work”
  • Django-friendly, Polars-native

More features will be added as the project grows toward deeper Django ↔ Polars interoperability.

📦 Installation

pip install django-polars-tools

Or with uv:

uv add django-polars-tools

📚 Usage

Basic Example

Convert any Django QuerySet to a Polars DataFrame:

from django_polars_tools import django_queryset_to_dataframe
from myapp.models import MyModel

# Get a queryset
queryset = MyModel.objects.filter(active=True)

# Convert to Polars DataFrame
df = django_queryset_to_dataframe(queryset)

With Annotations

Works seamlessly with Django annotations:

from django.db.models import Count, F
from django_polars_tools import django_queryset_to_dataframe

queryset = MyModel.objects.annotate(
    total=Count('items'),
    display_name=F('first_name') + ' ' + F('last_name')
)

df = django_queryset_to_dataframe(queryset)

Using .values()

Also supports QuerySets with .values():

queryset = MyModel.objects.values('id', 'name', 'created_at')
df = django_queryset_to_dataframe(queryset)

Custom Field Mapping

Override the default Django → Polars type mapping:

import polars as pl
from django.db import models
from django_polars_tools import django_queryset_to_dataframe, DJANGO_MAPPING

# Create custom mapping
custom_mapping = DJANGO_MAPPING.copy()
custom_mapping[models.JSONField] = pl.Object  # Store JSON as Object instead of String

df = django_queryset_to_dataframe(queryset, mapping=custom_mapping)

Additional Polars Options

Pass any pl.read_database() kwargs:

df = django_queryset_to_dataframe(
    queryset,
    batch_size=10000,
    # Other polars.read_database options...
)

📝 Why this library?

Polars' schema inference works great for many cases, but with Django querysets it can:

  • infer nullable fields incorrectly
  • misclassify types with limited sample size

This library provides consistent handling tailored for the Django ORM.

🤝 Contributing

Contributions are welcome!

Open an issue or submit a PR if you’d like to help improve the project.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_polars_tools-0.1.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_polars_tools-0.1.1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file django_polars_tools-0.1.1.tar.gz.

File metadata

  • Download URL: django_polars_tools-0.1.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.27 {"installer":{"name":"uv","version":"0.9.27","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for django_polars_tools-0.1.1.tar.gz
Algorithm Hash digest
SHA256 86ae0fd2387265b06a2778a4968c683fbf3638009395ae4d03a3b67561c933df
MD5 351353e599e1eb12e9356e6f74440f22
BLAKE2b-256 78872236343fd2923b702f0ff708d0d4d08cc380aafd6d70381a689291cd58a9

See more details on using hashes here.

File details

Details for the file django_polars_tools-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: django_polars_tools-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.27 {"installer":{"name":"uv","version":"0.9.27","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for django_polars_tools-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 104146b19fd6a90da61ce9c6ad64d3d6a746cc891222e215988e123106b8d30f
MD5 ecf6a92ee820c88e612084cc82152b5c
BLAKE2b-256 5baca82de7933ecfd95deb52c3625fd61ccc4329b3436f37592ab61cfa0082d0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page