@@ -484,7 +484,7 @@ def read_sql(sql, con, index_col=None, coerce_float=True, params=None,
484484
485485
486486def 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 ):
488488 """
489489 Write records stored in a DataFrame to a SQL database.
490490
@@ -517,6 +517,8 @@ def to_sql(frame, name, con, flavor='sqlite', schema=None, if_exists='fail',
517517 chunksize : int, default None
518518 If not None, then rows will be written in batches of this size at a
519519 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.
520522
521523 """
522524 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',
531533
532534 pandas_sql .to_sql (frame , name , if_exists = if_exists , index = index ,
533535 index_label = index_label , schema = schema ,
534- chunksize = chunksize )
536+ chunksize = chunksize , dtype = dtype )
535537
536538
537539def has_table (table_name , con , flavor = 'sqlite' , schema = None ):
@@ -596,7 +598,7 @@ class SQLTable(PandasObject):
596598 # TODO: support for multiIndex
597599 def __init__ (self , name , pandas_sql_engine , frame = None , index = True ,
598600 if_exists = 'fail' , prefix = 'pandas' , index_label = None ,
599- schema = None , keys = None ):
601+ schema = None , keys = None , dtype = None ):
600602 self .name = name
601603 self .pd_sql = pandas_sql_engine
602604 self .prefix = prefix
@@ -605,6 +607,7 @@ def __init__(self, name, pandas_sql_engine, frame=None, index=True,
605607 self .schema = schema
606608 self .if_exists = if_exists
607609 self .keys = keys
610+ self .dtype = dtype
608611
609612 if frame is not None :
610613 # We want to initialize based on a dataframe
@@ -885,6 +888,10 @@ def _sqlalchemy_type(self, col):
885888 from sqlalchemy .types import (BigInteger , Float , Text , Boolean ,
886889 DateTime , Date , Time )
887890
891+ dtype = self .dtype or {}
892+ if col .name in dtype :
893+ return self .dtype [col .name ]
894+
888895 if com .is_datetime64_dtype (col ):
889896 try :
890897 tz = col .tzinfo
@@ -1099,7 +1106,7 @@ def read_query(self, sql, index_col=None, coerce_float=True,
10991106 read_sql = read_query
11001107
11011108 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 ):
11031110 """
11041111 Write records stored in a DataFrame to a SQL database.
11051112
@@ -1125,11 +1132,20 @@ def to_sql(self, frame, name, if_exists='fail', index=True,
11251132 chunksize : int, default None
11261133 If not None, then rows will be written in batches of this size at a
11271134 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+
11291138 """
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+
11301146 table = SQLTable (name , self , frame = frame , index = index ,
11311147 if_exists = if_exists , index_label = index_label ,
1132- schema = schema )
1148+ schema = schema , dtype = dtype )
11331149 table .create ()
11341150 table .insert (chunksize )
11351151 # check for potentially case sensitivity issues (GH7815)
@@ -1297,6 +1313,9 @@ def _create_table_setup(self):
12971313 return create_stmts
12981314
12991315 def _sql_type_name (self , col ):
1316+ dtype = self .dtype or {}
1317+ if col .name in dtype :
1318+ return dtype [col .name ]
13001319 pytype = col .dtype .type
13011320 pytype_name = "text"
13021321 if issubclass (pytype , np .floating ):
@@ -1424,7 +1443,7 @@ def _fetchall_as_list(self, cur):
14241443 return result
14251444
14261445 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 ):
14281447 """
14291448 Write records stored in a DataFrame to a SQL database.
14301449
@@ -1448,10 +1467,19 @@ def to_sql(self, frame, name, if_exists='fail', index=True,
14481467 chunksize : int, default None
14491468 If not None, then rows will be written in batches of this
14501469 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.
14511472
14521473 """
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+
14531480 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 )
14551483 table .create ()
14561484 table .insert (chunksize )
14571485
0 commit comments