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

DEPR: Positional arguments in to_sql except name #54397

Merged
merged 16 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 10 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
10 changes: 5 additions & 5 deletions doc/source/user_guide/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5651,7 +5651,7 @@ the database using :func:`~pandas.DataFrame.to_sql`.
data = pd.DataFrame(d, columns=c)

data
data.to_sql("data", engine)
data.to_sql(name="data", con=engine)
mroeschke marked this conversation as resolved.
Show resolved Hide resolved

With some databases, writing large DataFrames can result in errors due to
packet size limitations being exceeded. This can be avoided by setting the
Expand All @@ -5660,7 +5660,7 @@ writes ``data`` to the database in batches of 1000 rows at a time:

.. ipython:: python

data.to_sql("data_chunked", engine, chunksize=1000)
data.to_sql(name="data_chunked", con=engine, chunksize=1000)

SQL data types
++++++++++++++
Expand All @@ -5680,7 +5680,7 @@ default ``Text`` type for string columns:

from sqlalchemy.types import String

data.to_sql("data_dtype", engine, dtype={"Col_1": String})
data.to_sql(name="data_dtype", con=engine, dtype={"Col_1": String})

.. note::

Expand Down Expand Up @@ -5849,7 +5849,7 @@ have schema's). For example:

.. code-block:: python

df.to_sql("table", engine, schema="other_schema")
df.to_sql(name="table", con=engine, schema="other_schema")
pd.read_sql_table("table", engine, schema="other_schema")

Querying
Expand All @@ -5876,7 +5876,7 @@ Specifying this will return an iterator through chunks of the query result:
.. ipython:: python

df = pd.DataFrame(np.random.randn(20, 3), columns=list("abc"))
df.to_sql("data_chunks", engine, index=False)
df.to_sql(name="data_chunks", con=engine, index=False)

.. ipython:: python

Expand Down
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.14.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ This ``engine`` can then be used to write or read data to/from this database:
.. ipython:: python

df = pd.DataFrame({'A': [1, 2, 3], 'B': ['a', 'b', 'c']})
df.to_sql('db_table', engine, index=False)
df.to_sql(name='db_table', con=engine, index=False)

You can read data from a database by specifying the table name:

Expand Down
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ Other enhancements
- :meth:`DataFrame.to_parquet` and :func:`read_parquet` will now write and read ``attrs`` respectively (:issue:`54346`)
- Added support for the DataFrame Consortium Standard (:issue:`54383`)
- Performance improvement in :meth:`GroupBy.quantile` (:issue:`51722`)
-

.. ---------------------------------------------------------------------------
.. _whatsnew_210.notable_bug_fixes:
Expand Down Expand Up @@ -560,6 +561,7 @@ Other Deprecations
- Deprecated the use of non-supported datetime64 and timedelta64 resolutions with :func:`pandas.array`. Supported resolutions are: "s", "ms", "us", "ns" resolutions (:issue:`53058`)
- Deprecated values "pad", "ffill", "bfill", "backfill" for :meth:`Series.interpolate` and :meth:`DataFrame.interpolate`, use ``obj.ffill()`` or ``obj.bfill()`` instead (:issue:`53581`)
- Deprecated the behavior of :meth:`Index.argmax`, :meth:`Index.argmin`, :meth:`Series.argmax`, :meth:`Series.argmin` with either all-NAs and skipna=True or any-NAs and skipna=False returning -1; in a future version this will raise ``ValueError`` (:issue:`33941`, :issue:`33942`)
- Updated ``con`` parameter for :meth:`DataFrame.to_sql` to be a keyword argument. (:issue:`54229`)
mroeschke marked this conversation as resolved.
Show resolved Hide resolved
-

.. ---------------------------------------------------------------------------
Expand Down
20 changes: 12 additions & 8 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@
SettingWithCopyWarning,
_chained_assignment_method_msg,
)
from pandas.util._decorators import doc
from pandas.util._decorators import (
deprecate_nonkeyword_arguments,
doc,
)
from pandas.util._exceptions import find_stack_level
from pandas.util._validators import (
check_dtype_backend,
Expand Down Expand Up @@ -2792,6 +2795,7 @@ def to_hdf(
)

@final
@deprecate_nonkeyword_arguments(version="3.0", allowed_args=["name"], name="to_sql")
mroeschke marked this conversation as resolved.
Show resolved Hide resolved
def to_sql(
self,
name: str,
Expand Down Expand Up @@ -2911,7 +2915,7 @@ def to_sql(
1 User 2
2 User 3

>>> df.to_sql('users', con=engine)
>>> df.to_sql(name='users', con=engine)
3
>>> from sqlalchemy import text
>>> with engine.connect() as conn:
Expand All @@ -2922,14 +2926,14 @@ def to_sql(

>>> with engine.begin() as connection:
... df1 = pd.DataFrame({'name' : ['User 4', 'User 5']})
... df1.to_sql('users', con=connection, if_exists='append')
... df1.to_sql(name='users', con=connection, if_exists='append')
2

This is allowed to support operations that require that the same
DBAPI connection is used for the entire operation.

>>> df2 = pd.DataFrame({'name' : ['User 6', 'User 7']})
>>> df2.to_sql('users', con=engine, if_exists='append')
>>> df2.to_sql(name='users', con=engine, if_exists='append')
2
>>> with engine.connect() as conn:
... conn.execute(text("SELECT * FROM users")).fetchall()
Expand All @@ -2939,7 +2943,7 @@ def to_sql(

Overwrite the table with just ``df2``.

>>> df2.to_sql('users', con=engine, if_exists='replace',
>>> df2.to_sql(name='users', con=engine, if_exists='replace',
... index_label='id')
2
>>> with engine.connect() as conn:
Expand All @@ -2956,7 +2960,7 @@ def to_sql(
... stmt = insert(table.table).values(data).on_conflict_do_nothing(index_elements=["a"])
... result = conn.execute(stmt)
... return result.rowcount
>>> df_conflict.to_sql("conflict_table", conn, if_exists="append", method=insert_on_conflict_nothing) # doctest: +SKIP
>>> df_conflict.to_sql(name="conflict_table", con=conn, if_exists="append", method=insert_on_conflict_nothing) # doctest: +SKIP
0

For MySQL, a callable to update columns ``b`` and ``c`` if there's a conflict
Expand All @@ -2973,7 +2977,7 @@ def to_sql(
... stmt = stmt.on_duplicate_key_update(b=stmt.inserted.b, c=stmt.inserted.c)
... result = conn.execute(stmt)
... return result.rowcount
>>> df_conflict.to_sql("conflict_table", conn, if_exists="append", method=insert_on_conflict_update) # doctest: +SKIP
>>> df_conflict.to_sql(name="conflict_table", con=conn, if_exists="append", method=insert_on_conflict_update) # doctest: +SKIP
2

Specify the dtype (especially useful for integers with missing values).
Expand All @@ -2989,7 +2993,7 @@ def to_sql(
2 2.0

>>> from sqlalchemy.types import Integer
>>> df.to_sql('integers', con=engine, index=False,
>>> df.to_sql(name='integers', con=engine, index=False,
... dtype={"A": Integer()})
3

Expand Down
2 changes: 1 addition & 1 deletion pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ def read_sql(
>>> conn = connect(':memory:')
>>> df = pd.DataFrame(data=[[0, '10/11/12'], [1, '12/11/10']],
... columns=['int_column', 'date_column'])
>>> df.to_sql('test_data', conn)
>>> df.to_sql(name='test_data', con=conn)
2

>>> pd.read_sql('SELECT int_column, date_column FROM test_data', conn)
Expand Down
Loading