@@ -3144,11 +3144,22 @@ DB-API <http://www.python.org/dev/peps/pep-0249/>`__.
3144
3144
See also some :ref: `cookbook examples <cookbook.sql >` for some advanced strategies.
3145
3145
3146
3146
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 `
3150
3147
3148
+ .. autosummary ::
3149
+ :toctree: generated/
3150
+
3151
+ read_sql_table
3152
+ read_sql_query
3153
+ read_sql
3154
+ DataFrame.to_sql
3151
3155
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
+
3152
3163
In the following example, we use the `SQlite <http://www.sqlite.org/ >`__ SQL database
3153
3164
engine. You can use a temporary SQLite database where data are stored in
3154
3165
"memory".
@@ -3160,7 +3171,7 @@ connecting to.
3160
3171
For more information on :func: `create_engine ` and the URI formatting, see the examples
3161
3172
below and the SQLAlchemy `documentation <http://docs.sqlalchemy.org/en/rel_0_9/core/engines.html >`__
3162
3173
3163
- .. code-block :: python
3174
+ .. ipython :: python
3164
3175
3165
3176
from sqlalchemy import create_engine
3166
3177
from pandas.io import sql
@@ -3171,8 +3182,7 @@ Writing DataFrames
3171
3182
~~~~~~~~~~~~~~~~~~
3172
3183
3173
3184
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 `.
3176
3186
3177
3187
+-----+------------+-------+-------+-------+
3178
3188
| id | Date | Col_1 | Col_2 | Col_3 |
@@ -3185,13 +3195,6 @@ the database using :func:`~pandas.io.sql.to_sql`.
3185
3195
+-----+------------+-------+-------+-------+
3186
3196
3187
3197
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
-
3195
3198
.. ipython :: python
3196
3199
:suppress:
3197
3200
@@ -3202,44 +3205,47 @@ the database using :func:`~pandas.io.sql.to_sql`.
3202
3205
(63 , datetime.datetime(2010 ,10 ,20 ), ' Z' , 5.73 , True )]
3203
3206
3204
3207
data = DataFrame(d, columns = c)
3205
- sql.to_sql(data, ' data' , engine)
3208
+
3209
+ .. ipython :: python
3210
+
3211
+ data.to_sql(' data' , engine)
3206
3212
3207
3213
Reading Tables
3208
3214
~~~~~~~~~~~~~~
3209
3215
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
3211
3217
table name and optionally a subset of columns to read.
3212
3218
3213
3219
.. note ::
3214
3220
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
3216
3222
SQLAlchemy optional dependency installed.
3217
3223
3218
3224
.. ipython :: python
3219
3225
3220
- sql.read_table (' data' , engine)
3226
+ pd.read_sql_table (' data' , engine)
3221
3227
3222
3228
You can also specify the name of the column as the DataFrame index,
3223
3229
and specify a subset of columns to be read.
3224
3230
3225
3231
.. ipython :: python
3226
3232
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' ])
3229
3235
3230
3236
And you can explicitly force columns to be parsed as dates:
3231
3237
3232
3238
.. ipython :: python
3233
3239
3234
- sql.read_table (' data' , engine, parse_dates = [' Date' ])
3240
+ pd.read_sql_table (' data' , engine, parse_dates = [' Date' ])
3235
3241
3236
3242
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 `:
3238
3244
3239
3245
.. code-block :: python
3240
3246
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' }})
3243
3249
3244
3250
3245
3251
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.
3250
3256
Querying
3251
3257
~~~~~~~~
3252
3258
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.
3254
3260
In this case you must use the SQL variant appropriate for your database.
3255
3261
When using SQLAlchemy, you can also pass SQLAlchemy Expression language constructs,
3256
3262
which are database-agnostic.
3257
3263
3258
3264
.. ipython :: python
3259
3265
3260
- sql.read_sql (' SELECT * FROM data' , engine)
3266
+ pd.read_sql_query (' SELECT * FROM data' , engine)
3261
3267
3262
3268
Of course, you can specify a more "complex" query.
3263
3269
3264
3270
.. ipython :: python
3265
3271
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)
3267
3273
3268
3274
3269
3275
You can also run a plain query without creating a dataframe with
@@ -3321,7 +3327,7 @@ you are using.
3321
3327
3322
3328
.. code-block :: python
3323
3329
3324
- sql .to_sql(data, ' data' , cnx, flavor = ' sqlite' )
3330
+ data .to_sql(' data' , cnx, flavor = ' sqlite' )
3325
3331
3326
3332
sql.read_sql(" SELECT * FROM data" , cnx, flavor = ' sqlite' )
3327
3333
0 commit comments