35
35
except ImportError :
36
36
pandas = None
37
37
38
+ try :
39
+ import pandas_gbq .schema .pandas_to_bigquery
40
+ except ImportError :
41
+ pandas_gbq = None
42
+
38
43
try :
39
44
import geopandas
40
45
except ImportError :
@@ -1281,7 +1286,21 @@ def test_dataframe_to_parquet_compression_method(module_under_test):
1281
1286
1282
1287
1283
1288
@pytest .mark .skipif (pandas is None , reason = "Requires `pandas`" )
1284
- def test_dataframe_to_bq_schema_w_named_index (module_under_test ):
1289
+ @pytest .mark .skipif (pandas_gbq is None , reason = "Requires `pandas-gbq`" )
1290
+ def test_dataframe_to_bq_schema_returns_schema_with_pandas_gbq (
1291
+ module_under_test , monkeypatch
1292
+ ):
1293
+ monkeypatch .setattr (module_under_test , "pandas_gbq" , None )
1294
+ dataframe = pandas .DataFrame ({"field00" : ["foo" , "bar" ]})
1295
+ got = module_under_test .dataframe_to_bq_schema (dataframe , [])
1296
+ # Don't assert beyond this, since pandas-gbq is now source of truth.
1297
+ assert got is not None
1298
+
1299
+
1300
+ @pytest .mark .skipif (pandas is None , reason = "Requires `pandas`" )
1301
+ def test_dataframe_to_bq_schema_w_named_index (module_under_test , monkeypatch ):
1302
+ monkeypatch .setattr (module_under_test , "pandas_gbq" , None )
1303
+
1285
1304
df_data = collections .OrderedDict (
1286
1305
[
1287
1306
("str_column" , ["hello" , "world" ]),
@@ -1292,7 +1311,8 @@ def test_dataframe_to_bq_schema_w_named_index(module_under_test):
1292
1311
index = pandas .Index (["a" , "b" ], name = "str_index" )
1293
1312
dataframe = pandas .DataFrame (df_data , index = index )
1294
1313
1295
- returned_schema = module_under_test .dataframe_to_bq_schema (dataframe , [])
1314
+ with pytest .warns (FutureWarning , match = "pandas-gbq" ):
1315
+ returned_schema = module_under_test .dataframe_to_bq_schema (dataframe , [])
1296
1316
1297
1317
expected_schema = (
1298
1318
schema .SchemaField ("str_index" , "STRING" , "NULLABLE" ),
@@ -1304,7 +1324,9 @@ def test_dataframe_to_bq_schema_w_named_index(module_under_test):
1304
1324
1305
1325
1306
1326
@pytest .mark .skipif (pandas is None , reason = "Requires `pandas`" )
1307
- def test_dataframe_to_bq_schema_w_multiindex (module_under_test ):
1327
+ def test_dataframe_to_bq_schema_w_multiindex (module_under_test , monkeypatch ):
1328
+ monkeypatch .setattr (module_under_test , "pandas_gbq" , None )
1329
+
1308
1330
df_data = collections .OrderedDict (
1309
1331
[
1310
1332
("str_column" , ["hello" , "world" ]),
@@ -1321,7 +1343,8 @@ def test_dataframe_to_bq_schema_w_multiindex(module_under_test):
1321
1343
)
1322
1344
dataframe = pandas .DataFrame (df_data , index = index )
1323
1345
1324
- returned_schema = module_under_test .dataframe_to_bq_schema (dataframe , [])
1346
+ with pytest .warns (FutureWarning , match = "pandas-gbq" ):
1347
+ returned_schema = module_under_test .dataframe_to_bq_schema (dataframe , [])
1325
1348
1326
1349
expected_schema = (
1327
1350
schema .SchemaField ("str_index" , "STRING" , "NULLABLE" ),
@@ -1335,7 +1358,9 @@ def test_dataframe_to_bq_schema_w_multiindex(module_under_test):
1335
1358
1336
1359
1337
1360
@pytest .mark .skipif (pandas is None , reason = "Requires `pandas`" )
1338
- def test_dataframe_to_bq_schema_w_bq_schema (module_under_test ):
1361
+ def test_dataframe_to_bq_schema_w_bq_schema (module_under_test , monkeypatch ):
1362
+ monkeypatch .setattr (module_under_test , "pandas_gbq" , None )
1363
+
1339
1364
df_data = collections .OrderedDict (
1340
1365
[
1341
1366
("str_column" , ["hello" , "world" ]),
@@ -1350,7 +1375,10 @@ def test_dataframe_to_bq_schema_w_bq_schema(module_under_test):
1350
1375
{"name" : "bool_column" , "type" : "BOOL" , "mode" : "REQUIRED" },
1351
1376
]
1352
1377
1353
- returned_schema = module_under_test .dataframe_to_bq_schema (dataframe , dict_schema )
1378
+ with pytest .warns (FutureWarning , match = "pandas-gbq" ):
1379
+ returned_schema = module_under_test .dataframe_to_bq_schema (
1380
+ dataframe , dict_schema
1381
+ )
1354
1382
1355
1383
expected_schema = (
1356
1384
schema .SchemaField ("str_column" , "STRING" , "NULLABLE" ),
@@ -1361,7 +1389,11 @@ def test_dataframe_to_bq_schema_w_bq_schema(module_under_test):
1361
1389
1362
1390
1363
1391
@pytest .mark .skipif (pandas is None , reason = "Requires `pandas`" )
1364
- def test_dataframe_to_bq_schema_fallback_needed_wo_pyarrow (module_under_test ):
1392
+ def test_dataframe_to_bq_schema_fallback_needed_wo_pyarrow (
1393
+ module_under_test , monkeypatch
1394
+ ):
1395
+ monkeypatch .setattr (module_under_test , "pandas_gbq" , None )
1396
+
1365
1397
dataframe = pandas .DataFrame (
1366
1398
data = [
1367
1399
{"id" : 10 , "status" : "FOO" , "execution_date" : datetime .date (2019 , 5 , 10 )},
@@ -1389,7 +1421,11 @@ def test_dataframe_to_bq_schema_fallback_needed_wo_pyarrow(module_under_test):
1389
1421
1390
1422
@pytest .mark .skipif (pandas is None , reason = "Requires `pandas`" )
1391
1423
@pytest .mark .skipif (isinstance (pyarrow , mock .Mock ), reason = "Requires `pyarrow`" )
1392
- def test_dataframe_to_bq_schema_fallback_needed_w_pyarrow (module_under_test ):
1424
+ def test_dataframe_to_bq_schema_fallback_needed_w_pyarrow (
1425
+ module_under_test , monkeypatch
1426
+ ):
1427
+ monkeypatch .setattr (module_under_test , "pandas_gbq" , None )
1428
+
1393
1429
dataframe = pandas .DataFrame (
1394
1430
data = [
1395
1431
{"id" : 10 , "status" : "FOO" , "created_at" : datetime .date (2019 , 5 , 10 )},
@@ -1419,7 +1455,9 @@ def test_dataframe_to_bq_schema_fallback_needed_w_pyarrow(module_under_test):
1419
1455
1420
1456
@pytest .mark .skipif (pandas is None , reason = "Requires `pandas`" )
1421
1457
@pytest .mark .skipif (isinstance (pyarrow , mock .Mock ), reason = "Requires `pyarrow`" )
1422
- def test_dataframe_to_bq_schema_pyarrow_fallback_fails (module_under_test ):
1458
+ def test_dataframe_to_bq_schema_pyarrow_fallback_fails (module_under_test , monkeypatch ):
1459
+ monkeypatch .setattr (module_under_test , "pandas_gbq" , None )
1460
+
1423
1461
dataframe = pandas .DataFrame (
1424
1462
data = [
1425
1463
{"struct_field" : {"one" : 2 }, "status" : "FOO" },
@@ -1443,9 +1481,11 @@ def test_dataframe_to_bq_schema_pyarrow_fallback_fails(module_under_test):
1443
1481
1444
1482
1445
1483
@pytest .mark .skipif (geopandas is None , reason = "Requires `geopandas`" )
1446
- def test_dataframe_to_bq_schema_geography (module_under_test ):
1484
+ def test_dataframe_to_bq_schema_geography (module_under_test , monkeypatch ):
1447
1485
from shapely import wkt
1448
1486
1487
+ monkeypatch .setattr (module_under_test , "pandas_gbq" , None )
1488
+
1449
1489
df = geopandas .GeoDataFrame (
1450
1490
pandas .DataFrame (
1451
1491
dict (
@@ -1456,7 +1496,10 @@ def test_dataframe_to_bq_schema_geography(module_under_test):
1456
1496
),
1457
1497
geometry = "geo1" ,
1458
1498
)
1459
- bq_schema = module_under_test .dataframe_to_bq_schema (df , [])
1499
+
1500
+ with pytest .warns (FutureWarning , match = "pandas-gbq" ):
1501
+ bq_schema = module_under_test .dataframe_to_bq_schema (df , [])
1502
+
1460
1503
assert bq_schema == (
1461
1504
schema .SchemaField ("name" , "STRING" ),
1462
1505
schema .SchemaField ("geo1" , "GEOGRAPHY" ),
0 commit comments