-
-
Notifications
You must be signed in to change notification settings - Fork 19.1k
Description
Pandas version checks
-
I have checked that this issue has not already been reported.
-
I have confirmed this bug exists on the latest version of pandas.
-
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
from sqlalchemy import create_engine
engine = create_engine("")
with engine.begin() as connection:
# Delete the table if it exists
connection.execute(text(f"DROP TABLE IF EXISTS schema.table"))
df = pd.DataFrame([{"test1": 5}])
# Write the DataFrame to the database
df.to_sql(name="table", con=connection, if_exists="append", schema="schema", index=False, chunksize=5000)
Issue Description
using sqlalchemy 2.0.38
using pandas 2.2.3
In the above example, i get the following error:
(psycopg2.errors.UndefinedColumn) column "test1" of relation "jobs_PDA" does not exist
LINE 1: INSERT INTO workflow_stage."jobs_PDA" (test1) VALUES (5)
Expected Behavior
Usually, df.to_sql with if_exists='append' will create a new table if one doesn't already exist. Since I am deleting the table in my transaction before i run df.to_sql, i expect the transaction to be commited with scheam.table being deleted, then recreated with column 'test1' int, and then populated with a single row {test1:5}.
Installed Versions
INSTALLED VERSIONS
commit : 0691c5c
python : 3.12.5
python-bits : 64
OS : Darwin
OS-release : 24.2.0
Version : Darwin Kernel Version 24.2.0: Fri Dec 6 18:56:34 PST 2024; root:xnu-11215.61.5~2/RELEASE_ARM64_T6020
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.2.3
numpy : 2.2.2
pytz : 2024.2
dateutil : 2.9.0.post0
pip : 24.2
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2024.12.0
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.5
lxml.etree : None
matplotlib : 3.10.0
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : 2.9.10
pymysql : 1.4.6
pyarrow : 19.0.0
pyreadstat : None
pytest : 8.3.4
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : 2.0.38
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.1
qtpy : None
pyqt5 : None