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
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_polars_tools-0.1.2.tar.gz.
File metadata
- Download URL: django_polars_tools-0.1.2.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
213b0d76cdc550d3d5179c1d2c11995c78cb04c0c2a369225ecccf58dccbc2d1
|
|
| MD5 |
223b779ac488c5d9c5eccbbaa97ea64b
|
|
| BLAKE2b-256 |
345a7a0927266bd6e98998a029b8cd21a2b24d846b2cc2c173bfe98da6eec120
|
File details
Details for the file django_polars_tools-0.1.2-py3-none-any.whl.
File metadata
- Download URL: django_polars_tools-0.1.2-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6aaf7d18df5c07da0aaaf3de689fa3c0b22a7075a6c0096dd24883493257cba7
|
|
| MD5 |
8d2ece4d8cdcc07c5fc62661a633f9f4
|
|
| BLAKE2b-256 |
d4c0061887c3ece6a8c0b74d32808c41afe5b84ac28152a110e89a8ad95846c1
|