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 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: 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

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.1.0.tar.gz (3.7 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.1.0-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

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

Hashes for articuno-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4a85e8ac885d2a1094e7889d9bf747040c264b57b75e171f90dee3a567595c7f
MD5 128f00e2b8dc4c3b54129c8e5c6b7e27
BLAKE2b-256 edd798004b423043f715ffb5bbb9c50d2a2806c1273e505256801ebd1fecfcd5

See more details on using hashes here.

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

Hashes for articuno-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 12f04697b713cc25de99f646734a8f3ee03c5ba969946984ef18255b845effbb
MD5 02cbb60e873f318f4e6b5cee8cc35c5e
BLAKE2b-256 df8430efd547142e4da4eb7432403e2b12ed58ce4caa8673b59c54a5e50b057f

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