@@ -484,7 +484,7 @@ def read_sql(sql, con, index_col=None, coerce_float=True, params=None,
484
484
485
485
486
486
def to_sql (frame , name , con , flavor = 'sqlite' , schema = None , if_exists = 'fail' ,
487
- index = True , index_label = None , chunksize = None ):
487
+ index = True , index_label = None , chunksize = None , dtype = None ):
488
488
"""
489
489
Write records stored in a DataFrame to a SQL database.
490
490
@@ -517,6 +517,8 @@ def to_sql(frame, name, con, flavor='sqlite', schema=None, if_exists='fail',
517
517
chunksize : int, default None
518
518
If not None, then rows will be written in batches of this size at a
519
519
time. If None, all rows will be written at once.
520
+ dtype : dictionary of column name to SQLAchemy type, default None
521
+ optional datatypes for SQL columns.
520
522
521
523
"""
522
524
if if_exists not in ('fail' , 'replace' , 'append' ):
@@ -531,7 +533,7 @@ def to_sql(frame, name, con, flavor='sqlite', schema=None, if_exists='fail',
531
533
532
534
pandas_sql .to_sql (frame , name , if_exists = if_exists , index = index ,
533
535
index_label = index_label , schema = schema ,
534
- chunksize = chunksize )
536
+ chunksize = chunksize , dtype = dtype )
535
537
536
538
537
539
def has_table (table_name , con , flavor = 'sqlite' , schema = None ):
@@ -596,7 +598,7 @@ class SQLTable(PandasObject):
596
598
# TODO: support for multiIndex
597
599
def __init__ (self , name , pandas_sql_engine , frame = None , index = True ,
598
600
if_exists = 'fail' , prefix = 'pandas' , index_label = None ,
599
- schema = None , keys = None ):
601
+ schema = None , keys = None , dtype = None ):
600
602
self .name = name
601
603
self .pd_sql = pandas_sql_engine
602
604
self .prefix = prefix
@@ -605,6 +607,7 @@ def __init__(self, name, pandas_sql_engine, frame=None, index=True,
605
607
self .schema = schema
606
608
self .if_exists = if_exists
607
609
self .keys = keys
610
+ self .dtype = dtype
608
611
609
612
if frame is not None :
610
613
# We want to initialize based on a dataframe
@@ -885,6 +888,10 @@ def _sqlalchemy_type(self, col):
885
888
from sqlalchemy .types import (BigInteger , Float , Text , Boolean ,
886
889
DateTime , Date , Time )
887
890
891
+ dtype = self .dtype or {}
892
+ if col .name in dtype :
893
+ return self .dtype [col .name ]
894
+
888
895
if com .is_datetime64_dtype (col ):
889
896
try :
890
897
tz = col .tzinfo
@@ -1099,7 +1106,7 @@ def read_query(self, sql, index_col=None, coerce_float=True,
1099
1106
read_sql = read_query
1100
1107
1101
1108
def to_sql (self , frame , name , if_exists = 'fail' , index = True ,
1102
- index_label = None , schema = None , chunksize = None ):
1109
+ index_label = None , schema = None , chunksize = None , dtype = None ):
1103
1110
"""
1104
1111
Write records stored in a DataFrame to a SQL database.
1105
1112
@@ -1125,11 +1132,20 @@ def to_sql(self, frame, name, if_exists='fail', index=True,
1125
1132
chunksize : int, default None
1126
1133
If not None, then rows will be written in batches of this size at a
1127
1134
time. If None, all rows will be written at once.
1128
-
1135
+ dtype : dictionary of column name to SQLAlchemy type, default None
1136
+ Optional datatypes for SQL columns.
1137
+
1129
1138
"""
1139
+ if dtype is not None :
1140
+ import sqlalchemy .sql .type_api as type_api
1141
+ for col , my_type in dtype .items ():
1142
+ if not issubclass (my_type , type_api .TypeEngine ):
1143
+ raise ValueError ('The type of %s is not a SQLAlchemy '
1144
+ 'type ' % col )
1145
+
1130
1146
table = SQLTable (name , self , frame = frame , index = index ,
1131
1147
if_exists = if_exists , index_label = index_label ,
1132
- schema = schema )
1148
+ schema = schema , dtype = dtype )
1133
1149
table .create ()
1134
1150
table .insert (chunksize )
1135
1151
# check for potentially case sensitivity issues (GH7815)
@@ -1297,6 +1313,9 @@ def _create_table_setup(self):
1297
1313
return create_stmts
1298
1314
1299
1315
def _sql_type_name (self , col ):
1316
+ dtype = self .dtype or {}
1317
+ if col .name in dtype :
1318
+ return dtype [col .name ]
1300
1319
pytype = col .dtype .type
1301
1320
pytype_name = "text"
1302
1321
if issubclass (pytype , np .floating ):
@@ -1424,7 +1443,7 @@ def _fetchall_as_list(self, cur):
1424
1443
return result
1425
1444
1426
1445
def to_sql (self , frame , name , if_exists = 'fail' , index = True ,
1427
- index_label = None , schema = None , chunksize = None ):
1446
+ index_label = None , schema = None , chunksize = None , dtype = None ):
1428
1447
"""
1429
1448
Write records stored in a DataFrame to a SQL database.
1430
1449
@@ -1448,10 +1467,19 @@ def to_sql(self, frame, name, if_exists='fail', index=True,
1448
1467
chunksize : int, default None
1449
1468
If not None, then rows will be written in batches of this
1450
1469
size at a time. If None, all rows will be written at once.
1470
+ dtype : dictionary of column_name to SQLite string type, default None
1471
+ optional datatypes for SQL columns.
1451
1472
1452
1473
"""
1474
+ if dtype is not None :
1475
+ for col , my_type in dtype .items ():
1476
+ if not isinstance (my_type , str ):
1477
+ raise ValueError ('%s (%s) not a string' % (
1478
+ col , str (my_type )))
1479
+
1453
1480
table = SQLiteTable (name , self , frame = frame , index = index ,
1454
- if_exists = if_exists , index_label = index_label )
1481
+ if_exists = if_exists , index_label = index_label ,
1482
+ dtype = dtype )
1455
1483
table .create ()
1456
1484
table .insert (chunksize )
1457
1485
0 commit comments