Skip to content

Commit c73dc7f

Browse files
authored
Make pandas/io/sql.py work with sqlalchemy 2.0 (#48576)
1 parent 7ffc0ad commit c73dc7f

File tree

13 files changed

+188
-93
lines changed

13 files changed

+188
-93
lines changed

ci/deps/actions-310.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ dependencies:
4848
- pyxlsb
4949
- s3fs>=2021.08.0
5050
- scipy
51-
- sqlalchemy<1.4.46
51+
- sqlalchemy
5252
- tabulate
5353
- tzdata>=2022a
5454
- xarray

ci/deps/actions-311.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ dependencies:
4848
- pyxlsb
4949
- s3fs>=2021.08.0
5050
- scipy
51-
- sqlalchemy<1.4.46
51+
- sqlalchemy
5252
- tabulate
5353
- tzdata>=2022a
5454
- xarray

ci/deps/actions-38-downstream_compat.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ dependencies:
4848
- pyxlsb
4949
- s3fs>=2021.08.0
5050
- scipy
51-
- sqlalchemy<1.4.46
51+
- sqlalchemy
5252
- tabulate
5353
- xarray
5454
- xlrd

ci/deps/actions-38.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ dependencies:
4848
- pyxlsb
4949
- s3fs>=2021.08.0
5050
- scipy
51-
- sqlalchemy<1.4.46
51+
- sqlalchemy
5252
- tabulate
5353
- xarray
5454
- xlrd

ci/deps/actions-39.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ dependencies:
4848
- pyxlsb
4949
- s3fs>=2021.08.0
5050
- scipy
51-
- sqlalchemy<1.4.46
51+
- sqlalchemy
5252
- tabulate
5353
- tzdata>=2022a
5454
- xarray

ci/deps/circle-38-arm64.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ dependencies:
4949
- pyxlsb
5050
- s3fs>=2021.08.0
5151
- scipy
52-
- sqlalchemy<1.4.46
52+
- sqlalchemy
5353
- tabulate
5454
- xarray
5555
- xlrd

doc/source/user_guide/io.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5868,15 +5868,15 @@ If you have an SQLAlchemy description of your database you can express where con
58685868
sa.Column("Col_3", sa.Boolean),
58695869
)
58705870
5871-
pd.read_sql(sa.select([data_table]).where(data_table.c.Col_3 is True), engine)
5871+
pd.read_sql(sa.select(data_table).where(data_table.c.Col_3 is True), engine)
58725872
58735873
You can combine SQLAlchemy expressions with parameters passed to :func:`read_sql` using :func:`sqlalchemy.bindparam`
58745874

58755875
.. ipython:: python
58765876
58775877
import datetime as dt
58785878
5879-
expr = sa.select([data_table]).where(data_table.c.Date > sa.bindparam("date"))
5879+
expr = sa.select(data_table).where(data_table.c.Date > sa.bindparam("date"))
58805880
pd.read_sql(expr, engine, params={"date": dt.datetime(2010, 10, 18)})
58815881
58825882

doc/source/whatsnew/v2.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ Other enhancements
294294
- Added :meth:`DatetimeIndex.as_unit` and :meth:`TimedeltaIndex.as_unit` to convert to different resolutions; supported resolutions are "s", "ms", "us", and "ns" (:issue:`50616`)
295295
- Added :meth:`Series.dt.unit` and :meth:`Series.dt.as_unit` to convert to different resolutions; supported resolutions are "s", "ms", "us", and "ns" (:issue:`51223`)
296296
- Added new argument ``dtype`` to :func:`read_sql` to be consistent with :func:`read_sql_query` (:issue:`50797`)
297+
- Added support for SQLAlchemy 2.0 (:issue:`40686`)
297298
-
298299

299300
.. ---------------------------------------------------------------------------

environment.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ dependencies:
5151
- pyxlsb
5252
- s3fs>=2021.08.0
5353
- scipy
54-
- sqlalchemy<1.4.46
54+
- sqlalchemy
5555
- tabulate
5656
- tzdata>=2022a
5757
- xarray

pandas/core/generic.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2711,7 +2711,7 @@ def to_sql(
27112711
library. Legacy support is provided for sqlite3.Connection objects. The user
27122712
is responsible for engine disposal and connection closure for the SQLAlchemy
27132713
connectable. See `here \
2714-
<https://docs.sqlalchemy.org/en/14/core/connections.html>`_.
2714+
<https://docs.sqlalchemy.org/en/20/core/connections.html>`_.
27152715
If passing a sqlalchemy.engine.Connection which is already in a transaction,
27162716
the transaction will not be committed. If passing a sqlite3.Connection,
27172717
it will not be possible to roll back the record insertion.
@@ -2761,7 +2761,7 @@ def to_sql(
27612761
attribute of ``sqlite3.Cursor`` or SQLAlchemy connectable which may not
27622762
reflect the exact number of written rows as stipulated in the
27632763
`sqlite3 <https://docs.python.org/3/library/sqlite3.html#sqlite3.Cursor.rowcount>`__ or
2764-
`SQLAlchemy <https://docs.sqlalchemy.org/en/14/core/connections.html#sqlalchemy.engine.BaseCursorResult.rowcount>`__.
2764+
`SQLAlchemy <https://docs.sqlalchemy.org/en/20/core/connections.html#sqlalchemy.engine.CursorResult.rowcount>`__.
27652765
27662766
.. versionadded:: 1.4.0
27672767
@@ -2805,7 +2805,9 @@ def to_sql(
28052805
28062806
>>> df.to_sql('users', con=engine)
28072807
3
2808-
>>> engine.execute("SELECT * FROM users").fetchall()
2808+
>>> from sqlalchemy import text
2809+
>>> with engine.connect() as conn:
2810+
... conn.execute(text("SELECT * FROM users")).fetchall()
28092811
[(0, 'User 1'), (1, 'User 2'), (2, 'User 3')]
28102812
28112813
An `sqlalchemy.engine.Connection` can also be passed to `con`:
@@ -2821,7 +2823,8 @@ def to_sql(
28212823
>>> df2 = pd.DataFrame({'name' : ['User 6', 'User 7']})
28222824
>>> df2.to_sql('users', con=engine, if_exists='append')
28232825
2
2824-
>>> engine.execute("SELECT * FROM users").fetchall()
2826+
>>> with engine.connect() as conn:
2827+
... conn.execute(text("SELECT * FROM users")).fetchall()
28252828
[(0, 'User 1'), (1, 'User 2'), (2, 'User 3'),
28262829
(0, 'User 4'), (1, 'User 5'), (0, 'User 6'),
28272830
(1, 'User 7')]
@@ -2831,7 +2834,8 @@ def to_sql(
28312834
>>> df2.to_sql('users', con=engine, if_exists='replace',
28322835
... index_label='id')
28332836
2
2834-
>>> engine.execute("SELECT * FROM users").fetchall()
2837+
>>> with engine.connect() as conn:
2838+
... conn.execute(text("SELECT * FROM users")).fetchall()
28352839
[(0, 'User 6'), (1, 'User 7')]
28362840
28372841
Specify the dtype (especially useful for integers with missing values).
@@ -2851,7 +2855,8 @@ def to_sql(
28512855
... dtype={"A": Integer()})
28522856
3
28532857
2854-
>>> engine.execute("SELECT * FROM integers").fetchall()
2858+
>>> with engine.connect() as conn:
2859+
... conn.execute(text("SELECT * FROM integers")).fetchall()
28552860
[(1,), (None,), (2,)]
28562861
""" # noqa:E501
28572862
from pandas.io import sql

0 commit comments

Comments
 (0)