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
@@ -684,13 +685,14 @@ def _get_column_names_and_types(self, dtype_mapper):
684685 if self .index is not None :
685686 for i , idx_label in enumerate (self .index ):
686687 idx_type = dtype_mapper (
687- self .frame .index .get_level_values (i ). dtype )
688+ self .frame .index .get_level_values (i ))
688689 column_names_and_types .append ((idx_label , idx_type ))
689690
690- column_names_and_types += zip (
691- list (map (str , self .frame .columns )),
692- map (dtype_mapper , self .frame .dtypes )
693- )
691+ column_names_and_types += [
692+ (str (self .frame .columns [i ]),
693+ dtype_mapper (self .frame .iloc [:,i ]))
694+ for i in range (len (self .frame .columns ))
695+ ]
694696 return column_names_and_types
695697
696698 def _create_table_statement (self ):
@@ -756,30 +758,33 @@ def _harmonize_columns(self, parse_dates=None):
756758 except KeyError :
757759 pass # this column not in results
758760
759- def _sqlalchemy_type (self , arr_or_dtype ):
761+ def _sqlalchemy_type (self , col ):
760762 from sqlalchemy .types import (BigInteger , Float , Text , Boolean ,
761- DateTime , Date , Interval )
763+ DateTime , Date , Time , Interval )
762764
763- if arr_or_dtype is date :
764- return Date
765- if com .is_datetime64_dtype (arr_or_dtype ):
765+ if com .is_datetime64_dtype (col ):
766766 try :
767- tz = arr_or_dtype .tzinfo
767+ tz = col .tzinfo
768768 return DateTime (timezone = True )
769769 except :
770770 return DateTime
771- if com .is_timedelta64_dtype (arr_or_dtype ):
771+ if com .is_timedelta64_dtype (col ):
772772 warnings .warn ("the 'timedelta' type is not supported, and will be "
773773 "written as integer values (ns frequency) to the "
774774 "database." , UserWarning )
775775 return BigInteger
776- elif com .is_float_dtype (arr_or_dtype ):
776+ elif com .is_float_dtype (col ):
777777 return Float
778- elif com .is_integer_dtype (arr_or_dtype ):
778+ elif com .is_integer_dtype (col ):
779779 # TODO: Refine integer size.
780780 return BigInteger
781- elif com .is_bool_dtype (arr_or_dtype ):
781+ elif com .is_bool_dtype (col ):
782782 return Boolean
783+ inferred = lib .infer_dtype (com ._ensure_object (col ))
784+ if inferred == 'date' :
785+ return Date
786+ if inferred == 'time' :
787+ return Time
783788 return Text
784789
785790 def _numpy_type (self , sqltype ):
@@ -908,7 +913,11 @@ def _create_sql_schema(self, frame, table_name):
908913 },
909914 'date' : {
910915 'mysql' : 'DATE' ,
911- 'sqlite' : 'TIMESTAMP' ,
916+ 'sqlite' : 'DATE' ,
917+ },
918+ 'time' : {
919+ 'mysql' : 'TIME' ,
920+ 'sqlite' : 'TIME' ,
912921 },
913922 'bool' : {
914923 'mysql' : 'BOOLEAN' ,
@@ -1014,8 +1023,8 @@ def _create_table_statement(self):
10141023 create_statement = template % {'name' : self .name , 'columns' : columns }
10151024 return create_statement
10161025
1017- def _sql_type_name (self , dtype ):
1018- pytype = dtype .type
1026+ def _sql_type_name (self , col ):
1027+ pytype = col . dtype .type
10191028 pytype_name = "text"
10201029 if issubclass (pytype , np .floating ):
10211030 pytype_name = "float"
@@ -1029,10 +1038,14 @@ def _sql_type_name(self, dtype):
10291038 elif issubclass (pytype , np .datetime64 ) or pytype is datetime :
10301039 # Caution: np.datetime64 is also a subclass of np.number.
10311040 pytype_name = "datetime"
1032- elif pytype is datetime .date :
1033- pytype_name = "date"
10341041 elif issubclass (pytype , np .bool_ ):
10351042 pytype_name = "bool"
1043+ elif issubclass (pytype , np .object ):
1044+ pytype = lib .infer_dtype (com ._ensure_object (col ))
1045+ if pytype == "date" :
1046+ pytype_name = "date"
1047+ elif pytype == "time" :
1048+ pytype_name = "time"
10361049
10371050 return _SQL_TYPES [pytype_name ][self .pd_sql .flavor ]
10381051
0 commit comments