@@ -315,7 +315,7 @@ def read_sql_table(table_name, con, schema=None, index_col=None,
315
315
except sqlalchemy .exc .InvalidRequestError :
316
316
raise ValueError ("Table %s not found" % table_name )
317
317
318
- pandas_sql = PandasSQLAlchemy (con , meta = meta )
318
+ pandas_sql = SQLDatabase (con , meta = meta )
319
319
table = pandas_sql .read_table (
320
320
table_name , index_col = index_col , coerce_float = coerce_float ,
321
321
parse_dates = parse_dates , columns = columns )
@@ -374,7 +374,7 @@ def read_sql_query(sql, con, index_col=None, coerce_float=True, params=None,
374
374
375
375
"""
376
376
pandas_sql = pandasSQL_builder (con )
377
- return pandas_sql .read_sql (
377
+ return pandas_sql .read_query (
378
378
sql , index_col = index_col , params = params , coerce_float = coerce_float ,
379
379
parse_dates = parse_dates )
380
380
@@ -388,7 +388,7 @@ def read_sql(sql, con, index_col=None, coerce_float=True, params=None,
388
388
----------
389
389
sql : string
390
390
SQL query to be executed or database table name.
391
- con : SQLAlchemy engine or DBAPI2 connection (legacy mode)
391
+ con : SQLAlchemy engine or DBAPI2 connection (fallback mode)
392
392
Using SQLAlchemy makes it possible to use any DB supported by that
393
393
library.
394
394
If a DBAPI2 object, only sqlite3 is supported.
@@ -435,8 +435,8 @@ def read_sql(sql, con, index_col=None, coerce_float=True, params=None,
435
435
"""
436
436
pandas_sql = pandasSQL_builder (con )
437
437
438
- if isinstance (pandas_sql , PandasSQLLegacy ):
439
- return pandas_sql .read_sql (
438
+ if isinstance (pandas_sql , SQLiteDatabase ):
439
+ return pandas_sql .read_query (
440
440
sql , index_col = index_col , params = params ,
441
441
coerce_float = coerce_float , parse_dates = parse_dates )
442
442
@@ -451,7 +451,7 @@ def read_sql(sql, con, index_col=None, coerce_float=True, params=None,
451
451
sql , index_col = index_col , coerce_float = coerce_float ,
452
452
parse_dates = parse_dates , columns = columns )
453
453
else :
454
- return pandas_sql .read_sql (
454
+ return pandas_sql .read_query (
455
455
sql , index_col = index_col , params = params ,
456
456
coerce_float = coerce_float , parse_dates = parse_dates )
457
457
@@ -551,14 +551,14 @@ def pandasSQL_builder(con, flavor=None, schema=None, meta=None,
551
551
# When support for DBAPI connections is removed,
552
552
# is_cursor should not be necessary.
553
553
if _is_sqlalchemy_engine (con ):
554
- return PandasSQLAlchemy (con , schema = schema , meta = meta )
554
+ return SQLDatabase (con , schema = schema , meta = meta )
555
555
else :
556
556
if flavor == 'mysql' :
557
557
warnings .warn (_MYSQL_WARNING , FutureWarning )
558
- return PandasSQLLegacy (con , flavor , is_cursor = is_cursor )
558
+ return SQLiteDatabase (con , flavor , is_cursor = is_cursor )
559
559
560
560
561
- class PandasSQLTable (PandasObject ):
561
+ class SQLTable (PandasObject ):
562
562
"""
563
563
For mapping Pandas tables to SQL tables.
564
564
Uses fact that table is reflected by SQLAlchemy to
@@ -890,10 +890,24 @@ def to_sql(self, *args, **kwargs):
890
890
" or connection+sql flavor" )
891
891
892
892
893
- class PandasSQLAlchemy (PandasSQL ):
893
+ class SQLDatabase (PandasSQL ):
894
894
"""
895
895
This class enables convertion between DataFrame and SQL databases
896
896
using SQLAlchemy to handle DataBase abstraction
897
+
898
+ Parameters
899
+ ----------
900
+ engine : SQLAlchemy engine
901
+ Engine to connect with the database. Using SQLAlchemy makes it possible to use any DB supported by that
902
+ library.
903
+ schema : string, default None
904
+ Name of SQL schema in database to write to (if database flavor
905
+ supports this). If None, use default schema (default).
906
+ meta : SQLAlchemy MetaData object, default None
907
+ If provided, this MetaData object is used instead of a newly
908
+ created. This allows to specify database flavor specific
909
+ arguments in the MetaData object.
910
+
897
911
"""
898
912
899
913
def __init__ (self , engine , schema = None , meta = None ):
@@ -913,13 +927,86 @@ def execute(self, *args, **kwargs):
913
927
914
928
def read_table (self , table_name , index_col = None , coerce_float = True ,
915
929
parse_dates = None , columns = None , schema = None ):
916
- table = PandasSQLTable (
917
- table_name , self , index = index_col , schema = schema )
930
+ """Read SQL database table into a DataFrame.
931
+
932
+ Parameters
933
+ ----------
934
+ table_name : string
935
+ Name of SQL table in database
936
+ index_col : string, optional
937
+ Column to set as index
938
+ coerce_float : boolean, default True
939
+ Attempt to convert values to non-string, non-numeric objects (like
940
+ decimal.Decimal) to floating point. Can result in loss of Precision.
941
+ parse_dates : list or dict
942
+ - List of column names to parse as dates
943
+ - Dict of ``{column_name: format string}`` where format string is
944
+ strftime compatible in case of parsing string times or is one of
945
+ (D, s, ns, ms, us) in case of parsing integer timestamps
946
+ - Dict of ``{column_name: arg dict}``, where the arg dict corresponds
947
+ to the keyword arguments of :func:`pandas.to_datetime`
948
+ Especially useful with databases without native Datetime support,
949
+ such as SQLite
950
+ columns : list
951
+ List of column names to select from sql table
952
+ schema : string, default None
953
+ Name of SQL schema in database to query (if database flavor
954
+ supports this). If specified, this overwrites the default
955
+ schema of the SQLDatabase object.
956
+
957
+ Returns
958
+ -------
959
+ DataFrame
960
+
961
+ See also
962
+ --------
963
+ pandas.read_sql_table
964
+ SQLDatabase.read_query
965
+
966
+ """
967
+ table = SQLTable (table_name , self , index = index_col , schema = schema )
918
968
return table .read (coerce_float = coerce_float ,
919
969
parse_dates = parse_dates , columns = columns )
920
-
921
- def read_sql (self , sql , index_col = None , coerce_float = True ,
970
+
971
+ def read_query (self , sql , index_col = None , coerce_float = True ,
922
972
parse_dates = None , params = None ):
973
+ """Read SQL query into a DataFrame.
974
+
975
+ Parameters
976
+ ----------
977
+ sql : string
978
+ SQL query to be executed
979
+ index_col : string, optional
980
+ Column name to use as index for the returned DataFrame object.
981
+ coerce_float : boolean, default True
982
+ Attempt to convert values to non-string, non-numeric objects (like
983
+ decimal.Decimal) to floating point, useful for SQL result sets
984
+ params : list, tuple or dict, optional
985
+ List of parameters to pass to execute method. The syntax used
986
+ to pass parameters is database driver dependent. Check your
987
+ database driver documentation for which of the five syntax styles,
988
+ described in PEP 249's paramstyle, is supported.
989
+ Eg. for psycopg2, uses %(name)s so use params={'name' : 'value'}
990
+ parse_dates : list or dict
991
+ - List of column names to parse as dates
992
+ - Dict of ``{column_name: format string}`` where format string is
993
+ strftime compatible in case of parsing string times or is one of
994
+ (D, s, ns, ms, us) in case of parsing integer timestamps
995
+ - Dict of ``{column_name: arg dict}``, where the arg dict corresponds
996
+ to the keyword arguments of :func:`pandas.to_datetime`
997
+ Especially useful with databases without native Datetime support,
998
+ such as SQLite
999
+
1000
+ Returns
1001
+ -------
1002
+ DataFrame
1003
+
1004
+ See also
1005
+ --------
1006
+ read_sql_table : Read SQL database table into a DataFrame
1007
+ read_sql
1008
+
1009
+ """
923
1010
args = _convert_params (sql , params )
924
1011
925
1012
result = self .execute (* args )
@@ -935,12 +1022,41 @@ def read_sql(self, sql, index_col=None, coerce_float=True,
935
1022
data_frame .set_index (index_col , inplace = True )
936
1023
937
1024
return data_frame
1025
+
1026
+ read_sql = read_query
938
1027
939
1028
def to_sql (self , frame , name , if_exists = 'fail' , index = True ,
940
1029
index_label = None , schema = None , chunksize = None ):
941
- table = PandasSQLTable (
942
- name , self , frame = frame , index = index , if_exists = if_exists ,
943
- index_label = index_label , schema = schema )
1030
+ """
1031
+ Write records stored in a DataFrame to a SQL database.
1032
+
1033
+ Parameters
1034
+ ----------
1035
+ frame : DataFrame
1036
+ name : string
1037
+ Name of SQL table
1038
+ if_exists : {'fail', 'replace', 'append'}, default 'fail'
1039
+ - fail: If table exists, do nothing.
1040
+ - replace: If table exists, drop it, recreate it, and insert data.
1041
+ - append: If table exists, insert data. Create if does not exist.
1042
+ index : boolean, default True
1043
+ Write DataFrame index as a column
1044
+ index_label : string or sequence, default None
1045
+ Column label for index column(s). If None is given (default) and
1046
+ `index` is True, then the index names are used.
1047
+ A sequence should be given if the DataFrame uses MultiIndex.
1048
+ schema : string, default None
1049
+ Name of SQL schema in database to write to (if database flavor
1050
+ supports this). If specified, this overwrites the default
1051
+ schema of the SQLDatabase object.
1052
+ chunksize : int, default None
1053
+ If not None, then rows will be written in batches of this size at a
1054
+ time. If None, all rows will be written at once.
1055
+
1056
+ """
1057
+ table = SQLTable (name , self , frame = frame , index = index ,
1058
+ if_exists = if_exists , index_label = index_label ,
1059
+ schema = schema )
944
1060
table .create ()
945
1061
table .insert (chunksize )
946
1062
# check for potentially case sensitivity issues (GH7815)
@@ -972,8 +1088,7 @@ def drop_table(self, table_name, schema=None):
972
1088
self .meta .clear ()
973
1089
974
1090
def _create_sql_schema (self , frame , table_name , keys = None ):
975
- table = PandasSQLTable (table_name , self , frame = frame , index = False ,
976
- keys = keys )
1091
+ table = SQLTable (table_name , self , frame = frame , index = False , keys = keys )
977
1092
return str (table .sql_schema ())
978
1093
979
1094
@@ -1032,9 +1147,9 @@ def _create_sql_schema(self, frame, table_name, keys=None):
1032
1147
"underscores." )
1033
1148
1034
1149
1035
- class PandasSQLTableLegacy ( PandasSQLTable ):
1150
+ class SQLiteTable ( SQLTable ):
1036
1151
"""
1037
- Patch the PandasSQLTable for legacy support.
1152
+ Patch the SQLTable for fallback support.
1038
1153
Instead of a table variable just use the Create Table statement.
1039
1154
"""
1040
1155
@@ -1135,7 +1250,19 @@ def _sql_type_name(self, col):
1135
1250
return _SQL_TYPES [pytype_name ][self .pd_sql .flavor ]
1136
1251
1137
1252
1138
- class PandasSQLLegacy (PandasSQL ):
1253
+ class SQLiteDatabase (PandasSQL ):
1254
+ """
1255
+ Version of SQLDatabase to support sqlite connections (fallback without
1256
+ sqlalchemy). This should only be used internally.
1257
+
1258
+ For now still supports `flavor` argument to deal with 'mysql' database
1259
+ for backwards compatibility, but this will be removed in future versions.
1260
+
1261
+ Parameters
1262
+ ----------
1263
+ con : sqlite connection object
1264
+
1265
+ """
1139
1266
1140
1267
def __init__ (self , con , flavor , is_cursor = False ):
1141
1268
self .is_cursor = is_cursor
@@ -1181,7 +1308,7 @@ def execute(self, *args, **kwargs):
1181
1308
ex = DatabaseError ("Execution failed on sql '%s': %s" % (args [0 ], exc ))
1182
1309
raise_with_traceback (ex )
1183
1310
1184
- def read_sql (self , sql , index_col = None , coerce_float = True , params = None ,
1311
+ def read_query (self , sql , index_col = None , coerce_float = True , params = None ,
1185
1312
parse_dates = None ):
1186
1313
args = _convert_params (sql , params )
1187
1314
cursor = self .execute (* args )
@@ -1197,7 +1324,7 @@ def read_sql(self, sql, index_col=None, coerce_float=True, params=None,
1197
1324
if index_col is not None :
1198
1325
data_frame .set_index (index_col , inplace = True )
1199
1326
return data_frame
1200
-
1327
+
1201
1328
def _fetchall_as_list (self , cur ):
1202
1329
result = cur .fetchall ()
1203
1330
if not isinstance (result , list ):
@@ -1231,9 +1358,8 @@ def to_sql(self, frame, name, if_exists='fail', index=True,
1231
1358
size at a time. If None, all rows will be written at once.
1232
1359
1233
1360
"""
1234
- table = PandasSQLTableLegacy (
1235
- name , self , frame = frame , index = index , if_exists = if_exists ,
1236
- index_label = index_label )
1361
+ table = SQLiteTable (name , self , frame = frame , index = index ,
1362
+ if_exists = if_exists , index_label = index_label )
1237
1363
table .create ()
1238
1364
table .insert (chunksize )
1239
1365
@@ -1247,15 +1373,15 @@ def has_table(self, name, schema=None):
1247
1373
return len (self .execute (query ).fetchall ()) > 0
1248
1374
1249
1375
def get_table (self , table_name , schema = None ):
1250
- return None # not supported in Legacy mode
1376
+ return None # not supported in fallback mode
1251
1377
1252
1378
def drop_table (self , name , schema = None ):
1253
1379
drop_sql = "DROP TABLE %s" % name
1254
1380
self .execute (drop_sql )
1255
1381
1256
1382
def _create_sql_schema (self , frame , table_name , keys = None ):
1257
- table = PandasSQLTableLegacy (table_name , self , frame = frame ,
1258
- index = False , keys = keys )
1383
+ table = SQLiteTable (table_name , self , frame = frame , index = False ,
1384
+ keys = keys )
1259
1385
return str (table .sql_schema ())
1260
1386
1261
1387
0 commit comments