Skip to content

Allow SQLAlchemy types in DataFrame.to_sql dtype#1763

Open
SAY-5 wants to merge 2 commits into
pandas-dev:mainfrom
SAY-5:fix-to-sql-sqlalchemy-dtype
Open

Allow SQLAlchemy types in DataFrame.to_sql dtype#1763
SAY-5 wants to merge 2 commits into
pandas-dev:mainfrom
SAY-5:fix-to-sql-sqlalchemy-dtype

Conversation

@SAY-5

@SAY-5 SAY-5 commented Jun 6, 2026

Copy link
Copy Markdown

DataFrame.to_sql passes its dtype mapping straight through to SQLAlchemy, so the values can be SQLAlchemy types (either the class or an instance like NVARCHAR(length=1024)), but the stub only accepted DtypeArg. Widen the dtype parameter to also allow a column mapping of TypeEngine classes/instances.

Signed-off-by: Sai Asish Y <say.apm35@gmail.com>

@loicdiridollou loicdiridollou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the CI failures, please run poetry run poe test_all for checking the CI locally.
Added some comments in the code to guide you.

Comment thread pandas-stubs/core/generic.pyi Outdated
chunksize: int | None = None,
dtype: DtypeArg | None = None,
dtype: (
DtypeArg | Mapping[Hashable, type[TypeEngine[Any]] | TypeEngine[Any]] | None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you may need TypeEngineMixin instead.
Please try running tests locally with poetry run poe test_all for the full CI cycle or poetry run poe mypy for just mypy.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 140b790. TypeEngineMixin alone did not fix the dict case since mypy would not pick a context from a union with two separate Mapping members, so I merged them into a single Mapping and also allowed a bare sqlalchemy type since to_sql applies a scalar to all columns. poe test_all passes locally now.

Comment thread tests/test_io.py
"test",
con=engine,
dtype={
"a": sqlalchemy.types.VARCHAR(16),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You added the case for dict[str, TypeEngineMixin], to be complete you should also add for TypeEngineMixin so you test all the cases possible.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added scalar checks for both an instance and a class in 140b790.

Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
@SAY-5 SAY-5 force-pushed the fix-to-sql-sqlalchemy-dtype branch from a71f68e to 140b790 Compare June 9, 2026 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: dtype parameter of DataFrame.to_sql() rejects SQLAlchemy type instances

2 participants