@@ -253,8 +253,8 @@ def uquery(sql, con=None, cur=None, retry=True, params=None):
253
253
#------------------------------------------------------------------------------
254
254
#--- Read and write to DataFrames
255
255
256
- def read_sql_table (table_name , con , schema = None , index_col = None , coerce_float = True ,
257
- parse_dates = None , columns = None ):
256
+ def read_sql_table (table_name , con , schema = None , index_col = None ,
257
+ coerce_float = True , parse_dates = None , columns = None ):
258
258
"""Read SQL database table into a DataFrame.
259
259
260
260
Given a table name and an SQLAlchemy engine, returns a DataFrame.
@@ -266,7 +266,9 @@ def read_sql_table(table_name, con, schema=None, index_col=None, coerce_float=Tr
266
266
Name of SQL table in database
267
267
con : SQLAlchemy engine
268
268
Sqlite DBAPI connection mode not supported
269
- schema : Name of SQL schema in database.
269
+ schema : string, default None
270
+ Name of SQL schema in database to query (if database flavor supports this).
271
+ If None, use default schema (default).
270
272
index_col : string, optional
271
273
Column to set as index
272
274
coerce_float : boolean, default True
@@ -299,16 +301,16 @@ def read_sql_table(table_name, con, schema=None, index_col=None, coerce_float=Tr
299
301
"SQLAlchemy engines." )
300
302
import sqlalchemy
301
303
from sqlalchemy .schema import MetaData
302
- meta = MetaData (con )
304
+ meta = MetaData (con , schema = schema )
303
305
try :
304
- meta .reflect (only = [table_name ], schema = schema )
306
+ meta .reflect (only = [table_name ])
305
307
except sqlalchemy .exc .InvalidRequestError :
306
308
raise ValueError ("Table %s not found" % table_name )
307
309
308
310
pandas_sql = PandasSQLAlchemy (con , meta = meta )
309
311
table = pandas_sql .read_table (
310
312
table_name , index_col = index_col , coerce_float = coerce_float ,
311
- parse_dates = parse_dates , columns = columns , schema = schema )
313
+ parse_dates = parse_dates , columns = columns )
312
314
313
315
if table is not None :
314
316
return table
@@ -456,7 +458,9 @@ def to_sql(frame, name, con, flavor='sqlite', schema=None, if_exists='fail',
456
458
The flavor of SQL to use. Ignored when using SQLAlchemy engine.
457
459
'mysql' is deprecated and will be removed in future versions, but it
458
460
will be further supported through SQLAlchemy engines.
459
- schema : Name of SQL schema in database.
461
+ schema : string, default None
462
+ Name of SQL schema in database to write to (if database flavor supports
463
+ this). If None, use default schema (default).
460
464
if_exists : {'fail', 'replace', 'append'}, default 'fail'
461
465
- fail: If table exists, do nothing.
462
466
- replace: If table exists, drop it, recreate it, and insert data.
@@ -475,7 +479,7 @@ def to_sql(frame, name, con, flavor='sqlite', schema=None, if_exists='fail',
475
479
if if_exists not in ('fail' , 'replace' , 'append' ):
476
480
raise ValueError ("'{0}' is not valid for if_exists" .format (if_exists ))
477
481
478
- pandas_sql = pandasSQL_builder (con , flavor = flavor )
482
+ pandas_sql = pandasSQL_builder (con , schema = schema , flavor = flavor )
479
483
480
484
if isinstance (frame , Series ):
481
485
frame = frame .to_frame ()
@@ -503,14 +507,16 @@ def has_table(table_name, con, flavor='sqlite', schema=None):
503
507
The flavor of SQL to use. Ignored when using SQLAlchemy engine.
504
508
'mysql' is deprecated and will be removed in future versions, but it
505
509
will be further supported through SQLAlchemy engines.
506
- schema : Name of SQL schema in database.
510
+ schema : string, default None
511
+ Name of SQL schema in database to write to (if database flavor supports
512
+ this). If None, use default schema (default).
507
513
508
514
Returns
509
515
-------
510
516
boolean
511
517
"""
512
- pandas_sql = pandasSQL_builder (con , flavor = flavor )
513
- return pandas_sql .has_table (table_name , schema = schema )
518
+ pandas_sql = pandasSQL_builder (con , flavor = flavor , schema = schema )
519
+ return pandas_sql .has_table (table_name )
514
520
515
521
table_exists = has_table
516
522
@@ -519,15 +525,15 @@ def has_table(table_name, con, flavor='sqlite', schema=None):
519
525
"and will be removed in future versions. "
520
526
"MySQL will be further supported with SQLAlchemy engines." )
521
527
522
- def pandasSQL_builder (con , flavor = None , meta = None , is_cursor = False ):
528
+ def pandasSQL_builder (con , flavor = None , schema = None , meta = None , is_cursor = False ):
523
529
"""
524
530
Convenience function to return the correct PandasSQL subclass based on the
525
531
provided parameters
526
532
"""
527
533
# When support for DBAPI connections is removed,
528
534
# is_cursor should not be necessary.
529
535
if _is_sqlalchemy_engine (con ):
530
- return PandasSQLAlchemy (con , meta = meta )
536
+ return PandasSQLAlchemy (con , schema = schema , meta = meta )
531
537
else :
532
538
if flavor == 'mysql' :
533
539
warnings .warn (_MYSQL_WARNING , FutureWarning )
@@ -836,11 +842,11 @@ class PandasSQLAlchemy(PandasSQL):
836
842
using SQLAlchemy to handle DataBase abstraction
837
843
"""
838
844
839
- def __init__ (self , engine , meta = None ):
845
+ def __init__ (self , engine , schema = None , meta = None ):
840
846
self .engine = engine
841
847
if not meta :
842
848
from sqlalchemy .schema import MetaData
843
- meta = MetaData (self .engine )
849
+ meta = MetaData (self .engine , schema = schema )
844
850
845
851
self .meta = meta
846
852
@@ -886,15 +892,17 @@ def tables(self):
886
892
return self .meta .tables
887
893
888
894
def has_table (self , name , schema = None ):
889
- return self .engine .has_table (name , schema )
895
+ return self .engine .has_table (name , schema or self . meta . schema )
890
896
891
897
def get_table (self , table_name , schema = None ):
898
+ schema = schema or self .meta .schema
892
899
if schema :
893
900
return self .meta .tables .get ('.' .join ([schema , table_name ]))
894
901
else :
895
902
return self .meta .tables .get (table_name )
896
903
897
904
def drop_table (self , table_name , schema = None ):
905
+ schema = schema or self .meta .schema
898
906
if self .engine .has_table (table_name , schema ):
899
907
self .meta .reflect (only = [table_name ], schema = schema )
900
908
self .get_table (table_name , schema ).drop ()
@@ -1123,7 +1131,7 @@ def _fetchall_as_list(self, cur):
1123
1131
return result
1124
1132
1125
1133
def to_sql (self , frame , name , if_exists = 'fail' , index = True ,
1126
- index_label = None , chunksize = None ):
1134
+ index_label = None , schema = None , chunksize = None ):
1127
1135
"""
1128
1136
Write records stored in a DataFrame to a SQL database.
1129
1137
@@ -1143,7 +1151,7 @@ def to_sql(self, frame, name, if_exists='fail', index=True,
1143
1151
index_label = index_label )
1144
1152
table .insert (chunksize )
1145
1153
1146
- def has_table (self , name ):
1154
+ def has_table (self , name , schema = None ):
1147
1155
flavor_map = {
1148
1156
'sqlite' : ("SELECT name FROM sqlite_master "
1149
1157
"WHERE type='table' AND name='%s';" ) % name ,
@@ -1152,10 +1160,10 @@ def has_table(self, name):
1152
1160
1153
1161
return len (self .execute (query ).fetchall ()) > 0
1154
1162
1155
- def get_table (self , table_name ):
1163
+ def get_table (self , table_name , schema = None ):
1156
1164
return None # not supported in Legacy mode
1157
1165
1158
- def drop_table (self , name ):
1166
+ def drop_table (self , name , schema = None ):
1159
1167
drop_sql = "DROP TABLE %s" % name
1160
1168
self .execute (drop_sql )
1161
1169
0 commit comments