2929from datetime import datetime
3030
3131from pandas import DataFrame , Series , Index , MultiIndex , isnull
32- from pandas import to_timedelta
32+ from pandas import date_range , to_datetime , to_timedelta
3333import pandas .compat as compat
34- from pandas .compat import StringIO , range , lrange
34+ from pandas .compat import StringIO , range , lrange , string_types
3535from pandas .core .datetools import format as date_format
3636
3737import pandas .io .sql as sql
@@ -870,6 +870,29 @@ def test_date_parsing(self):
870870 self .assertTrue (issubclass (df .IntDateCol .dtype .type , np .datetime64 ),
871871 "IntDateCol loaded with incorrect type" )
872872
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+
873896 def test_mixed_dtype_insert (self ):
874897 # see GH6509
875898 s1 = Series (2 ** 25 + 1 ,dtype = np .int32 )
@@ -895,7 +918,7 @@ def connect(self):
895918
896919 def setup_driver (self ):
897920 # sqlite3 is built-in
898- pass
921+ self . driver = None
899922
900923 def tearDown (self ):
901924 # in memory so tables should not be removed explicitly
0 commit comments