question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I integrate or use SQLModel with Pandas?

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google “How to X in SQLModel” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

import pandas as pd


jobs_df = pd.read_csv('data/nyc-jobs.csv')

# SQL Model starts from here

Description

I would like to ingest Pandas DataFrame to SQL using the SQLModel. I also would like to retrieve data back using Pandas and SQLModel. Is there a way for it?

Operating System

Linux, Windows

Operating System Details

No response

SQLModel Version

0.0.6

Python Version

3.9

Additional Context

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

5reactions
johnziebrocommented, Apr 8, 2022

Came here for the sqmodel_to_df() function, thank you. I found that the created dataframe does not retain the column order, so here is a slight improvement which does using the first object’s schema attribute.

def sqlmodel_to_df(objects: List[SQLModel], set_index: bool = True) -> pd.DataFrame:
    """Converts SQLModel objects into a Pandas DataFrame.
    Usage
    ----------
    df = sqlmodel_to_df(list_of_sqlmodels)
    Parameters
    ----------
    :param objects: List[SQLModel]: List of SQLModel objects to be converted.
    :param set_index: bool: Sets the first column, usually the primary key, to dataframe index."""

    records = [obj.dict() for obj in objects]
    columns = list(objects[0].schema()["properties"].keys())
    df = pd.DataFrame.from_records(records, columns=columns)
    return df.set_index(columns[0]) if set_index else df
4reactions
SamEdwardescommented, Jan 7, 2022

If you want to convert a DataFrame back to a list of SQLModel objects you could do this.

from typing import List, Optional

import pandas as pd
from sqlmodel import Field, SQLModel


class User(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    email: str
    password: str
    name: str
    

# Create a few users for example
users = [
    User(email="sam@gmail.com", password="1234", name="sam"),
    User(email="jake@gmail.com", password="abcd", name="jake"),
    User(email="olivia@gmail.com", password="password", name="olivia"),
]


def sqmodel_to_df(objs: List[SQLModel]) -> pd.DataFrame:
    """Convert a SQLModel objects into a pandas DataFrame."""
    records = [i.dict() for i in objs]
    df = pd.DataFrame.from_records(records)
    return df

df = sqmodel_to_df(users)
print(df)

#      id             email  password    name
# 0  None     sam@gmail.com      1234     sam
# 1  None    jake@gmail.com      abcd    jake
# 2  None  olivia@gmail.com  password  Olivia

def df_to_sqlmodel(df: pd.DataFrame) -> List[SQLModel]:
    """Convert a pandas DataFrame into a a list of SQLModel objects."""    
    objs = [User(**row) for row in df.to_dict('records')]
    return objs

sql_model_objs = df_to_sqlmodel(df)
for i in sql_model_objs:
    print(i)
    
# id=None email='sam@gmail.com' password='1234' name='sam'
# id=None email='jake@gmail.com' password='abcd' name='jake'
# id=None email='olivia@gmail.com' password='password' name='olivia'

Not sure how to do this without a loop of some kind. Maybe you could come up with something that uses pandas.DataFrame.apply() but I am not sure if that would be faster than the list comprehension in my example.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL for Pythonistas - Python in Plain English
SQLModel, pandas.io.sql) use SQLAlchemy under the hood. Object relational mappers (ORMs) allow to interact with SQL databases using Python classes.
Read more >
SQLModel
SQLModel is a library for interacting with SQL databases from Python code, with Python objects. It is designed to be intuitive, easy to...
Read more >
Build a REST API with FastAPI and SQLModel - YouTube
Hey guys, In this I walk you through how to build a REST API with FastAPI and SQLModel. SQLModel is a library for...
Read more >
SQLModel is the Pydantic inspired Python ORM we ... - YouTube
The first 1000 people to use this link will get a 1 month free trial of Skillshare: https://skl.sh/johnwatsonrooney03221An ORM provides a ...
Read more >
GroupBy in SQL & Python: Comparison for Data Analysis | Mode
A GroupBy in Python is performed using the pandas library .groupby() function and a GroupBy in SQL is performed using an SQL GROUP...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found