Skip to content

Commit 9681959

Browse files
Merge pull request #6867 from jorisvandenbossche/sql-api
API: update SQL functional api (GH6300)
2 parents 39b9f79 + f300279 commit 9681959

File tree

5 files changed

+248
-158
lines changed

5 files changed

+248
-158
lines changed

doc/source/api.rst

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,11 @@ SQL
7878

7979
.. autosummary::
8080
:toctree: generated/
81-
81+
82+
read_sql_table
83+
read_sql_query
8284
read_sql
8385

84-
.. currentmodule:: pandas.io.sql
85-
86-
.. autosummary::
87-
:toctree: generated/
88-
89-
read_frame
90-
write_frame
91-
9286
Google BigQuery
9387
~~~~~~~~~~~~~~~
9488
.. currentmodule:: pandas.io.gbq

doc/source/io.rst

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3144,11 +3144,22 @@ DB-API <http://www.python.org/dev/peps/pep-0249/>`__.
31443144
See also some :ref:`cookbook examples <cookbook.sql>` for some advanced strategies.
31453145

31463146
The key functions are:
3147-
:func:`~pandas.io.sql.to_sql`
3148-
:func:`~pandas.io.sql.read_sql`
3149-
:func:`~pandas.io.sql.read_table`
31503147

3148+
.. autosummary::
3149+
:toctree: generated/
3150+
3151+
read_sql_table
3152+
read_sql_query
3153+
read_sql
3154+
DataFrame.to_sql
31513155

3156+
.. note::
3157+
3158+
The function :func:`~pandas.read_sql` is a convenience wrapper around
3159+
:func:`~pandas.read_sql_table` and :func:`~pandas.read_sql_query` (and for
3160+
backward compatibility) and will delegate to specific function depending on
3161+
the provided input (database table name or sql query).
3162+
31523163
In the following example, we use the `SQlite <http://www.sqlite.org/>`__ SQL database
31533164
engine. You can use a temporary SQLite database where data are stored in
31543165
"memory".
@@ -3160,7 +3171,7 @@ connecting to.
31603171
For more information on :func:`create_engine` and the URI formatting, see the examples
31613172
below and the SQLAlchemy `documentation <http://docs.sqlalchemy.org/en/rel_0_9/core/engines.html>`__
31623173

3163-
.. code-block:: python
3174+
.. ipython:: python
31643175
31653176
from sqlalchemy import create_engine
31663177
from pandas.io import sql
@@ -3171,8 +3182,7 @@ Writing DataFrames
31713182
~~~~~~~~~~~~~~~~~~
31723183

31733184
Assuming the following data is in a DataFrame ``data``, we can insert it into
3174-
the database using :func:`~pandas.io.sql.to_sql`.
3175-
3185+
the database using :func:`~pandas.DataFrame.to_sql`.
31763186

31773187
+-----+------------+-------+-------+-------+
31783188
| id | Date | Col_1 | Col_2 | Col_3 |
@@ -3185,13 +3195,6 @@ the database using :func:`~pandas.io.sql.to_sql`.
31853195
+-----+------------+-------+-------+-------+
31863196

31873197

3188-
.. ipython:: python
3189-
:suppress:
3190-
3191-
from sqlalchemy import create_engine
3192-
from pandas.io import sql
3193-
engine = create_engine('sqlite:///:memory:')
3194-
31953198
.. ipython:: python
31963199
:suppress:
31973200
@@ -3202,44 +3205,47 @@ the database using :func:`~pandas.io.sql.to_sql`.
32023205
(63, datetime.datetime(2010,10,20), 'Z', 5.73, True)]
32033206
32043207
data = DataFrame(d, columns=c)
3205-
sql.to_sql(data, 'data', engine)
3208+
3209+
.. ipython:: python
3210+
3211+
data.to_sql('data', engine)
32063212
32073213
Reading Tables
32083214
~~~~~~~~~~~~~~
32093215

3210-
:func:`~pandas.io.sql.read_table` will read a databse table given the
3216+
:func:`~pandas.read_sql_table` will read a database table given the
32113217
table name and optionally a subset of columns to read.
32123218

32133219
.. note::
32143220

3215-
In order to use :func:`~pandas.io.sql.read_table`, you **must** have the
3221+
In order to use :func:`~pandas.read_sql_table`, you **must** have the
32163222
SQLAlchemy optional dependency installed.
32173223

32183224
.. ipython:: python
32193225
3220-
sql.read_table('data', engine)
3226+
pd.read_sql_table('data', engine)
32213227
32223228
You can also specify the name of the column as the DataFrame index,
32233229
and specify a subset of columns to be read.
32243230

32253231
.. ipython:: python
32263232
3227-
sql.read_table('data', engine, index_col='id')
3228-
sql.read_table('data', engine, columns=['Col_1', 'Col_2'])
3233+
pd.read_sql_table('data', engine, index_col='id')
3234+
pd.read_sql_table('data', engine, columns=['Col_1', 'Col_2'])
32293235
32303236
And you can explicitly force columns to be parsed as dates:
32313237

32323238
.. ipython:: python
32333239
3234-
sql.read_table('data', engine, parse_dates=['Date'])
3240+
pd.read_sql_table('data', engine, parse_dates=['Date'])
32353241
32363242
If needed you can explicitly specifiy a format string, or a dict of arguments
3237-
to pass to :func:`pandas.tseries.tools.to_datetime`.
3243+
to pass to :func:`pandas.to_datetime`:
32383244

32393245
.. code-block:: python
32403246
3241-
sql.read_table('data', engine, parse_dates={'Date': '%Y-%m-%d'})
3242-
sql.read_table('data', engine, parse_dates={'Date': {'format': '%Y-%m-%d %H:%M:%S'}})
3247+
pd.read_sql_table('data', engine, parse_dates={'Date': '%Y-%m-%d'})
3248+
pd.read_sql_table('data', engine, parse_dates={'Date': {'format': '%Y-%m-%d %H:%M:%S'}})
32433249
32443250
32453251
You can check if a table exists using :func:`~pandas.io.sql.has_table`
@@ -3250,20 +3256,20 @@ instantiated directly for more manual control over the SQL interaction.
32503256
Querying
32513257
~~~~~~~~
32523258

3253-
You can query using raw SQL in the :func:`~pandas.io.sql.read_sql` function.
3259+
You can query using raw SQL in the :func:`~pandas.read_sql_query` function.
32543260
In this case you must use the SQL variant appropriate for your database.
32553261
When using SQLAlchemy, you can also pass SQLAlchemy Expression language constructs,
32563262
which are database-agnostic.
32573263

32583264
.. ipython:: python
32593265
3260-
sql.read_sql('SELECT * FROM data', engine)
3266+
pd.read_sql_query('SELECT * FROM data', engine)
32613267
32623268
Of course, you can specify a more "complex" query.
32633269

32643270
.. ipython:: python
32653271
3266-
sql.read_frame("SELECT id, Col_1, Col_2 FROM data WHERE id = 42;", engine)
3272+
pd.read_sql_query("SELECT id, Col_1, Col_2 FROM data WHERE id = 42;", engine)
32673273
32683274
32693275
You can also run a plain query without creating a dataframe with
@@ -3321,7 +3327,7 @@ you are using.
33213327

33223328
.. code-block:: python
33233329
3324-
sql.to_sql(data, 'data', cnx, flavor='sqlite')
3330+
data.to_sql('data', cnx, flavor='sqlite')
33253331
33263332
sql.read_sql("SELECT * FROM data", cnx, flavor='sqlite')
33273333

pandas/io/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from pandas.io.pytables import HDFStore, Term, get_store, read_hdf
99
from pandas.io.json import read_json
1010
from pandas.io.html import read_html
11-
from pandas.io.sql import read_sql
11+
from pandas.io.sql import read_sql, read_sql_table, read_sql_query
1212
from pandas.io.stata import read_stata
1313
from pandas.io.pickle import read_pickle, to_pickle
1414
from pandas.io.packers import read_msgpack, to_msgpack

0 commit comments

Comments
 (0)