Skip to main content

pandas-to-pydantic

WARNING: Library is currently unstable and in beta.

This library provides functions for converting Pandas Dataframes to Pydantic Models. This allows you to easily transform data in a table-like format into a json-like format. Pydantic Model annotations are matched with Pandas Dataframe columns. Supports models nested in lists.

PyPI - Version PyPI - Python Version


Table of Contents

Installation

pip install pandas-to-pydantic

Example 1

This example will show how to convert data from a flat structure (.csv file, pandas dataframe) to a hierarchical structure (json file, pydantic models)

Example Book Data

BookID Title AuthorName Genre PublishedYear
1 Harry Potter and the Philosopher's Stone J.K. Rowling Fantasy 1997
2 Harry Potter and the Chamber of Secrets J.K. Rowling Fantasy 1998
3 1984 George Orwell Dystopian Fiction 1949
4 Animal Farm George Orwell Political Satire 1945
5 Pride and Prejudice Jane Austen Romance 1813
7 Murder on the Orient Express Agatha Christie Mystery 1934
9 Adventures of Huckleberry Finn Mark Twain Adventure 1884
10 The Adventures of Tom Sawyer Mark Twain Adventure 1876
11 The Hobbit J.R.R. Tolkien Fantasy 1937
12 The Lord of the Rings J.R.R. Tolkien Fantasy 1954
import pandas as pd
from pydantic import BaseModel
from pandas_to_pydantic import dataframe_to_pydantic

# Declare pydantic models
class Book(BaseModel):
    BookID: int
    Title: str
    AuthorName: str
    Genre: str
    PublishedYear: int

# Update this to your your file path
book_data = pd.read_csv(FILE_PATH)

# Convert pandas dataframe to a pydantic root model
book_list_root = dataframe_to_pydantic(book_data, Book)

dataframe_to_pydantic returns a pydantic RootModel. Data can be accessed using its attributes and methods. https://docs.pydantic.dev/latest/api/root_model/

For example:

# Access data as a list of pydantic models
book_list_root.root

Returns (output shortened):

[Book(BookID=1, Title="Harry Potter and the Philosopher's Stone", AuthorName='J.K. Rowling', Genre='Fantasy', PublishedYear=1997),
Book(BookID=2, Title='Harry Potter and the Chamber of Secrets', AuthorName='J.K. Rowling', Genre='Fantasy', PublishedYear=1998),
Book(BookID=3, Title='1984', AuthorName='George Orwell', Genre='Dystopian Fiction', PublishedYear=1949),
...]

For example:

# Access data as a list of dict
book_list_root.model_dump()

Returns (output shortened):

[{'BookID': 1,
  'Title': "Harry Potter and the Philosopher's Stone",
  'AuthorName': 'J.K. Rowling',
  'Genre': 'Fantasy',
  'PublishedYear': 1997},
 {'BookID': 2,
  'Title': 'Harry Potter and the Chamber of Secrets',
  'AuthorName': 'J.K. Rowling',
  'Genre': 'Fantasy',
  'PublishedYear': 1998},
 {'BookID': 3,
  'Title': '1984',
  'AuthorName': 'George Orwell',
  'Genre': 'Dystopian Fiction',
  'PublishedYear': 1949},
...]

Example 2

In this example, Pydantic models are nested using the list type annotation. When there are multiple layers of nesting, unique id fields should be provided for each list field with a child model using id_column_map.

Here, the unique id column for the Genre model is Genre, and the unique id column for the Author model is AuthorName. Keys in id_column_map can be the model name or field name. Values in id_column_map are the unique column name.

For example:

class Book(BaseModel):
    BookID: int
    Title: str
    PublishedYear: int

class Author(BaseModel):
    AuthorName: str
    BookList: list[Book]

class Genre(BaseModel):
    Genre: str
    AuthorList: list[Author]

dataframe_to_pydantic(
    data=bookData,
    model=Genre,
    id_column_map={"Genre": "Genre", "AuthorList": "AuthorName"},
).model_dump()

Returns (output shortened)

[{'Genre': 'Fantasy',
  'AuthorList': [{'AuthorName': 'J.K. Rowling',
    'BookList': [{'BookID': 1,
      'Title': "Harry Potter and the Philosopher's Stone",
      'PublishedYear': 1997},
     {'BookID': 2,
      'Title': 'Harry Potter and the Chamber of Secrets',
      'PublishedYear': 1998}]},
   {'AuthorName': 'J.R.R. Tolkien',
    'BookList': [{'BookID': 11, 'Title': 'The Hobbit', 'PublishedYear': 1937},
     {'BookID': 12,
      'Title': 'The Lord of the Rings',
      'PublishedYear': 1954}]}]},
 {'Genre': 'Dystopian Fiction',
  'AuthorList': [{'AuthorName': 'George Orwell',
    'BookList': [{'BookID': 3, 'Title': '1984', 'PublishedYear': 1949}]}]},
...]

dataframe_to_pydantic

Args

  • data (pandas.DataFrame)
    • Dataframe with columns matching fields in the pydantic model
    • When the pydantic model includes nested models, it is assumed that the first column is unique. See Example 2
  • model (pydantic._internal._model_construction.ModelMetaClass)
    • Accepts classes created with pydantic.BaseModel
    • Supports nested models in lists
    • Annotation names must match columns in the dataframe
  • id_column_map(dict[str,str])
    • Required when nesting Pydantic models
    • Each key corresponds with field name or model name
    • Each value corresponds with the unique id column for the nested Pydantic model
    • For the parent level model, use the model name as key

Returns

Advanced Example

This example uses a larger data set with additional nesting.

Example Library Data

import pandas as pd
from pydantic import BaseModel
from pandas_to_pydantic import dataframe_to_pydantic

# Declare pydantic models
class LibaryDetail(BaseModel):
    LibraryName: str
    Location: str
    EstablishedYear: int
    BookCollectionSize: int

class Author(BaseModel):
    AuthorID: int
    AuthorName: str
    AuthorBirthdate: str

class Book(BaseModel):
    BookID: int
    Title: str
    Genre: str
    PublishedYear: int

class Library(BaseModel):
    LibraryID: int
    Detail: LibaryDetail
    AuthorList: list[Author]
    BookList: list[Book]

# Input data is a pandas dataframe
data = pd.read_csv(FILE_PATH)

# Convert pandas dataframe to a pydantic root model
library_list_root = dataframe_to_pydantic(
    data,
    Library,
    {
        "Library": "LibraryID",
        "BookList": "BookID",
        "AuthorList": "AuthorID",
    },
)

# Access data as a list of pydantic models
library_list_root.root

# Access data as a list of dict
library_list_root.model_dump()

Metadata

Release files for pandas-to-pydantic 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pandas-to-pydantic 0.1.5
File Size Uploaded Provenance Actions
pandas_to_pydantic-0.1.5.tar.gz 11.9 kB With provenance Details for pandas_to_pydantic-0.1.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pandas-to-pydantic 0.1.5
File Interpreter ABI Platform Actions
pandas_to_pydantic-0.1.5-py3-none-any.whl Python 3 none any Details for pandas_to_pydantic-0.1.5-py3-none-any.whl

Total release size: 19.1 kB

Release files / pandas_to_pydantic-0.1.5.tar.gz

Download URL pandas_to_pydantic-0.1.5.tar.gz
Size 11.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f58f5d814e23510e80052defd2979e984f942bb416be6ca17c972e4ff0a08d45
BLAKE2b-256 checksum
How to use checksums
a7f38ccc64624563cc84cf77dac858503abcbe65c187fd2f8922bd78c5323fe7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 30, 2025.

Transparency log

Release files / pandas_to_pydantic-0.1.5-py3-none-any.whl

Download URL pandas_to_pydantic-0.1.5-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e1830fc5590436398a9d342cfa10b3cafcfbdef8ba4555954854347e9a3b1a33
BLAKE2b-256 checksum
How to use checksums
3fbc3413f6827a7ba9172440aa7930a20b311e4cf81eae2b50716695f384a71e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 30, 2025.

Transparency log

Security for pandas-to-pydantic 0.1.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pandas_to_pydantic-0.1.5.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 30, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page