Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: to_sql not properly escaping column names postgresql #40183

Open
2 of 3 tasks
mbkupfer opened this issue Mar 2, 2021 · 0 comments
Open
2 of 3 tasks

BUG: to_sql not properly escaping column names postgresql #40183

mbkupfer opened this issue Mar 2, 2021 · 0 comments
Labels
Bug IO SQL to_sql, read_sql, read_sql_query

Comments

@mbkupfer
Copy link
Contributor

mbkupfer commented Mar 2, 2021

  • I have checked that this issue has not already been reported.
    Although, this issue seems relevant: BUG/API: converting invalid column names in to_sql #6796

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

# Your code here
>>> from sqlalchemy import create_engine
>>> eng = create_engine(postgesql-dsn)

>>> pd.DataFrame({'(abc]': [1,2,3]}).to_sql('tbl', eng, if_exists='replace')
KeyError: '(abc])s'

>>> pd.DataFrame({'(abc)': [1,2,3]}).to_sql('tbl', eng, if_exists='replace')
KeyError: '(abc'

>>> pd.DataFrame({'[abc)': [1,2,3]}).to_sql('tbl', eng, if_exists='replace')
KeyError: '[abc'

Problem description

I noticed that to_sql throws an error when column names have a combination of ( and ]s in them. I didn't try this across all sql flavors, but I didn't run into issues with sqlite so I'm guessing this is tied to psycopg2 variable identifier substitution.

Expected Output

No KeyErrors

Output of pd.show_versions()

INSTALLED VERSIONS

commit : f2c8480
python : 3.8.5.final.0
python-bits : 64
OS : Linux
OS-release : 4.19.128-microsoft-standard
Version : #1 SMP Tue Jun 23 12:58:10 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.2.3
numpy : 1.19.4
pytz : 2020.4
dateutil : 2.8.1
pip : 20.0.2
setuptools : 45.2.0
Cython : 0.29.21
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.8.6 (dt dec pq3 ext lo64)
jinja2 : 2.10.1
IPython : 7.19.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
numba : None

@mbkupfer mbkupfer added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 2, 2021
@jbrockmendel jbrockmendel added IO SQL to_sql, read_sql, read_sql_query and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO SQL to_sql, read_sql, read_sql_query
Projects
None yet
Development

No branches or pull requests

2 participants