From a4956b771ddd0a639bb3482df7c3bb4db0647fb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=B9i=20Ho=C3=A0ng=20T=C3=BA?= Date: Sun, 7 Apr 2024 07:59:06 +0000 Subject: [PATCH 1/2] fix SQLAlchemy doesn't accept raw str anymore --- pandasql/sqldf.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandasql/sqldf.py b/pandasql/sqldf.py index e25398a..8c5dac0 100644 --- a/pandasql/sqldf.py +++ b/pandasql/sqldf.py @@ -1,7 +1,7 @@ import inspect from contextlib import contextmanager from pandas.io.sql import to_sql, read_sql -from sqlalchemy import create_engine +from sqlalchemy import create_engine, text import re from warnings import catch_warnings, filterwarnings from sqlalchemy.exc import DatabaseError, ResourceClosedError @@ -89,7 +89,7 @@ def conn(self): def _init_connection(self, conn): if self.engine.name == 'postgresql': - conn.execute('set search_path to pg_temp') + conn.execute(text('set search_path to pg_temp')) def _set_text_factory(self, dbapi_con, connection_record): dbapi_con.text_factory = str From afb34455f02944659fe98048d7288f78c016702c Mon Sep 17 00:00:00 2001 From: bhtu Date: Sun, 7 Apr 2024 15:12:54 +0700 Subject: [PATCH 2/2] add postgresql example --- examples/demo_postgresql.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 examples/demo_postgresql.py diff --git a/examples/demo_postgresql.py b/examples/demo_postgresql.py new file mode 100644 index 0000000..408bdc1 --- /dev/null +++ b/examples/demo_postgresql.py @@ -0,0 +1,13 @@ +from pandas import DataFrame +from pandasql import PandaSQL + +# a dataframe +df1 = DataFrame({"a": [1, 2, 3, 1], "b": [7, 1, 5, 3]}) + +# establish postgresql connection using psycopg2 driver +# log in with username: tubh; password: tubh; using unix socket at /tmp +sqlClass = PandaSQL("postgresql+psycopg2://tubh:tubh@/tubh?host=/tmp") + +# try function GREATEST which is not supported by sqlite by is supported by postgresql +result = sqlClass(("select GREATEST(tbl.a, tbl.b) as maxA_B from tbl"), {"tbl": df1}) +print(result)