Skip to main content

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, nullable fields, and Pydantic class code generation.


Installation

pip install articuno

Basic Usage

import articuno
import polars as pl

df = pl.DataFrame({
    "name": ["Alice", "Bob"],
    "age": [30, 25],
    "active": [True, False]
})

AutoModel = articuno.infer_pydantic_model(df)
models = articuno.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 = articuno.infer_pydantic_model(df)
instances = articuno.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 = articuno.infer_pydantic_model(df)
instances = articuno.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: Int8Int64, UInt8–UInt64, Float32, Float64
  • String: Utf8
  • Boolean
  • Date and time: Date, Datetime, Time, Duration
  • Complex types: List, Struct
  • Other: Decimal, Binary, Categorical, Enum, Null

Generate Pydantic Class Code

Example Usage

from pydantic import create_model
import articuno

# Create a dynamic model
DynamicUser = create_model('DynamicUser', name=(str, ...), age=(int, 0))

# Generate the class code
code = articuno.generate_pydantic_class_code(DynamicUser)

print(code)

Output

from __future__ import annotations
from pydantic import BaseModel

class DynamicUser(BaseModel):
    name: str
    age: int = 0

Saving to a File

To write the generated code to a Python file:

articuno.generate_pydantic_class_code(DynamicUser, output_path="user_model.py")

Custom Class Name

To override the class name in the output (useful for renaming dynamic models):

articuno.generate_pydantic_class_code(DynamicUser, model_name="User")

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

articuno-0.2.0.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.

articuno-0.2.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file articuno-0.2.0.tar.gz.

File metadata

  • Download URL: articuno-0.2.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for articuno-0.2.0.tar.gz
Algorithm Hash digest
SHA256 eeaea6ef82e9fb1c25d702d2395d495ed26a3a1c0278f11262be55464bf08ac5
MD5 ac9ee324175c492d945d297717f9ef28
BLAKE2b-256 07a9a947dfaab8bc2d81fe6acf1a6d83d7d205a4c0caa9d112095ff2e05d3076

See more details on using hashes here.

File details

Details for the file articuno-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: articuno-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for articuno-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f219857c616e84bfbb87d30b2cb46ad001d3b8c715291f23250f7f2d132209ab
MD5 9503ffec5aa7926931ac390d54583447
BLAKE2b-256 1795084721d483673b22bf08391455b968c74b7c1f360b27b3275d5cf4c9b1da

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