@@ -80,14 +80,14 @@ def execute(sql, con, cur=None, params=None, flavor='sqlite'):
80
80
81
81
Parameters
82
82
----------
83
- sql: string
83
+ sql : string
84
84
Query to be executed
85
- con: SQLAlchemy engine or DBAPI2 connection (legacy mode)
85
+ con : SQLAlchemy engine or DBAPI2 connection (legacy mode)
86
86
Using SQLAlchemy makes it possible to use any DB supported by that
87
87
library.
88
88
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
91
91
List of parameters to pass to execute method.
92
92
flavor : string "sqlite", "mysql"
93
93
Specifies the flavor of SQL to use.
@@ -178,42 +178,46 @@ def read_sql(sql, con, index_col=None, flavor='sqlite', coerce_float=True,
178
178
Returns a DataFrame corresponding to the result set of the query
179
179
string.
180
180
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.
183
183
184
184
Parameters
185
185
----------
186
- sql: string
186
+ sql : string
187
187
SQL query to be executed
188
- con: SQLAlchemy engine or DBAPI2 connection (legacy mode)
188
+ con : SQLAlchemy engine or DBAPI2 connection (legacy mode)
189
189
Using SQLAlchemy makes it possible to use any DB supported by that
190
190
library.
191
191
If a DBAPI2 object is given, a supported SQL flavor must also be provided
192
- index_col: string, optional
192
+ index_col : string, optional
193
193
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
195
196
SQLAlchemy engine. Required when using DBAPI2 connection.
196
197
coerce_float : boolean, default True
197
198
Attempt to convert values to non-string, non-numeric objects (like
198
199
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
201
202
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
213
212
214
213
Returns
215
214
-------
216
215
DataFrame
216
+
217
+ See also
218
+ --------
219
+ read_table
220
+
217
221
"""
218
222
pandas_sql = pandasSQL_builder (con , flavor = flavor )
219
223
return pandas_sql .read_sql (sql ,
@@ -229,38 +233,43 @@ def to_sql(frame, name, con, flavor='sqlite', if_exists='fail', index=True):
229
233
230
234
Parameters
231
235
----------
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)
235
240
Using SQLAlchemy makes it possible to use any DB supported by that
236
241
library.
237
242
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.
244
250
index : boolean, default True
245
- Write DataFrame index as an column
251
+ Write DataFrame index as a column
246
252
"""
247
253
pandas_sql = pandasSQL_builder (con , flavor = flavor )
248
254
pandas_sql .to_sql (frame , name , if_exists = if_exists , index = index )
249
255
250
256
251
257
def has_table (table_name , con , meta = None , flavor = 'sqlite' ):
252
258
"""
253
- Check if DB has named table
259
+ Check if DataBase has named table.
254
260
255
261
Parameters
256
262
----------
257
- frame: DataFrame
258
- name: name of SQL table
263
+ table_name: string
264
+ Name of SQL table
259
265
con: SQLAlchemy engine or DBAPI2 connection (legacy mode)
260
266
Using SQLAlchemy makes it possible to use any DB supported by that
261
267
library.
262
268
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
+
264
273
Returns
265
274
-------
266
275
boolean
@@ -272,33 +281,42 @@ def has_table(table_name, con, meta=None, flavor='sqlite'):
272
281
def read_table (table_name , con , meta = None , index_col = None , coerce_float = True ,
273
282
parse_dates = None , columns = None ):
274
283
"""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.
276
286
277
287
Parameters
278
288
----------
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
283
297
coerce_float : boolean, default True
284
298
Attempt to convert values to non-string, non-numeric objects (like
285
299
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
298
310
List of column names to select from sql table
311
+
299
312
Returns
300
313
-------
301
314
DataFrame
315
+
316
+ See also
317
+ --------
318
+ read_sql
319
+
302
320
"""
303
321
pandas_sql = PandasSQLAlchemy (con , meta = meta )
304
322
table = pandas_sql .read_table (table_name ,
@@ -342,11 +360,12 @@ def pandasSQL_builder(con, flavor=None, meta=None):
342
360
343
361
344
362
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.
350
369
"""
351
370
# TODO: support for multiIndex
352
371
def __init__ (self , name , pandas_sql_engine , frame = None , index = True ,
@@ -556,7 +575,6 @@ def _numpy_type(self, sqltype):
556
575
557
576
558
577
class PandasSQL (PandasObject ):
559
-
560
578
"""
561
579
Subclasses Should define read_sql and to_sql
562
580
"""
@@ -571,7 +589,6 @@ def to_sql(self, *args, **kwargs):
571
589
572
590
573
591
class PandasSQLAlchemy (PandasSQL ):
574
-
575
592
"""
576
593
This class enables convertion between DataFrame and SQL databases
577
594
using SQLAlchemy to handle DataBase abstraction
0 commit comments