Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas read_sql with Parameters Gives `TypeError: dict is not a sequence` for PostgreSQL Database

I am trying to use parameters for SQL queries with Pandas:

import pandas as pd
from sqlalchemy import create_engine

engine = create_engine('postgresql:///mydb')
conn = engine.connect()
sql = '''
SELECT id, name, age FROM person WHERE name=%{name}s
'''

df = pd.read_sql(sql, conn, params={'name': 'John Doe'})
print(df)

From how I understand the Pandas documentation this should work. But it throws a ‘Type Error: dict is not a sequence’.

The solution is to wrap the SQL in text() from SQLAlchemy and use a different placeholder syntax:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import pandas as pd
from sqlalchemy import create_engine, text

engine = create_engine('postgresql:///mydb')
conn = engine.connect()
sql = '''
SELECT id, name, age FROM person WHERE name=:name
'''

df = pd.read_sql(text(sql), conn, params={'name': 'John Doe'})
print(df)

I am fine using text() because it makes the code also independent of the backend. But what is wrong with the first approach? IMHO, it exactly follows the docs.

>Solution :

It is expecting parenthesis not curly brackets:

import pandas as pd
from sqlalchemy import create_engine

engine = create_engine('postgresql:///mydb')
conn = engine.connect()
sql = '''
SELECT id, name, age FROM person WHERE name=%(name)s
'''

df = pd.read_sql(sql, conn, params={'name': 'John Doe'})
print(df)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading