@@ -1255,6 +1255,29 @@ def test_datetime_with_timezone(self):
1255
1255
# to datetime64[ns,psycopg2.tz.FixedOffsetTimezone..], which is ok
1256
1256
# but should be more natural, so coerce to datetime64[ns] for now
1257
1257
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
+
1258
1281
# GH11216
1259
1282
df = pd .read_sql_query ("select * from types_test_data" , self .conn )
1260
1283
if not hasattr (df ,'DateColWithTz' ):
@@ -1263,25 +1286,29 @@ def test_datetime_with_timezone(self):
1263
1286
# this is parsed on Travis (linux), but not on macosx for some reason
1264
1287
# even with the same versions of psycopg2 & sqlalchemy, possibly a Postgrsql server
1265
1288
# 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 ))
1269
1293
1270
1294
df = pd .read_sql_query ("select * from types_test_data" , self .conn , parse_dates = ['DateColWithTz' ])
1271
1295
if not hasattr (df ,'DateColWithTz' ):
1272
1296
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 )
1277
1298
1278
1299
df = pd .concat (list (pd .read_sql_query ("select * from types_test_data" ,
1279
1300
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' )
1283
1305
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 )
1285
1312
1286
1313
def test_date_parsing (self ):
1287
1314
# No Parsing
@@ -1781,23 +1808,6 @@ def test_schema_support(self):
1781
1808
res2 = pdsql .read_table ('test_schema_other2' )
1782
1809
tm .assert_frame_equal (res1 , res2 )
1783
1810
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
-
1801
1811
class TestMySQLAlchemy (_TestMySQLAlchemy , _TestSQLAlchemy ):
1802
1812
pass
1803
1813
0 commit comments