Convert Polars DataFrames to lists of Pydantic models with schema inference
Project description
Articuno
Convert Polars DataFrames into Pydantic models easily, with automatic schema inference including nested structs and nullable fields.
Installation
pip install articuno
Basic Usage
from articuno import df_to_pydantic, infer_pydantic_model
import polars as pl
df = pl.DataFrame({
"name": ["Alice", "Bob"],
"age": [30, 25],
"active": [True, False]
})
AutoModel = infer_pydantic_model(df)
models = df_to_pydantic(df, AutoModel)
for model in models:
print(model)
Handling Nested Structs
df = pl.DataFrame({
"user": [
{"name": "Alice", "address": {"city": "NY", "zip": 10001}},
{"name": "Bob", "address": {"city": "LA", "zip": 90001}},
]
})
Model = infer_pydantic_model(df)
instances = df_to_pydantic(df, Model)
print(instances[0].user.name) # Alice
print(instances[1].user.address.zip) # 90001
Nullable Fields
df = pl.DataFrame({
"name": ["Alice", None],
"age": [30, None]
}).with_columns([
pl.col("name").cast(pl.Utf8).set_nullable(True),
pl.col("age").cast(pl.Int32).set_nullable(True)
])
Model = infer_pydantic_model(df)
instances = df_to_pydantic(df, Model)
print(instances[0].name) # Alice
print(instances[1].name) # None
print(instances[1].age) # None
Using a Custom Pydantic Model
from pydantic import BaseModel
class Person(BaseModel):
name: str
age: int
df = pl.DataFrame({
"name": ["Alice", "Bob"],
"age": [30, 25],
})
people = df_to_pydantic(df, Person)
Supported Polars Types
- Numeric types:
Int8–Int64,UInt8–UInt64,Float32,Float64 - String:
Utf8 - Boolean
- Date and time:
Date,Datetime,Time,Duration - Complex types:
List,Struct - Other:
Decimal,Binary,Categorical,Enum,Null
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
articuno-0.1.0.tar.gz
(3.7 kB
view details)
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 articuno-0.1.0.tar.gz.
File metadata
- Download URL: articuno-0.1.0.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a85e8ac885d2a1094e7889d9bf747040c264b57b75e171f90dee3a567595c7f
|
|
| MD5 |
128f00e2b8dc4c3b54129c8e5c6b7e27
|
|
| BLAKE2b-256 |
edd798004b423043f715ffb5bbb9c50d2a2806c1273e505256801ebd1fecfcd5
|
File details
Details for the file articuno-0.1.0-py3-none-any.whl.
File metadata
- Download URL: articuno-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
12f04697b713cc25de99f646734a8f3ee03c5ba969946984ef18255b845effbb
|
|
| MD5 |
02cbb60e873f318f4e6b5cee8cc35c5e
|
|
| BLAKE2b-256 |
df8430efd547142e4da4eb7432403e2b12ed58ce4caa8673b59c54a5e50b057f
|