6
6
7
7
import google .oauth2 .service_account
8
8
import numpy as np
9
+ import pandas
9
10
import pandas .util .testing as tm
10
- import pytest
11
- import pytz
12
11
from pandas import DataFrame , NaT , compat
13
12
from pandas .compat import range , u
13
+ import pytest
14
+ import pytz
14
15
15
16
from pandas_gbq import gbq
16
17
@@ -311,7 +312,8 @@ def test_should_properly_handle_timestamp_unix_epoch(self, project_id):
311
312
tm .assert_frame_equal (
312
313
df ,
313
314
DataFrame (
314
- {"unix_epoch" : [np .datetime64 ("1970-01-01T00:00:00.000000Z" )]}
315
+ {"unix_epoch" : ["1970-01-01T00:00:00.000000Z" ]},
316
+ dtype = "datetime64[ns, UTC]" ,
315
317
),
316
318
)
317
319
@@ -323,6 +325,38 @@ def test_should_properly_handle_arbitrary_timestamp(self, project_id):
323
325
credentials = self .credentials ,
324
326
dialect = "legacy" ,
325
327
)
328
+ tm .assert_frame_equal (
329
+ df ,
330
+ DataFrame (
331
+ {"valid_timestamp" : ["2004-09-15T05:00:00.000000Z" ]},
332
+ dtype = "datetime64[ns, UTC]" ,
333
+ ),
334
+ )
335
+
336
+ def test_should_properly_handle_datetime_unix_epoch (self , project_id ):
337
+ query = 'SELECT DATETIME("1970-01-01 00:00:00") AS unix_epoch'
338
+ df = gbq .read_gbq (
339
+ query ,
340
+ project_id = project_id ,
341
+ credentials = self .credentials ,
342
+ dialect = "legacy" ,
343
+ )
344
+ tm .assert_frame_equal (
345
+ df ,
346
+ DataFrame (
347
+ {"unix_epoch" : ["1970-01-01T00:00:00.000000Z" ]},
348
+ dtype = "datetime64[ns]" ,
349
+ ),
350
+ )
351
+
352
+ def test_should_properly_handle_arbitrary_datetime (self , project_id ):
353
+ query = 'SELECT DATETIME("2004-09-15 05:00:00") AS valid_timestamp'
354
+ df = gbq .read_gbq (
355
+ query ,
356
+ project_id = project_id ,
357
+ credentials = self .credentials ,
358
+ dialect = "legacy" ,
359
+ )
326
360
tm .assert_frame_equal (
327
361
df ,
328
362
DataFrame (
@@ -338,7 +372,7 @@ def test_should_properly_handle_arbitrary_timestamp(self, project_id):
338
372
"expression, type_" ,
339
373
[
340
374
("current_date()" , "<M8[ns]" ),
341
- ("current_timestamp()" , "<M8 [ns]" ),
375
+ ("current_timestamp()" , "datetime64 [ns, UTC ]" ),
342
376
("current_datetime()" , "<M8[ns]" ),
343
377
("TRUE" , bool ),
344
378
("FALSE" , bool ),
@@ -370,7 +404,20 @@ def test_should_properly_handle_null_timestamp(self, project_id):
370
404
credentials = self .credentials ,
371
405
dialect = "legacy" ,
372
406
)
373
- tm .assert_frame_equal (df , DataFrame ({"null_timestamp" : [NaT ]}))
407
+ tm .assert_frame_equal (
408
+ df ,
409
+ DataFrame ({"null_timestamp" : [NaT ]}, dtype = "datetime64[ns, UTC]" ),
410
+ )
411
+
412
+ def test_should_properly_handle_null_datetime (self , project_id ):
413
+ query = "SELECT CAST(NULL AS DATETIME) AS null_datetime"
414
+ df = gbq .read_gbq (
415
+ query ,
416
+ project_id = project_id ,
417
+ credentials = self .credentials ,
418
+ dialect = "standard" ,
419
+ )
420
+ tm .assert_frame_equal (df , DataFrame ({"null_datetime" : [NaT ]}))
374
421
375
422
def test_should_properly_handle_null_boolean (self , project_id ):
376
423
query = "SELECT BOOLEAN(NULL) AS null_boolean"
@@ -541,18 +588,13 @@ def test_zero_rows(self, project_id):
541
588
credentials = self .credentials ,
542
589
dialect = "legacy" ,
543
590
)
544
- page_array = np .zeros (
545
- (0 ,),
546
- dtype = [
547
- ("title" , object ),
548
- ("id" , np .dtype (int )),
549
- ("is_bot" , np .dtype (bool )),
550
- ("ts" , "M8[ns]" ),
551
- ],
552
- )
553
- expected_result = DataFrame (
554
- page_array , columns = ["title" , "id" , "is_bot" , "ts" ]
555
- )
591
+ empty_columns = {
592
+ "title" : pandas .Series ([], dtype = object ),
593
+ "id" : pandas .Series ([], dtype = object ),
594
+ "is_bot" : pandas .Series ([], dtype = object ),
595
+ "ts" : pandas .Series ([], dtype = "datetime64[ns, UTC]" ),
596
+ }
597
+ expected_result = DataFrame (empty_columns )
556
598
tm .assert_frame_equal (df , expected_result , check_index_type = False )
557
599
558
600
def test_one_row_one_column (self , project_id ):
0 commit comments