@@ -253,8 +253,8 @@ def uquery(sql, con=None, cur=None, retry=True, params=None):
253253#------------------------------------------------------------------------------
254254#--- Read and write to DataFrames
255255
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 ):
258258 """Read SQL database table into a DataFrame.
259259
260260 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
266266 Name of SQL table in database
267267 con : SQLAlchemy engine
268268 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).
270272 index_col : string, optional
271273 Column to set as index
272274 coerce_float : boolean, default True
@@ -299,16 +301,16 @@ def read_sql_table(table_name, con, schema=None, index_col=None, coerce_float=Tr
299301 "SQLAlchemy engines." )
300302 import sqlalchemy
301303 from sqlalchemy .schema import MetaData
302- meta = MetaData (con )
304+ meta = MetaData (con , schema = schema )
303305 try :
304- meta .reflect (only = [table_name ], schema = schema )
306+ meta .reflect (only = [table_name ])
305307 except sqlalchemy .exc .InvalidRequestError :
306308 raise ValueError ("Table %s not found" % table_name )
307309
308310 pandas_sql = PandasSQLAlchemy (con , meta = meta )
309311 table = pandas_sql .read_table (
310312 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 )
312314
313315 if table is not None :
314316 return table
@@ -456,7 +458,9 @@ def to_sql(frame, name, con, flavor='sqlite', schema=None, if_exists='fail',
456458 The flavor of SQL to use. Ignored when using SQLAlchemy engine.
457459 'mysql' is deprecated and will be removed in future versions, but it
458460 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).
460464 if_exists : {'fail', 'replace', 'append'}, default 'fail'
461465 - fail: If table exists, do nothing.
462466 - 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',
475479 if if_exists not in ('fail' , 'replace' , 'append' ):
476480 raise ValueError ("'{0}' is not valid for if_exists" .format (if_exists ))
477481
478- pandas_sql = pandasSQL_builder (con , flavor = flavor )
482+ pandas_sql = pandasSQL_builder (con , schema = schema , flavor = flavor )
479483
480484 if isinstance (frame , Series ):
481485 frame = frame .to_frame ()
@@ -503,14 +507,16 @@ def has_table(table_name, con, flavor='sqlite', schema=None):
503507 The flavor of SQL to use. Ignored when using SQLAlchemy engine.
504508 'mysql' is deprecated and will be removed in future versions, but it
505509 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).
507513
508514 Returns
509515 -------
510516 boolean
511517 """
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 )
514520
515521table_exists = has_table
516522
@@ -519,15 +525,15 @@ def has_table(table_name, con, flavor='sqlite', schema=None):
519525 "and will be removed in future versions. "
520526 "MySQL will be further supported with SQLAlchemy engines." )
521527
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 ):
523529 """
524530 Convenience function to return the correct PandasSQL subclass based on the
525531 provided parameters
526532 """
527533 # When support for DBAPI connections is removed,
528534 # is_cursor should not be necessary.
529535 if _is_sqlalchemy_engine (con ):
530- return PandasSQLAlchemy (con , meta = meta )
536+ return PandasSQLAlchemy (con , schema = schema , meta = meta )
531537 else :
532538 if flavor == 'mysql' :
533539 warnings .warn (_MYSQL_WARNING , FutureWarning )
@@ -836,11 +842,11 @@ class PandasSQLAlchemy(PandasSQL):
836842 using SQLAlchemy to handle DataBase abstraction
837843 """
838844
839- def __init__ (self , engine , meta = None ):
845+ def __init__ (self , engine , schema = None , meta = None ):
840846 self .engine = engine
841847 if not meta :
842848 from sqlalchemy .schema import MetaData
843- meta = MetaData (self .engine )
849+ meta = MetaData (self .engine , schema = schema )
844850
845851 self .meta = meta
846852
@@ -886,15 +892,17 @@ def tables(self):
886892 return self .meta .tables
887893
888894 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 )
890896
891897 def get_table (self , table_name , schema = None ):
898+ schema = schema or self .meta .schema
892899 if schema :
893900 return self .meta .tables .get ('.' .join ([schema , table_name ]))
894901 else :
895902 return self .meta .tables .get (table_name )
896903
897904 def drop_table (self , table_name , schema = None ):
905+ schema = schema or self .meta .schema
898906 if self .engine .has_table (table_name , schema ):
899907 self .meta .reflect (only = [table_name ], schema = schema )
900908 self .get_table (table_name , schema ).drop ()
@@ -1123,7 +1131,7 @@ def _fetchall_as_list(self, cur):
11231131 return result
11241132
11251133 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 ):
11271135 """
11281136 Write records stored in a DataFrame to a SQL database.
11291137
@@ -1143,7 +1151,7 @@ def to_sql(self, frame, name, if_exists='fail', index=True,
11431151 index_label = index_label )
11441152 table .insert (chunksize )
11451153
1146- def has_table (self , name ):
1154+ def has_table (self , name , schema = None ):
11471155 flavor_map = {
11481156 'sqlite' : ("SELECT name FROM sqlite_master "
11491157 "WHERE type='table' AND name='%s';" ) % name ,
@@ -1152,10 +1160,10 @@ def has_table(self, name):
11521160
11531161 return len (self .execute (query ).fetchall ()) > 0
11541162
1155- def get_table (self , table_name ):
1163+ def get_table (self , table_name , schema = None ):
11561164 return None # not supported in Legacy mode
11571165
1158- def drop_table (self , name ):
1166+ def drop_table (self , name , schema = None ):
11591167 drop_sql = "DROP TABLE %s" % name
11601168 self .execute (drop_sql )
11611169
0 commit comments