29
29
from datetime import datetime
30
30
31
31
from pandas import DataFrame , Series , Index , MultiIndex , isnull
32
- from pandas import to_timedelta
32
+ from pandas import date_range , to_datetime , to_timedelta
33
33
import pandas .compat as compat
34
- from pandas .compat import StringIO , range , lrange
34
+ from pandas .compat import StringIO , range , lrange , string_types
35
35
from pandas .core .datetools import format as date_format
36
36
37
37
import pandas .io .sql as sql
@@ -870,6 +870,29 @@ def test_date_parsing(self):
870
870
self .assertTrue (issubclass (df .IntDateCol .dtype .type , np .datetime64 ),
871
871
"IntDateCol loaded with incorrect type" )
872
872
873
+ def test_datetime (self ):
874
+ if self .driver == 'pymysql' :
875
+ raise nose .SkipTest ('writing datetime not working with pymysql' )
876
+
877
+ df = DataFrame ({'A' : date_range ('2013-01-01 09:00:00' , periods = 3 ),
878
+ 'B' : np .arange (3.0 )})
879
+ df .to_sql ('test_datetime' , self .conn )
880
+
881
+ # with read_table -> type information from schema used
882
+ result = sql .read_sql_table ('test_datetime' , self .conn )
883
+ result = result .drop ('index' , axis = 1 )
884
+ tm .assert_frame_equal (result , df )
885
+
886
+ # with read_sql -> no type information -> sqlite has no native
887
+ result = sql .read_sql_query ('SELECT * FROM test_datetime' , self .conn )
888
+ result = result .drop ('index' , axis = 1 )
889
+ if self .flavor == 'sqlite' :
890
+ self .assertTrue (isinstance (result .loc [0 , 'A' ], string_types ))
891
+ result ['A' ] = to_datetime (result ['A' ])
892
+ tm .assert_frame_equal (result , df )
893
+ else :
894
+ tm .assert_frame_equal (result , df )
895
+
873
896
def test_mixed_dtype_insert (self ):
874
897
# see GH6509
875
898
s1 = Series (2 ** 25 + 1 ,dtype = np .int32 )
@@ -895,7 +918,7 @@ def connect(self):
895
918
896
919
def setup_driver (self ):
897
920
# sqlite3 is built-in
898
- pass
921
+ self . driver = None
899
922
900
923
def tearDown (self ):
901
924
# in memory so tables should not be removed explicitly
0 commit comments