11
11
import re
12
12
import numpy as np
13
13
14
+ import pandas .lib as lib
14
15
import pandas .core .common as com
15
16
from pandas .compat import lzip , map , zip , raise_with_traceback , string_types
16
17
from pandas .core .api import DataFrame , Series
@@ -689,13 +690,14 @@ def _get_column_names_and_types(self, dtype_mapper):
689
690
if self .index is not None :
690
691
for i , idx_label in enumerate (self .index ):
691
692
idx_type = dtype_mapper (
692
- self .frame .index .get_level_values (i ). dtype )
693
+ self .frame .index .get_level_values (i ))
693
694
column_names_and_types .append ((idx_label , idx_type ))
694
695
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
+ ]
699
701
return column_names_and_types
700
702
701
703
def _create_table_statement (self ):
@@ -761,30 +763,33 @@ def _harmonize_columns(self, parse_dates=None):
761
763
except KeyError :
762
764
pass # this column not in results
763
765
764
- def _sqlalchemy_type (self , arr_or_dtype ):
766
+ def _sqlalchemy_type (self , col ):
765
767
from sqlalchemy .types import (BigInteger , Float , Text , Boolean ,
766
- DateTime , Date , Interval )
768
+ DateTime , Date , Time , Interval )
767
769
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 ):
771
771
try :
772
- tz = arr_or_dtype .tzinfo
772
+ tz = col .tzinfo
773
773
return DateTime (timezone = True )
774
774
except :
775
775
return DateTime
776
- if com .is_timedelta64_dtype (arr_or_dtype ):
776
+ if com .is_timedelta64_dtype (col ):
777
777
warnings .warn ("the 'timedelta' type is not supported, and will be "
778
778
"written as integer values (ns frequency) to the "
779
779
"database." , UserWarning )
780
780
return BigInteger
781
- elif com .is_float_dtype (arr_or_dtype ):
781
+ elif com .is_float_dtype (col ):
782
782
return Float
783
- elif com .is_integer_dtype (arr_or_dtype ):
783
+ elif com .is_integer_dtype (col ):
784
784
# TODO: Refine integer size.
785
785
return BigInteger
786
- elif com .is_bool_dtype (arr_or_dtype ):
786
+ elif com .is_bool_dtype (col ):
787
787
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
788
793
return Text
789
794
790
795
def _numpy_type (self , sqltype ):
@@ -913,7 +918,11 @@ def _create_sql_schema(self, frame, table_name):
913
918
},
914
919
'date' : {
915
920
'mysql' : 'DATE' ,
916
- 'sqlite' : 'TIMESTAMP' ,
921
+ 'sqlite' : 'DATE' ,
922
+ },
923
+ 'time' : {
924
+ 'mysql' : 'TIME' ,
925
+ 'sqlite' : 'TIME' ,
917
926
},
918
927
'bool' : {
919
928
'mysql' : 'BOOLEAN' ,
@@ -1019,8 +1028,8 @@ def _create_table_statement(self):
1019
1028
create_statement = template % {'name' : self .name , 'columns' : columns }
1020
1029
return create_statement
1021
1030
1022
- def _sql_type_name (self , dtype ):
1023
- pytype = dtype .type
1031
+ def _sql_type_name (self , col ):
1032
+ pytype = col . dtype .type
1024
1033
pytype_name = "text"
1025
1034
if issubclass (pytype , np .floating ):
1026
1035
pytype_name = "float"
@@ -1034,10 +1043,14 @@ def _sql_type_name(self, dtype):
1034
1043
elif issubclass (pytype , np .datetime64 ) or pytype is datetime :
1035
1044
# Caution: np.datetime64 is also a subclass of np.number.
1036
1045
pytype_name = "datetime"
1037
- elif pytype is datetime .date :
1038
- pytype_name = "date"
1039
1046
elif issubclass (pytype , np .bool_ ):
1040
1047
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"
1041
1054
1042
1055
return _SQL_TYPES [pytype_name ][self .pd_sql .flavor ]
1043
1056
0 commit comments