-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: to_sql works only for strings #61385
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
Comments
The block in question should only be hit when pandas falls back to |
IG, There is no problem at all you can try this solution make you will Dtype correctly `import pandas as pd data = pd.DataFrame({ data_check_DateTime = pd.DataFrame({ data_double = pd.DataFrame({ Create a dummy SQLite engineengine = create_engine('sqlite:///:memory:') column_types_filtered_data = { Writing DataFrame to SQLdata.to_sql('my_table', con=engine, if_exists='replace', index=False, dtype=column_types_filtered_data) result_data = pd.read_sql('SELECT * FROM my_table', con=engine) column_types_filtered_data_datetime = {'timestamp': DateTime()} Verify the DateTime DataFrameresult_datetime = pd.read_sql('SELECT * FROM my_table_datetime', con=engine) Writing the DOUBLE-only DataFrame to SQLcolumn_types_filtered_data_double = {'value': DOUBLE()} Verify the DOUBLE DataFrameresult_double = pd.read_sql('SELECT * FROM my_table_double', con=engine) |
Pandas version checks Example DataFrame with timestamp, float, and string columnsdata = pd.DataFrame({ Using SQLAlchemy data typescolumn_types_filtered_data = {col: DOUBLE() for col in data.columns} This will raise an error if using a raw sqlite3 connectiondata.to_sql('my_table', con='sqlite3_connection_here', dtype=column_types_filtered_data) Issue Description When using a raw DB-API connection (e.g., sqlite3.connect()), passing a non-string type like sqlalchemy.types.DOUBLE() to to_sql(dtype=...) raises this error: ValueError: column_name (<sqlalchemy.sql.sqltypes.DOUBLE object>) not a string This happens due to the following code in pandas.io.sql: for col, my_type in dtype.items(): This block is only executed when pandas falls back to sqlite3 (or other DB-API connections without SQLAlchemy). Expected Behavior If SQLAlchemy types are unsupported with raw DB-API connections, this should be: Alternatively, a fallback mechanism could convert known SQLAlchemy types into SQL strings automatically. Update the documentation for to_sql() to specify: |
The API docs page says that the argument must be strings for sqlite3 legacy mode
The error message says the argument must be strings.
I'm negative on this - it is best that such logic is not owned by pandas. |
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
Issue Description
For any type other than str this block in pandas.io.sql will fail.
Expected Behavior
Different datatypes should be supported.
Installed Versions
pandas==2.2.3
The text was updated successfully, but these errors were encountered: