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.0.tar.gz (4.9 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.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_polars_tools-0.1.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"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.0.tar.gz
Algorithm Hash digest
SHA256 69d2206e2cc97d053ce0c6bccc2c8b32c246a9d7fba874fb90548598002c4790
MD5 abeb904e3821557de0c277a6f8ba4859
BLAKE2b-256 2712e1b7aafb624d04ac33c15f3d7fe23eca42c69aede2e861170bbdda928bc7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_polars_tools-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1cb79b90b3d0cbafc8c89af0513865edba78ed2b73cdf2e3277ddb79acd73c09
MD5 a51bb433061e1320d419363bc826f2e2
BLAKE2b-256 fae17881dd2af9c2e22ac6dc85c7402e1d521935148fbdea70cf6d295b30c445

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