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
@@ -684,13 +685,14 @@ def _get_column_names_and_types(self, dtype_mapper):
684
685
if self .index is not None :
685
686
for i , idx_label in enumerate (self .index ):
686
687
idx_type = dtype_mapper (
687
- self .frame .index .get_level_values (i ). dtype )
688
+ self .frame .index .get_level_values (i ))
688
689
column_names_and_types .append ((idx_label , idx_type ))
689
690
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
+ ]
694
696
return column_names_and_types
695
697
696
698
def _create_table_statement (self ):
@@ -756,30 +758,33 @@ def _harmonize_columns(self, parse_dates=None):
756
758
except KeyError :
757
759
pass # this column not in results
758
760
759
- def _sqlalchemy_type (self , arr_or_dtype ):
761
+ def _sqlalchemy_type (self , col ):
760
762
from sqlalchemy .types import (BigInteger , Float , Text , Boolean ,
761
- DateTime , Date , Interval )
763
+ DateTime , Date , Time , Interval )
762
764
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 ):
766
766
try :
767
- tz = arr_or_dtype .tzinfo
767
+ tz = col .tzinfo
768
768
return DateTime (timezone = True )
769
769
except :
770
770
return DateTime
771
- if com .is_timedelta64_dtype (arr_or_dtype ):
771
+ if com .is_timedelta64_dtype (col ):
772
772
warnings .warn ("the 'timedelta' type is not supported, and will be "
773
773
"written as integer values (ns frequency) to the "
774
774
"database." , UserWarning )
775
775
return BigInteger
776
- elif com .is_float_dtype (arr_or_dtype ):
776
+ elif com .is_float_dtype (col ):
777
777
return Float
778
- elif com .is_integer_dtype (arr_or_dtype ):
778
+ elif com .is_integer_dtype (col ):
779
779
# TODO: Refine integer size.
780
780
return BigInteger
781
- elif com .is_bool_dtype (arr_or_dtype ):
781
+ elif com .is_bool_dtype (col ):
782
782
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
783
788
return Text
784
789
785
790
def _numpy_type (self , sqltype ):
@@ -908,7 +913,11 @@ def _create_sql_schema(self, frame, table_name):
908
913
},
909
914
'date' : {
910
915
'mysql' : 'DATE' ,
911
- 'sqlite' : 'TIMESTAMP' ,
916
+ 'sqlite' : 'DATE' ,
917
+ },
918
+ 'time' : {
919
+ 'mysql' : 'TIME' ,
920
+ 'sqlite' : 'TIME' ,
912
921
},
913
922
'bool' : {
914
923
'mysql' : 'BOOLEAN' ,
@@ -1014,8 +1023,8 @@ def _create_table_statement(self):
1014
1023
create_statement = template % {'name' : self .name , 'columns' : columns }
1015
1024
return create_statement
1016
1025
1017
- def _sql_type_name (self , dtype ):
1018
- pytype = dtype .type
1026
+ def _sql_type_name (self , col ):
1027
+ pytype = col . dtype .type
1019
1028
pytype_name = "text"
1020
1029
if issubclass (pytype , np .floating ):
1021
1030
pytype_name = "float"
@@ -1029,10 +1038,14 @@ def _sql_type_name(self, dtype):
1029
1038
elif issubclass (pytype , np .datetime64 ) or pytype is datetime :
1030
1039
# Caution: np.datetime64 is also a subclass of np.number.
1031
1040
pytype_name = "datetime"
1032
- elif pytype is datetime .date :
1033
- pytype_name = "date"
1034
1041
elif issubclass (pytype , np .bool_ ):
1035
1042
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"
1036
1049
1037
1050
return _SQL_TYPES [pytype_name ][self .pd_sql .flavor ]
1038
1051
0 commit comments