Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

BUG: The .to_sql() of dtype argument does not strictly ensure the column name and datatype #60737

Open
Labels
Bug Closing CandidateMay be closeable, needs more eyeballs IO SQLto_sql, read_sql, read_sql_query
@ammarsaf

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 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug Closing CandidateMay be closeable, needs more eyeballs IO SQLto_sql, read_sql, read_sql_query

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

        AltStyle によって変換されたページ (->オリジナル) /