@@ -908,7 +908,7 @@ def _sqlalchemy_type(self, col):
908
908
909
909
col_type = self ._get_notnull_col_dtype (col )
910
910
911
- from sqlalchemy .types import (BigInteger , Float , Text , Boolean ,
911
+ from sqlalchemy .types import (BigInteger , Integer , Float , Text , Boolean ,
912
912
DateTime , Date , Time )
913
913
914
914
if col_type == 'datetime64' or col_type == 'datetime' :
@@ -923,10 +923,15 @@ def _sqlalchemy_type(self, col):
923
923
"database." , UserWarning )
924
924
return BigInteger
925
925
elif col_type == 'floating' :
926
- return Float
926
+ if col .dtype == 'float32' :
927
+ return Float (precision = 23 )
928
+ else :
929
+ return Float (precision = 53 )
927
930
elif col_type == 'integer' :
928
- # TODO: Refine integer size.
929
- return BigInteger
931
+ if col .dtype == 'int32' :
932
+ return Integer
933
+ else :
934
+ return BigInteger
930
935
elif col_type == 'boolean' :
931
936
return Boolean
932
937
elif col_type == 'date' :
@@ -1187,9 +1192,17 @@ def has_table(self, name, schema=None):
1187
1192
def get_table (self , table_name , schema = None ):
1188
1193
schema = schema or self .meta .schema
1189
1194
if schema :
1190
- return self .meta .tables .get ('.' .join ([schema , table_name ]))
1195
+ tbl = self .meta .tables .get ('.' .join ([schema , table_name ]))
1191
1196
else :
1192
- return self .meta .tables .get (table_name )
1197
+ tbl = self .meta .tables .get (table_name )
1198
+
1199
+ # Avoid casting double-precision floats into decimals
1200
+ from sqlalchemy import Numeric
1201
+ for column in tbl .columns :
1202
+ if isinstance (column .type , Numeric ):
1203
+ column .type .asdecimal = False
1204
+
1205
+ return tbl
1193
1206
1194
1207
def drop_table (self , table_name , schema = None ):
1195
1208
schema = schema or self .meta .schema
@@ -1198,8 +1211,9 @@ def drop_table(self, table_name, schema=None):
1198
1211
self .get_table (table_name , schema ).drop ()
1199
1212
self .meta .clear ()
1200
1213
1201
- def _create_sql_schema (self , frame , table_name , keys = None ):
1202
- table = SQLTable (table_name , self , frame = frame , index = False , keys = keys )
1214
+ def _create_sql_schema (self , frame , table_name , keys = None , dtype = None ):
1215
+ table = SQLTable (table_name , self , frame = frame , index = False , keys = keys ,
1216
+ dtype = dtype )
1203
1217
return str (table .sql_schema ())
1204
1218
1205
1219
@@ -1213,7 +1227,7 @@ def _create_sql_schema(self, frame, table_name, keys=None):
1213
1227
'sqlite' : 'TEXT' ,
1214
1228
},
1215
1229
'floating' : {
1216
- 'mysql' : 'FLOAT ' ,
1230
+ 'mysql' : 'DOUBLE ' ,
1217
1231
'sqlite' : 'REAL' ,
1218
1232
},
1219
1233
'integer' : {
@@ -1520,13 +1534,13 @@ def drop_table(self, name, schema=None):
1520
1534
drop_sql = "DROP TABLE %s" % name
1521
1535
self .execute (drop_sql )
1522
1536
1523
- def _create_sql_schema (self , frame , table_name , keys = None ):
1537
+ def _create_sql_schema (self , frame , table_name , keys = None , dtype = None ):
1524
1538
table = SQLiteTable (table_name , self , frame = frame , index = False ,
1525
- keys = keys )
1539
+ keys = keys , dtype = dtype )
1526
1540
return str (table .sql_schema ())
1527
1541
1528
1542
1529
- def get_schema (frame , name , flavor = 'sqlite' , keys = None , con = None ):
1543
+ def get_schema (frame , name , flavor = 'sqlite' , keys = None , con = None , dtype = None ):
1530
1544
"""
1531
1545
Get the SQL db table schema for the given frame.
1532
1546
@@ -1545,11 +1559,14 @@ def get_schema(frame, name, flavor='sqlite', keys=None, con=None):
1545
1559
Using SQLAlchemy makes it possible to use any DB supported by that
1546
1560
library.
1547
1561
If a DBAPI2 object, only sqlite3 is supported.
1562
+ dtype : dict of column name to SQL type, default None
1563
+ Optional specifying the datatype for columns. The SQL type should
1564
+ be a SQLAlchemy type, or a string for sqlite3 fallback connection.
1548
1565
1549
1566
"""
1550
1567
1551
1568
pandas_sql = pandasSQL_builder (con = con , flavor = flavor )
1552
- return pandas_sql ._create_sql_schema (frame , name , keys = keys )
1569
+ return pandas_sql ._create_sql_schema (frame , name , keys = keys , dtype = dtype )
1553
1570
1554
1571
1555
1572
# legacy names, with depreciation warnings and copied docs
0 commit comments