@@ -1255,6 +1255,29 @@ def test_datetime_with_timezone(self):
12551255 # to datetime64[ns,psycopg2.tz.FixedOffsetTimezone..], which is ok
12561256 # but should be more natural, so coerce to datetime64[ns] for now
12571257
1258+ def check (col ):
1259+ # check that a column is either datetime64[ns]
1260+ # or datetime64[ns, UTC]
1261+ if com .is_datetime64_dtype (col .dtype ):
1262+
1263+ # "2000-01-01 00:00:00-08:00" should convert to "2000-01-01 08:00:00"
1264+ self .assertEqual (col [0 ], Timestamp ('2000-01-01 08:00:00' ))
1265+
1266+ # "2000-06-01 00:00:00-07:00" should convert to "2000-06-01 07:00:00"
1267+ self .assertEqual (col [1 ], Timestamp ('2000-06-01 07:00:00' ))
1268+
1269+ elif com .is_datetime64tz_dtype (col .dtype ):
1270+ self .assertTrue (str (col .dt .tz ) == 'UTC' )
1271+
1272+ # "2000-01-01 00:00:00-08:00" should convert to "2000-01-01 08:00:00"
1273+ self .assertEqual (col [0 ], Timestamp ('2000-01-01 08:00:00' , tz = 'UTC' ))
1274+
1275+ # "2000-06-01 00:00:00-07:00" should convert to "2000-06-01 07:00:00"
1276+ self .assertEqual (col [1 ], Timestamp ('2000-06-01 07:00:00' , tz = 'UTC' ))
1277+
1278+ else :
1279+ raise AssertionError ("DateCol loaded with incorrect type -> {0}" .format (col .dtype ))
1280+
12581281 # GH11216
12591282 df = pd .read_sql_query ("select * from types_test_data" , self .conn )
12601283 if not hasattr (df ,'DateColWithTz' ):
@@ -1263,25 +1286,29 @@ def test_datetime_with_timezone(self):
12631286 # this is parsed on Travis (linux), but not on macosx for some reason
12641287 # even with the same versions of psycopg2 & sqlalchemy, possibly a Postgrsql server
12651288 # version difference
1266- dtype = df .DateColWithTz .dtype
1267- self .assertTrue (com .is_object_dtype (dtype ) or com .is_datetime64_dtype (dtype ),
1268- "DateCol loaded with incorrect type -> {0}" .format (dtype ))
1289+ col = df .DateColWithTz
1290+ self .assertTrue (com .is_object_dtype (col .dtype ) or com .is_datetime64_dtype (col .dtype ) \
1291+ or com .is_datetime64tz_dtype (col .dtype ),
1292+ "DateCol loaded with incorrect type -> {0}" .format (col .dtype ))
12691293
12701294 df = pd .read_sql_query ("select * from types_test_data" , self .conn , parse_dates = ['DateColWithTz' ])
12711295 if not hasattr (df ,'DateColWithTz' ):
12721296 raise nose .SkipTest ("no column with datetime with time zone" )
1273-
1274- dtype = df .DateColWithTz .dtype
1275- self .assertTrue (com .is_datetime64_dtype (dtype ),
1276- "DateCol loaded with incorrect type -> {0}" .format (dtype ))
1297+ check (df .DateColWithTz )
12771298
12781299 df = pd .concat (list (pd .read_sql_query ("select * from types_test_data" ,
12791300 self .conn ,chunksize = 1 )),ignore_index = True )
1280- dtype = df .DateColWithTz .dtype
1281- self .assertTrue (com .is_datetime64_dtype (dtype ),
1282- "DateCol loaded with incorrect type -> {0}" .format (dtype ))
1301+ col = df .DateColWithTz
1302+ self .assertTrue (com .is_datetime64tz_dtype (col .dtype ),
1303+ "DateCol loaded with incorrect type -> {0}" .format (col .dtype ))
1304+ self .assertTrue (str (col .dt .tz ) == 'UTC' )
12831305 expected = sql .read_sql_table ("types_test_data" , self .conn )
1284- tm .assert_series_equal (df .DateColWithTz , expected .DateColWithTz )
1306+ tm .assert_series_equal (df .DateColWithTz , expected .DateColWithTz .astype ('datetime64[ns, UTC]' ))
1307+
1308+ # xref #7139
1309+ # this might or might not be converted depending on the postgres driver
1310+ df = sql .read_sql_table ("types_test_data" , self .conn )
1311+ check (df .DateColWithTz )
12851312
12861313 def test_date_parsing (self ):
12871314 # No Parsing
@@ -1781,23 +1808,6 @@ def test_schema_support(self):
17811808 res2 = pdsql .read_table ('test_schema_other2' )
17821809 tm .assert_frame_equal (res1 , res2 )
17831810
1784- def test_datetime_with_time_zone (self ):
1785-
1786- # Test to see if we read the date column with timezones that
1787- # the timezone information is converted to utc and into a
1788- # np.datetime64 (GH #7139)
1789-
1790- df = sql .read_sql_table ("types_test_data" , self .conn )
1791- self .assertTrue (issubclass (df .DateColWithTz .dtype .type , np .datetime64 ),
1792- "DateColWithTz loaded with incorrect type -> {0}" .format (df .DateColWithTz .dtype ))
1793-
1794- # "2000-01-01 00:00:00-08:00" should convert to "2000-01-01 08:00:00"
1795- self .assertEqual (df .DateColWithTz [0 ], Timestamp ('2000-01-01 08:00:00' ))
1796-
1797- # "2000-06-01 00:00:00-07:00" should convert to "2000-06-01 07:00:00"
1798- self .assertEqual (df .DateColWithTz [1 ], Timestamp ('2000-06-01 07:00:00' ))
1799-
1800-
18011811class TestMySQLAlchemy (_TestMySQLAlchemy , _TestSQLAlchemy ):
18021812 pass
18031813
0 commit comments