1111import re
1212import numpy as np
1313
14+ import pandas .lib as lib
1415import pandas .core .common as com
1516from pandas .compat import lzip , map , zip , raise_with_traceback , string_types
1617from pandas .core .api import DataFrame , Series
@@ -689,13 +690,14 @@ def _get_column_names_and_types(self, dtype_mapper):
689690 if self .index is not None :
690691 for i , idx_label in enumerate (self .index ):
691692 idx_type = dtype_mapper (
692- self .frame .index .get_level_values (i ). dtype )
693+ self .frame .index .get_level_values (i ))
693694 column_names_and_types .append ((idx_label , idx_type ))
694695
695- column_names_and_types += zip (
696- list (map (str , self .frame .columns )),
697- map (dtype_mapper , self .frame .dtypes )
698- )
696+ column_names_and_types += [
697+ (str (self .frame .columns [i ]),
698+ dtype_mapper (self .frame .iloc [:,i ]))
699+ for i in range (len (self .frame .columns ))
700+ ]
699701 return column_names_and_types
700702
701703 def _create_table_statement (self ):
@@ -761,30 +763,33 @@ def _harmonize_columns(self, parse_dates=None):
761763 except KeyError :
762764 pass # this column not in results
763765
764- def _sqlalchemy_type (self , arr_or_dtype ):
766+ def _sqlalchemy_type (self , col ):
765767 from sqlalchemy .types import (BigInteger , Float , Text , Boolean ,
766- DateTime , Date , Interval )
768+ DateTime , Date , Time , Interval )
767769
768- if arr_or_dtype is date :
769- return Date
770- if com .is_datetime64_dtype (arr_or_dtype ):
770+ if com .is_datetime64_dtype (col ):
771771 try :
772- tz = arr_or_dtype .tzinfo
772+ tz = col .tzinfo
773773 return DateTime (timezone = True )
774774 except :
775775 return DateTime
776- if com .is_timedelta64_dtype (arr_or_dtype ):
776+ if com .is_timedelta64_dtype (col ):
777777 warnings .warn ("the 'timedelta' type is not supported, and will be "
778778 "written as integer values (ns frequency) to the "
779779 "database." , UserWarning )
780780 return BigInteger
781- elif com .is_float_dtype (arr_or_dtype ):
781+ elif com .is_float_dtype (col ):
782782 return Float
783- elif com .is_integer_dtype (arr_or_dtype ):
783+ elif com .is_integer_dtype (col ):
784784 # TODO: Refine integer size.
785785 return BigInteger
786- elif com .is_bool_dtype (arr_or_dtype ):
786+ elif com .is_bool_dtype (col ):
787787 return Boolean
788+ inferred = lib .infer_dtype (com ._ensure_object (col ))
789+ if inferred == 'date' :
790+ return Date
791+ if inferred == 'time' :
792+ return Time
788793 return Text
789794
790795 def _numpy_type (self , sqltype ):
@@ -913,7 +918,11 @@ def _create_sql_schema(self, frame, table_name):
913918 },
914919 'date' : {
915920 'mysql' : 'DATE' ,
916- 'sqlite' : 'TIMESTAMP' ,
921+ 'sqlite' : 'DATE' ,
922+ },
923+ 'time' : {
924+ 'mysql' : 'TIME' ,
925+ 'sqlite' : 'TIME' ,
917926 },
918927 'bool' : {
919928 'mysql' : 'BOOLEAN' ,
@@ -1019,8 +1028,8 @@ def _create_table_statement(self):
10191028 create_statement = template % {'name' : self .name , 'columns' : columns }
10201029 return create_statement
10211030
1022- def _sql_type_name (self , dtype ):
1023- pytype = dtype .type
1031+ def _sql_type_name (self , col ):
1032+ pytype = col . dtype .type
10241033 pytype_name = "text"
10251034 if issubclass (pytype , np .floating ):
10261035 pytype_name = "float"
@@ -1034,10 +1043,14 @@ def _sql_type_name(self, dtype):
10341043 elif issubclass (pytype , np .datetime64 ) or pytype is datetime :
10351044 # Caution: np.datetime64 is also a subclass of np.number.
10361045 pytype_name = "datetime"
1037- elif pytype is datetime .date :
1038- pytype_name = "date"
10391046 elif issubclass (pytype , np .bool_ ):
10401047 pytype_name = "bool"
1048+ elif issubclass (pytype , np .object ):
1049+ pytype = lib .infer_dtype (com ._ensure_object (col ))
1050+ if pytype == "date" :
1051+ pytype_name = "date"
1052+ elif pytype == "time" :
1053+ pytype_name = "time"
10411054
10421055 return _SQL_TYPES [pytype_name ][self .pd_sql .flavor ]
10431056
0 commit comments