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

DOC: fix EX03 Errors for pandas.DataFrame.to_sql #56866

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.errors.UndefinedVariableError \
pandas.read_json \
pandas.io.formats.style.Styler.to_latex \
pandas.read_parquet \
pandas.DataFrame.to_sql \
pandas.read_parquet
RET=$(($RET + $?)) ; echo $MSG "DONE"

fi
Expand Down
10 changes: 5 additions & 5 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2997,7 +2997,7 @@ def to_sql(
3
>>> from sqlalchemy import text
>>> with engine.connect() as conn:
... conn.execute(text("SELECT * FROM users")).fetchall()
... conn.execute(text("SELECT * FROM users")).fetchall()
[(0, 'User 1'), (1, 'User 2'), (2, 'User 3')]

An `sqlalchemy.engine.Connection` can also be passed to `con`:
Expand All @@ -3014,7 +3014,7 @@ def to_sql(
>>> df2.to_sql(name='users', con=engine, if_exists='append')
2
>>> with engine.connect() as conn:
... conn.execute(text("SELECT * FROM users")).fetchall()
... conn.execute(text("SELECT * FROM users")).fetchall()
[(0, 'User 1'), (1, 'User 2'), (2, 'User 3'),
(0, 'User 4'), (1, 'User 5'), (0, 'User 6'),
(1, 'User 7')]
Expand All @@ -3025,7 +3025,7 @@ def to_sql(
... index_label='id')
2
>>> with engine.connect() as conn:
... conn.execute(text("SELECT * FROM users")).fetchall()
... conn.execute(text("SELECT * FROM users")).fetchall()
[(0, 'User 6'), (1, 'User 7')]

Use ``method`` to define a callable insertion method to do nothing
Expand All @@ -3044,7 +3044,7 @@ def to_sql(
For MySQL, a callable to update columns ``b`` and ``c`` if there's a conflict
on a primary key.

>>> from sqlalchemy.dialects.mysql import insert
>>> from sqlalchemy.dialects.mysql import insert # doctest: +SKIP
Copy link
Member

Choose a reason for hiding this comment

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

Nice changes, just one comment. The error seems to suggest that insert is already imported before but never used. Can you have a look, and remove the previous import if that's really the case.

Copy link
Contributor Author

@erichxchen erichxchen Jan 16, 2024

Choose a reason for hiding this comment

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

It's actually used in L3036:

...     stmt = insert(table.table).values(data).on_conflict_do_nothing(index_elements=["a"])

I am not quite sure why it shows this error, but I think we could just # doctest: +SKIP this line

Copy link
Member

Choose a reason for hiding this comment

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

Ok, fair enough. Not sure if another option would be to remove this line. But having the skip is fine if there is no other obvious solution.

I'll resolve the conflict and let someone else review, so we have a second opinion, but this looks good to me.

>>> def insert_on_conflict_update(table, conn, keys, data_iter):
... # update columns "b" and "c" on primary key conflict
... data = [dict(zip(keys, row)) for row in data_iter]
Expand Down Expand Up @@ -3076,7 +3076,7 @@ def to_sql(
3

>>> with engine.connect() as conn:
... conn.execute(text("SELECT * FROM integers")).fetchall()
... conn.execute(text("SELECT * FROM integers")).fetchall()
[(1,), (None,), (2,)]
""" # noqa: E501
from pandas.io import sql
Expand Down
Loading