Skip to content

QST: Support of SQLAlchemy 2.0 in Pandas #40460

Closed
@mickaelandrieu

Description

@mickaelandrieu

After search in Stackoverflow and in your open and closed issues, I can't find any information about the support of SQL Alchemy 2.0 using pd.to_sql()


Question about pandas

An exemple is better than a talk.

from sqlalchemy import create_engine, MetaData, Table, Column, String, Integer
import pandas as pd

# Table creation
engine = create_engine(connection_string, echo=True, future=True) # This uses SQL Alchemy 2.0 API
metadata = MetaData()
category = Table(
        'category',
        metadata,
        Column('id', Integer, primary_key = True),
        Column('name', String(128), nullable = False),
        Column('slug', String(128), nullable = False)
    )

category.create(engine, checkfirst=True)

## Insert using pandas => FAIL
categories = pd.DataFrame([{'id' : 1, 'slug': 'test', 'name': 'Test'}])
categories.to_sql('category', engine, index=False)

This is the error I get from the CLI output :

  File "e:\Projects\test\database.py", line 127, in populate_category_table
    categories.to_sql('category', engine, index=False)
  File "E:\Projects\test\env\lib\site-packages\pandas\core\generic.py", 
line 2779, in to_sql
    sql.to_sql(
  File "E:\Projects\test\env\lib\site-packages\pandas\io\sql.py", line 590, in to_sql
    pandas_sql.to_sql(
  File "E:\Projects\test\env\lib\site-packages\pandas\io\sql.py", line 1392, in to_sql
    table.create()
  File "E:\Projects\test\env\lib\site-packages\pandas\io\sql.py", line 722, in create
    if self.exists():
  File "E:\Projects\test\env\lib\site-packages\pandas\io\sql.py", line 709, in exists
    return self.pd_sql.has_table(self.name, self.schema)
  File "E:\Projects\test\env\lib\site-packages\pandas\io\sql.py", line 1429, in has_table
    return self.connectable.run_callable(
  File "E:\Projects\test\env\lib\site-packages\sqlalchemy\future\engine.py", line 335, in _not_implemented
    raise NotImplementedError(
NotImplementedError: This method is not implemented for SQLAlchemy 2.0.

For information about the version of every dependency :

$ ./env/Scripts/pip list
Package         Version
--------------- ---------
beautifulsoup4  4.9.3
certifi         2020.12.5
chardet         4.0.0
esprima         4.0.1
greenlet        1.0.0
idna            2.10
mysqlclient     2.0.3
numpy           1.20.1
pandas          1.2.3
patreon         0.5.0
pip             21.0.1
python-dateutil 2.8.1
pytz            2021.1
requests        2.25.1
setuptools      49.2.1
six             1.15.0
soupsieve       2.2
SQLAlchemy      1.4.0
urllib3         1.26.3

Is there a workaround right now or should I avoid using pandas to insert the data into my database ?

Regards,

Metadata

Metadata

Assignees

No one assigned

    Labels

    DependenciesRequired and optional dependencies

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions