Skip to content

Commit 824dc0c

Browse files
Merge pull request #6298 from jorisvandenbossche/sql-docstring
DOC: clean-up docstrings of sql + fix doc build
2 parents cc52d9d + a88aa0a commit 824dc0c

File tree

2 files changed

+80
-61
lines changed

2 files changed

+80
-61
lines changed

doc/source/io.rst

+2
Original file line numberDiff line numberDiff line change
@@ -3142,12 +3142,14 @@ the database using :func:`~pandas.io.sql.to_sql`.
31423142
.. ipython:: python
31433143
:suppress:
31443144
3145+
import datetime
31453146
c = ['id', 'Date', 'Col_1', 'Col_2', 'Col_3']
31463147
d = [(26, datetime.datetime(2010,10,18), 'X', 27.5, True),
31473148
(42, datetime.datetime(2010,10,19), 'Y', -12.5, False),
31483149
(63, datetime.datetime(2010,10,20), 'Z', 5.73, True)]
31493150
31503151
data = DataFrame(d, columns=c)
3152+
sql.to_sql(data, 'data', engine)
31513153
31523154
Reading Tables
31533155
~~~~~~~~~~~~~~

pandas/io/sql.py

+78-61
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ def execute(sql, con, cur=None, params=None, flavor='sqlite'):
8080
8181
Parameters
8282
----------
83-
sql: string
83+
sql : string
8484
Query to be executed
85-
con: SQLAlchemy engine or DBAPI2 connection (legacy mode)
85+
con : SQLAlchemy engine or DBAPI2 connection (legacy mode)
8686
Using SQLAlchemy makes it possible to use any DB supported by that
8787
library.
8888
If a DBAPI2 object, a supported SQL flavor must also be provided
89-
cur: depreciated, cursor is obtained from connection
90-
params: list or tuple, optional
89+
cur : depreciated, cursor is obtained from connection
90+
params : list or tuple, optional
9191
List of parameters to pass to execute method.
9292
flavor : string "sqlite", "mysql"
9393
Specifies the flavor of SQL to use.
@@ -178,42 +178,46 @@ def read_sql(sql, con, index_col=None, flavor='sqlite', coerce_float=True,
178178
Returns a DataFrame corresponding to the result set of the query
179179
string.
180180
181-
Optionally provide an index_col parameter to use one of the
182-
columns as the index, otherwise default integer index will be used
181+
Optionally provide an `index_col` parameter to use one of the
182+
columns as the index, otherwise default integer index will be used.
183183
184184
Parameters
185185
----------
186-
sql: string
186+
sql : string
187187
SQL query to be executed
188-
con: SQLAlchemy engine or DBAPI2 connection (legacy mode)
188+
con : SQLAlchemy engine or DBAPI2 connection (legacy mode)
189189
Using SQLAlchemy makes it possible to use any DB supported by that
190190
library.
191191
If a DBAPI2 object is given, a supported SQL flavor must also be provided
192-
index_col: string, optional
192+
index_col : string, optional
193193
column name to use for the returned DataFrame object.
194-
flavor : string specifying the flavor of SQL to use. Ignored when using
194+
flavor : string, {'sqlite', 'mysql'}
195+
The flavor of SQL to use. Ignored when using
195196
SQLAlchemy engine. Required when using DBAPI2 connection.
196197
coerce_float : boolean, default True
197198
Attempt to convert values to non-string, non-numeric objects (like
198199
decimal.Decimal) to floating point, useful for SQL result sets
199-
cur: depreciated, cursor is obtained from connection
200-
params: list or tuple, optional
200+
cur : depreciated, cursor is obtained from connection
201+
params : list or tuple, optional
201202
List of parameters to pass to execute method.
202-
parse_dates: list or dict
203-
List of column names to parse as dates
204-
Or
205-
Dict of {column_name: format string} where format string is
206-
strftime compatible in case of parsing string times or is one of
207-
(D, s, ns, ms, us) in case of parsing integer timestamps
208-
Or
209-
Dict of {column_name: arg dict}, where the arg dict corresponds
210-
to the keyword arguments of :func:`pandas.tseries.tools.to_datetime`
211-
Especially useful with databases without native Datetime support,
212-
such as SQLite
203+
parse_dates : list or dict
204+
- List of column names to parse as dates
205+
- Dict of ``{column_name: format string}`` where format string is
206+
strftime compatible in case of parsing string times or is one of
207+
(D, s, ns, ms, us) in case of parsing integer timestamps
208+
- Dict of ``{column_name: arg dict}``, where the arg dict corresponds
209+
to the keyword arguments of :func:`pandas.to_datetime`
210+
Especially useful with databases without native Datetime support,
211+
such as SQLite
213212
214213
Returns
215214
-------
216215
DataFrame
216+
217+
See also
218+
--------
219+
read_table
220+
217221
"""
218222
pandas_sql = pandasSQL_builder(con, flavor=flavor)
219223
return pandas_sql.read_sql(sql,
@@ -229,38 +233,43 @@ def to_sql(frame, name, con, flavor='sqlite', if_exists='fail', index=True):
229233
230234
Parameters
231235
----------
232-
frame: DataFrame
233-
name: name of SQL table
234-
con: SQLAlchemy engine or DBAPI2 connection (legacy mode)
236+
frame : DataFrame
237+
name : string
238+
Name of SQL table
239+
con : SQLAlchemy engine or DBAPI2 connection (legacy mode)
235240
Using SQLAlchemy makes it possible to use any DB supported by that
236241
library.
237242
If a DBAPI2 object is given, a supported SQL flavor must also be provided
238-
flavor: {'sqlite', 'mysql', 'postgres'}, default 'sqlite'
239-
ignored when SQLAlchemy engine. Required when using DBAPI2 connection.
240-
if_exists: {'fail', 'replace', 'append'}, default 'fail'
241-
fail: If table exists, do nothing.
242-
replace: If table exists, drop it, recreate it, and insert data.
243-
append: If table exists, insert data. Create if does not exist.
243+
flavor : {'sqlite', 'mysql'}, default 'sqlite'
244+
The flavor of SQL to use. Ignored when using SQLAlchemy engine.
245+
Required when using DBAPI2 connection.
246+
if_exists : {'fail', 'replace', 'append'}, default 'fail'
247+
- fail: If table exists, do nothing.
248+
- replace: If table exists, drop it, recreate it, and insert data.
249+
- append: If table exists, insert data. Create if does not exist.
244250
index : boolean, default True
245-
Write DataFrame index as an column
251+
Write DataFrame index as a column
246252
"""
247253
pandas_sql = pandasSQL_builder(con, flavor=flavor)
248254
pandas_sql.to_sql(frame, name, if_exists=if_exists, index=index)
249255

250256

251257
def has_table(table_name, con, meta=None, flavor='sqlite'):
252258
"""
253-
Check if DB has named table
259+
Check if DataBase has named table.
254260
255261
Parameters
256262
----------
257-
frame: DataFrame
258-
name: name of SQL table
263+
table_name: string
264+
Name of SQL table
259265
con: SQLAlchemy engine or DBAPI2 connection (legacy mode)
260266
Using SQLAlchemy makes it possible to use any DB supported by that
261267
library.
262268
If a DBAPI2 object is given, a supported SQL flavor name must also be provided
263-
flavor: {'sqlite', 'mysql'}, default 'sqlite', ignored when using engine
269+
flavor: {'sqlite', 'mysql'}, default 'sqlite'
270+
The flavor of SQL to use. Ignored when using SQLAlchemy engine.
271+
Required when using DBAPI2 connection.
272+
264273
Returns
265274
-------
266275
boolean
@@ -272,33 +281,42 @@ def has_table(table_name, con, meta=None, flavor='sqlite'):
272281
def read_table(table_name, con, meta=None, index_col=None, coerce_float=True,
273282
parse_dates=None, columns=None):
274283
"""Given a table name and SQLAlchemy engine, return a DataFrame.
275-
Type convertions will be done automatically
284+
285+
Type convertions will be done automatically.
276286
277287
Parameters
278288
----------
279-
table_name: name of SQL table in database
280-
con: SQLAlchemy engine. Legacy mode not supported
281-
meta: SQLAlchemy meta, optional. If omitted MetaData is reflected from engine
282-
index_col: column to set as index, optional
289+
table_name : string
290+
Name of SQL table in database
291+
con : SQLAlchemy engine
292+
Legacy mode not supported
293+
meta : SQLAlchemy meta, optional
294+
If omitted MetaData is reflected from engine
295+
index_col : string, optional
296+
Column to set as index
283297
coerce_float : boolean, default True
284298
Attempt to convert values to non-string, non-numeric objects (like
285299
decimal.Decimal) to floating point. Can result in loss of Precision.
286-
parse_dates: list or dict
287-
List of column names to parse as dates
288-
Or
289-
Dict of {column_name: format string} where format string is
290-
strftime compatible in case of parsing string times or is one of
291-
(D, s, ns, ms, us) in case of parsing integer timestamps
292-
Or
293-
Dict of {column_name: arg dict}, where the arg dict corresponds
294-
to the keyword arguments of :func:`pandas.tseries.tools.to_datetime`
295-
Especially useful with databases without native Datetime support,
296-
such as SQLite
297-
columns: list
300+
parse_dates : list or dict
301+
- List of column names to parse as dates
302+
- Dict of ``{column_name: format string}`` where format string is
303+
strftime compatible in case of parsing string times or is one of
304+
(D, s, ns, ms, us) in case of parsing integer timestamps
305+
- Dict of ``{column_name: arg dict}``, where the arg dict corresponds
306+
to the keyword arguments of :func:`pandas.to_datetime`
307+
Especially useful with databases without native Datetime support,
308+
such as SQLite
309+
columns : list
298310
List of column names to select from sql table
311+
299312
Returns
300313
-------
301314
DataFrame
315+
316+
See also
317+
--------
318+
read_sql
319+
302320
"""
303321
pandas_sql = PandasSQLAlchemy(con, meta=meta)
304322
table = pandas_sql.read_table(table_name,
@@ -342,11 +360,12 @@ def pandasSQL_builder(con, flavor=None, meta=None):
342360

343361

344362
class PandasSQLTable(PandasObject):
345-
""" For mapping Pandas tables to SQL tables.
346-
Uses fact that table is reflected by SQLAlchemy to
347-
do better type convertions.
348-
Also holds various flags needed to avoid having to
349-
pass them between functions all the time.
363+
"""
364+
For mapping Pandas tables to SQL tables.
365+
Uses fact that table is reflected by SQLAlchemy to
366+
do better type convertions.
367+
Also holds various flags needed to avoid having to
368+
pass them between functions all the time.
350369
"""
351370
# TODO: support for multiIndex
352371
def __init__(self, name, pandas_sql_engine, frame=None, index=True,
@@ -556,7 +575,6 @@ def _numpy_type(self, sqltype):
556575

557576

558577
class PandasSQL(PandasObject):
559-
560578
"""
561579
Subclasses Should define read_sql and to_sql
562580
"""
@@ -571,7 +589,6 @@ def to_sql(self, *args, **kwargs):
571589

572590

573591
class PandasSQLAlchemy(PandasSQL):
574-
575592
"""
576593
This class enables convertion between DataFrame and SQL databases
577594
using SQLAlchemy to handle DataBase abstraction

0 commit comments

Comments
 (0)