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: The .to_sql() of dtype argument does not strictly ensure the column name and datatype #60737

Open
3 tasks done
ammarsaf opened this issue Jan 20, 2025 · 0 comments
Open
3 tasks done
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@ammarsaf
Copy link

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 types

dtypezzz = {  
        'date':types.Date(), 
        'bus_plates':types.Text(), 
        'driver_names':types.Text(), 
        'behavior':types.Text()
            }
df_bus.to_sql('mytable', 
            con=engine, schema='myschema', 
            if_exists='append', 
            index=False, 
            dtype=dtypezzz)

# df_bus have columns of bus_id and bus_plates

Issue Description

I spend time reading articles on how to ensure the datatype of dataframe is followed by the table datatype by using method .to_sql(). Most of it said that we can use the dtype argument in the method and put in a dict of column name and data type using sqlAlchemy from sqlalchemy import type.

However, I have tested this, to see if the method return an error by passing a dataframe that has totally different column name and datatype. My expectation is it should return an error, but instead of returning error due to wrong column name or data type, it allowed the dataframe to be inserted within the database table. This is very bad as I cannot ensure the data integrity within the table and because of the nature of this method that replace and alter the datatype, it could lead to many issue like wrongly inserted dataframe in the table and wrong datatype inserted without returning a useful error.

Expected Behavior

Expected behavior should be the to_sql() with dtype argument used will return error if dataframe has different column name/datatype to ensure the data integrity within the database table (this is possible in SQL as of we declare the datatype during the table creation)

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.10.16
python-bits : 64
OS : Linux
OS-release : 5.15.167.4-microsoft-standard-WSL2
Version : #1 SMP Tue Nov 5 00:21:55 UTC 2024
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.3
numpy : 2.2.2
pytz : 2024.2
dateutil : 2.9.0.post0
pip : 23.0.1
Cython : None
sphinx : None
IPython : 8.31.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : 2.9.10
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : 2.0.37
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2024.2
qtpy : None
pyqt5 : None

@ammarsaf ammarsaf added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

1 participant