This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathtest_dataframe_io.py
More file actions
1115 lines (921 loc) · 38.6 KB
/
test_dataframe_io.py
File metadata and controls
1115 lines (921 loc) · 38.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Tuple
import google.api_core.exceptions
import numpy
import numpy.testing
import pandas as pd
import pandas.testing
import pyarrow as pa
import pytest
import bigframes.dtypes as dtypes
from bigframes.testing import utils
try:
import pandas_gbq # type: ignore
except ImportError: # pragma: NO COVER
# TODO(b/332758806): Run system tests without "extras"
pandas_gbq = None
import typing
from google.cloud import bigquery
import bigframes
import bigframes.dataframe
import bigframes.enums
import bigframes.features
import bigframes.pandas as bpd
def test_sql_executes(scalars_df_default_index, bigquery_client):
"""Test that DataFrame.sql returns executable SQL.
DF.sql is used in public documentation such as
https://cloud.google.com/blog/products/data-analytics/using-bigquery-dataframes-with-carto-geospatial-tools
as a way to pass a DataFrame on to carto without executing the SQL
immediately.
Make sure that this SQL can be run outside of BigQuery DataFrames (assuming
similar credentials / access to the referenced tables).
"""
# Do some operations to make for more complex SQL.
df = (
scalars_df_default_index.drop(columns=["geography_col", "duration_col"])
.groupby("string_col")
.max()
)
df.index.name = None # Don't include unnamed indexes.
query = df.sql
bf_result = df.to_pandas().sort_values("rowindex").reset_index(drop=True)
bq_result = (
bigquery_client.query_and_wait(query)
.to_dataframe()
.sort_values("rowindex")
.reset_index(drop=True)
)
pandas.testing.assert_frame_equal(bf_result, bq_result, check_dtype=False)
def test_sql_executes_and_includes_named_index(
scalars_df_default_index, bigquery_client
):
"""Test that DataFrame.sql returns executable SQL.
DF.sql is used in public documentation such as
https://cloud.google.com/blog/products/data-analytics/using-bigquery-dataframes-with-carto-geospatial-tools
as a way to pass a DataFrame on to carto without executing the SQL
immediately.
Make sure that this SQL can be run outside of BigQuery DataFrames (assuming
similar credentials / access to the referenced tables).
"""
# Do some operations to make for more complex SQL.
df = (
scalars_df_default_index.drop(columns=["geography_col", "duration_col"])
.groupby("string_col")
.max()
)
query = df.sql
bf_result = df.to_pandas().sort_values("rowindex")
bq_result = (
bigquery_client.query_and_wait(query)
.to_dataframe()
.set_index("string_col")
.sort_values("rowindex")
)
pandas.testing.assert_frame_equal(
bf_result, bq_result, check_dtype=False, check_index_type=False
)
def test_sql_executes_and_includes_named_multiindex(
scalars_df_default_index, bigquery_client
):
"""Test that DataFrame.sql returns executable SQL.
DF.sql is used in public documentation such as
https://cloud.google.com/blog/products/data-analytics/using-bigquery-dataframes-with-carto-geospatial-tools
as a way to pass a DataFrame on to carto without executing the SQL
immediately.
Make sure that this SQL can be run outside of BigQuery DataFrames (assuming
similar credentials / access to the referenced tables).
"""
# Do some operations to make for more complex SQL.
df = (
scalars_df_default_index.drop(columns=["geography_col", "duration_col"])
.groupby(["string_col", "bool_col"])
.max()
)
query = df.sql
bf_result = df.to_pandas().sort_values("rowindex")
bq_result = (
bigquery_client.query_and_wait(query)
.to_dataframe()
.set_index(["string_col", "bool_col"])
.sort_values("rowindex")
)
pandas.testing.assert_frame_equal(
bf_result, bq_result, check_dtype=False, check_index_type=False
)
def test_to_arrow(scalars_df_default_index, scalars_pandas_df_default_index):
"""Verify to_arrow() APIs returns the expected data."""
expected = pa.Table.from_pandas(
scalars_pandas_df_default_index.drop(columns=["geography_col"])
)
with pytest.warns(
bigframes.exceptions.PreviewWarning,
match="to_arrow",
):
actual = scalars_df_default_index.drop(columns=["geography_col"]).to_arrow()
# Make string_col match type. Otherwise, pa.Table.from_pandas uses
# LargeStringArray. LargeStringArray is unnecessary because our strings are
# less than 2 GB.
expected = expected.set_column(
expected.column_names.index("string_col"),
pa.field("string_col", pa.string()),
expected["string_col"].cast(pa.string()),
)
# Note: the final .equals assertion covers all these checks, but these
# finer-grained assertions are easier to debug.
assert actual.column_names == expected.column_names
for column in actual.column_names:
assert actual[column].equals(expected[column])
assert actual.equals(expected)
def test_to_arrow_multiindex(scalars_df_index, scalars_pandas_df_index):
scalars_df_multiindex = scalars_df_index.set_index(["string_col", "int64_col"])
scalars_pandas_df_multiindex = scalars_pandas_df_index.set_index(
["string_col", "int64_col"]
)
expected = pa.Table.from_pandas(
scalars_pandas_df_multiindex.drop(columns=["geography_col"])
)
with pytest.warns(
bigframes.exceptions.PreviewWarning,
match="to_arrow",
):
actual = scalars_df_multiindex.drop(columns=["geography_col"]).to_arrow()
# Make string_col match type. Otherwise, pa.Table.from_pandas uses
# LargeStringArray. LargeStringArray is unnecessary because our strings are
# less than 2 GB.
expected = expected.set_column(
expected.column_names.index("string_col"),
pa.field("string_col", pa.string()),
expected["string_col"].cast(pa.string()),
)
# Note: the final .equals assertion covers all these checks, but these
# finer-grained assertions are easier to debug.
assert actual.column_names == expected.column_names
for column in actual.column_names:
assert actual[column].equals(expected[column])
assert actual.equals(expected)
def test_to_pandas_w_correct_dtypes(scalars_df_default_index):
"""Verify to_pandas() APIs returns the expected dtypes."""
actual = scalars_df_default_index.to_pandas().dtypes
expected = scalars_df_default_index.dtypes
pd.testing.assert_series_equal(actual, expected)
def test_to_pandas_array_struct_correct_result(session):
"""In future, we should support arrays with arrow types.
For now we fall back to the current connector behavior of converting
to Python objects"""
df = session.read_gbq(
"""SELECT
[1, 3, 2] AS array_column,
STRUCT(
"a" AS string_field,
1.2 AS float_field) AS struct_column"""
)
result = df.to_pandas()
expected = pd.DataFrame(
{
"array_column": pd.Series(
[[1, 3, 2]],
dtype=(
pd.ArrowDtype(pa.list_(pa.int64()))
if bigframes.features.PANDAS_VERSIONS.is_arrow_list_dtype_usable
else "object"
),
),
"struct_column": pd.Series(
[{"string_field": "a", "float_field": 1.2}],
dtype=pd.ArrowDtype(
pa.struct(
[
("string_field", pa.string()),
("float_field", pa.float64()),
]
)
),
),
}
)
expected.index = expected.index.astype("Int64")
pd.testing.assert_series_equal(result.dtypes, expected.dtypes)
pd.testing.assert_series_equal(result["array_column"], expected["array_column"])
# assert_series_equal not implemented for struct columns yet. Compare
# values as Python objects, instead.
pd.testing.assert_series_equal(
result["struct_column"].astype("O"), expected["struct_column"].astype("O")
)
def test_to_pandas_override_global_option(scalars_df_index):
# Direct call to_pandas uses global default setting (allow_large_results=True),
# table has 'bqdf' prefix.
with bigframes.option_context("compute.allow_large_results", True):
scalars_df_index.to_pandas()
table_id = scalars_df_index._query_job.destination.table_id
assert table_id is not None
# When allow_large_results=False, a query_job object should not be created.
# Therefore, the table_id should remain unchanged.
scalars_df_index.to_pandas(allow_large_results=False)
assert scalars_df_index._query_job.destination.table_id == table_id
def test_to_pandas_downsampling_option_override(session):
df = session.read_gbq("bigframes-dev.bigframes_tests_sys.batting")
download_size = 1
with pytest.warns(
UserWarning, match="The data size .* exceeds the maximum download limit"
):
# limits only apply for allow_large_result=True
df = df.to_pandas(
max_download_size=download_size,
sampling_method="head",
allow_large_results=True,
)
total_memory_bytes = df.memory_usage(deep=True).sum()
total_memory_mb = total_memory_bytes / (1024 * 1024)
assert total_memory_mb == pytest.approx(download_size, rel=0.5)
@pytest.mark.parametrize(
("kwargs", "message"),
[
pytest.param(
{"sampling_method": "head"},
r"DEPRECATED[\S\s]*sampling_method[\S\s]*DataFrame.sample",
id="sampling_method",
),
pytest.param(
{"random_state": 10},
r"DEPRECATED[\S\s]*random_state[\S\s]*DataFrame.sample",
id="random_state",
),
pytest.param(
{"max_download_size": 10},
r"DEPRECATED[\S\s]*max_download_size[\S\s]*DataFrame.to_pandas_batches",
id="max_download_size",
),
],
)
def test_to_pandas_warns_deprecated_parameters(scalars_df_index, kwargs, message):
with pytest.warns(FutureWarning, match=message):
scalars_df_index.to_pandas(
# limits only apply for allow_large_result=True
allow_large_results=True,
**kwargs,
)
def test_to_pandas_dry_run(session, scalars_pandas_df_multi_index):
bf_df = session.read_pandas(scalars_pandas_df_multi_index)
result = bf_df.to_pandas(dry_run=True)
assert isinstance(result, pd.Series)
assert len(result) > 0
def test_to_arrow_override_global_option(scalars_df_index):
# Direct call to_arrow uses global default setting (allow_large_results=True),
with bigframes.option_context("compute.allow_large_results", True):
scalars_df_index.to_arrow()
table_id = scalars_df_index._query_job.destination.table_id
assert table_id is not None
# When allow_large_results=False, a query_job object should not be created.
# Therefore, the table_id should remain unchanged.
scalars_df_index.to_arrow(allow_large_results=False)
assert scalars_df_index._query_job.destination.table_id == table_id
def test_to_pandas_batches_populates_total_bytes_processed(scalars_df_default_index):
batches = scalars_df_default_index.sort_values(
"int64_col"
).to_pandas_batches() # Do a sort to force query execution.
assert batches.total_bytes_processed > 0
def test_to_pandas_batches_w_correct_dtypes(scalars_df_default_index):
"""Verify to_pandas_batches() APIs returns the expected dtypes."""
expected = scalars_df_default_index.dtypes
for df in scalars_df_default_index.to_pandas_batches():
actual = df.dtypes
pd.testing.assert_series_equal(actual, expected)
def test_to_pandas_batches_w_empty_dataframe(session):
"""Verify to_pandas_batches() APIs returns at least one DataFrame.
See b/428918844 for additional context.
"""
empty = bpd.DataFrame(
{
"idx1": [],
"idx2": [],
"col1": pandas.Series([], dtype="string[pyarrow]"),
"col2": pandas.Series([], dtype="Int64"),
},
session=session,
).set_index(["idx1", "idx2"], drop=True)
results = list(empty.to_pandas_batches())
assert len(results) == 1
assert list(results[0].index.names) == ["idx1", "idx2"]
assert list(results[0].columns) == ["col1", "col2"]
pandas.testing.assert_series_equal(results[0].dtypes, empty.dtypes)
@pytest.mark.parametrize("allow_large_results", (True, False))
def test_to_pandas_batches_w_page_size_and_max_results(session, allow_large_results):
"""Verify to_pandas_batches() APIs returns the expected page size.
Regression test for b/407521010.
"""
bf_df = session.read_gbq(
"bigquery-public-data.usa_names.usa_1910_2013",
index_col=bigframes.enums.DefaultIndexKind.NULL,
)
expected_column_count = len(bf_df.columns)
batch_count = 0
for pd_df in bf_df.to_pandas_batches(
page_size=42, allow_large_results=allow_large_results, max_results=42 * 3
):
batch_row_count, batch_column_count = pd_df.shape
batch_count += 1
assert batch_column_count == expected_column_count
assert batch_row_count == 42
assert batch_count == 3
@pytest.mark.parametrize(
("index",),
[(True,), (False,)],
)
def test_to_csv_index(
scalars_dfs: Tuple[bigframes.dataframe.DataFrame, pd.DataFrame],
gcs_folder: str,
index: bool,
):
if pd.__version__.startswith("1."):
pytest.skip("date_format parameter not supported in pandas 1.x.")
"""Test the `to_csv` API with the `index` parameter."""
scalars_df, scalars_pandas_df = scalars_dfs
index_col = None
path = gcs_folder + f"test_index_df_to_csv_index_{index}*.csv"
if index:
index_col = typing.cast(str, scalars_df.index.name)
# TODO(swast): Support "date_format" parameter and make sure our
# DATETIME/TIMESTAMP column export is the same format as pandas by default.
scalars_df.to_csv(path, index=index)
# Pandas dataframes dtypes from read_csv are not fully compatible with
# BigQuery-backed dataframes, so manually convert the dtypes specifically
# here.
dtype = scalars_df.reset_index().dtypes.to_dict()
dtype.pop("geography_col")
dtype.pop("rowindex")
# read_csv will decode into bytes inproperly, convert_pandas_dtypes will encode properly from string
dtype.pop("bytes_col")
gcs_df = pd.read_csv(
utils.get_first_file_from_wildcard(path),
dtype=dtype,
date_format={"timestamp_col": "YYYY-MM-DD HH:MM:SS Z"},
index_col=index_col,
)
utils.convert_pandas_dtypes(gcs_df, bytes_col=True)
gcs_df.index.name = scalars_df.index.name
scalars_pandas_df = scalars_pandas_df.copy()
scalars_pandas_df.index = scalars_pandas_df.index.astype("int64")
# Ordering should be maintained for tables smaller than 1 GB.
pd.testing.assert_frame_equal(gcs_df, scalars_pandas_df)
def test_to_csv_tabs(
scalars_dfs: Tuple[bigframes.dataframe.DataFrame, pd.DataFrame],
gcs_folder: str,
):
if pd.__version__.startswith("1."):
pytest.skip("date_format parameter not supported in pandas 1.x.")
"""Test the `to_csv` API with the `sep` parameter."""
scalars_df, scalars_pandas_df = scalars_dfs
index_col = typing.cast(str, scalars_df.index.name)
path = gcs_folder + "test_to_csv_tabs*.csv"
# TODO(swast): Support "date_format" parameter and make sure our
# DATETIME/TIMESTAMP column export is the same format as pandas by default.
scalars_df.to_csv(path, sep="\t", index=True)
# Pandas dataframes dtypes from read_csv are not fully compatible with
# BigQuery-backed dataframes, so manually convert the dtypes specifically
# here.
dtype = scalars_df.reset_index().dtypes.to_dict()
dtype.pop("geography_col")
dtype.pop("rowindex")
# read_csv will decode into bytes inproperly, convert_pandas_dtypes will encode properly from string
dtype.pop("bytes_col")
gcs_df = pd.read_csv(
utils.get_first_file_from_wildcard(path),
sep="\t",
dtype=dtype,
date_format={"timestamp_col": "YYYY-MM-DD HH:MM:SS Z"},
index_col=index_col,
)
utils.convert_pandas_dtypes(gcs_df, bytes_col=True)
gcs_df.index.name = scalars_df.index.name
scalars_pandas_df = scalars_pandas_df.copy()
scalars_pandas_df.index = scalars_pandas_df.index.astype("int64")
# Ordering should be maintained for tables smaller than 1 GB.
pd.testing.assert_frame_equal(gcs_df, scalars_pandas_df)
@pytest.mark.parametrize(
("index"),
[True, False],
)
@pytest.mark.skipif(pandas_gbq is None, reason="required by pd.read_gbq")
def test_to_gbq_w_index(scalars_dfs, dataset_id, index):
"""Test the `to_gbq` API with the `index` parameter."""
scalars_df, scalars_pandas_df = scalars_dfs
destination_table = f"{dataset_id}.test_index_df_to_gbq_{index}"
df_in = scalars_df.copy()
if index:
index_col = "index"
df_in.index.name = index_col
else:
index_col = None
df_in.to_gbq(destination_table, if_exists="replace", index=index)
df_out = pd.read_gbq(destination_table, index_col=index_col)
if index:
df_out = df_out.sort_index()
else:
df_out = df_out.sort_values("rowindex_2").reset_index(drop=True)
utils.convert_pandas_dtypes(df_out, bytes_col=False)
# pd.read_gbq interprets bytes_col as object, reconvert to pyarrow binary
df_out["bytes_col"] = df_out["bytes_col"].astype(pd.ArrowDtype(pa.binary()))
expected = scalars_pandas_df.copy()
expected.index.name = index_col
pd.testing.assert_frame_equal(df_out, expected, check_index_type=False)
def test_to_gbq_if_exists_is_fail(scalars_dfs, dataset_id):
scalars_df, scalars_pandas_df = scalars_dfs
destination_table = f"{dataset_id}.test_to_gbq_if_exists_is_fails"
scalars_df.to_gbq(destination_table)
gcs_df = pd.read_gbq(destination_table, index_col="rowindex")
assert len(gcs_df) == len(scalars_pandas_df)
pd.testing.assert_index_equal(gcs_df.columns, scalars_pandas_df.columns)
# Test default value is "fails"
with pytest.raises(ValueError, match="Table already exists"):
scalars_df.to_gbq(destination_table)
with pytest.raises(ValueError, match="Table already exists"):
scalars_df.to_gbq(destination_table, if_exists="fail")
def test_to_gbq_if_exists_is_replace(scalars_dfs, dataset_id):
scalars_df, scalars_pandas_df = scalars_dfs
destination_table = f"{dataset_id}.test_to_gbq_if_exists_is_replace"
scalars_df.to_gbq(destination_table)
gcs_df = pd.read_gbq(destination_table, index_col="rowindex")
assert len(gcs_df) == len(scalars_pandas_df)
pd.testing.assert_index_equal(gcs_df.columns, scalars_pandas_df.columns)
# When replacing a table with same schema
scalars_df.to_gbq(destination_table, if_exists="replace")
gcs_df = pd.read_gbq(destination_table, index_col="rowindex")
assert len(gcs_df) == len(scalars_pandas_df)
pd.testing.assert_index_equal(gcs_df.columns, scalars_pandas_df.columns)
# When replacing a table with different schema
partitial_scalars_df = scalars_df.drop(columns=["string_col"])
partitial_scalars_df.to_gbq(destination_table, if_exists="replace")
gcs_df = pd.read_gbq(destination_table, index_col="rowindex")
assert len(gcs_df) == len(partitial_scalars_df)
pd.testing.assert_index_equal(gcs_df.columns, partitial_scalars_df.columns)
def test_to_gbq_if_exists_is_append(scalars_dfs, dataset_id):
scalars_df, scalars_pandas_df = scalars_dfs
destination_table = f"{dataset_id}.test_to_gbq_if_exists_is_append"
scalars_df.to_gbq(destination_table)
gcs_df = pd.read_gbq(destination_table, index_col="rowindex")
assert len(gcs_df) == len(scalars_pandas_df)
pd.testing.assert_index_equal(gcs_df.columns, scalars_pandas_df.columns)
# When appending to a table with same schema
scalars_df.to_gbq(destination_table, if_exists="append")
gcs_df = pd.read_gbq(destination_table, index_col="rowindex")
assert len(gcs_df) == 2 * len(scalars_pandas_df)
pd.testing.assert_index_equal(gcs_df.columns, scalars_pandas_df.columns)
# When appending to a table with different schema
partitial_scalars_df = scalars_df.drop(columns=["string_col"])
partitial_scalars_df.to_gbq(destination_table, if_exists="append")
gcs_df = pd.read_gbq(destination_table, index_col="rowindex")
assert len(gcs_df) == 3 * len(partitial_scalars_df)
pd.testing.assert_index_equal(gcs_df.columns, scalars_df.columns)
def test_to_gbq_w_duplicate_column_names(
scalars_df_index, scalars_pandas_df_index, dataset_id
):
"""Test the `to_gbq` API when dealing with duplicate column names."""
destination_table = f"{dataset_id}.test_to_gbq_w_duplicate_column_names"
# Renaming 'int64_too' to 'int64_col', which will result in 'int64_too'
# becoming 'int64_col_1' after deduplication.
scalars_df_index = scalars_df_index.rename(columns={"int64_too": "int64_col"})
scalars_df_index.to_gbq(destination_table, if_exists="replace")
bf_result = bpd.read_gbq(destination_table, index_col="rowindex").to_pandas()
pd.testing.assert_series_equal(
scalars_pandas_df_index["int64_col"], bf_result["int64_col"]
)
pd.testing.assert_series_equal(
scalars_pandas_df_index["int64_too"],
bf_result["int64_col_1"],
check_names=False,
)
def test_to_gbq_w_protected_column_names(
scalars_df_index, scalars_pandas_df_index, dataset_id
):
"""
Column names can't use any of the following prefixes:
* _TABLE_
* _FILE_
* _PARTITION
* _ROW_TIMESTAMP
* __ROOT__
* _COLIDENTIFIER
See: https://cloud.google.com/bigquery/docs/schemas#column_names
"""
destination_table = f"{dataset_id}.test_to_gbq_w_protected_column_names"
scalars_df_index = scalars_df_index.rename(
columns={
"bool_col": "_Table_Suffix",
"bytes_col": "_file_path",
"date_col": "_PARTITIONDATE",
"datetime_col": "_ROW_TIMESTAMP",
"int64_col": "__ROOT__",
"int64_too": "_COLIDENTIFIER",
"numeric_col": "COLIDENTIFIER", # Create a collision at serialization time.
}
)[
[
"_Table_Suffix",
"_file_path",
"_PARTITIONDATE",
"_ROW_TIMESTAMP",
"__ROOT__",
"_COLIDENTIFIER",
"COLIDENTIFIER",
]
]
scalars_df_index.to_gbq(destination_table, if_exists="replace")
bf_result = bpd.read_gbq(destination_table, index_col="rowindex").to_pandas()
# Leading _ characters are removed to make these columns valid in BigQuery.
expected = scalars_pandas_df_index.rename(
columns={
"bool_col": "Table_Suffix",
"bytes_col": "file_path",
"date_col": "PARTITIONDATE",
"datetime_col": "ROW_TIMESTAMP",
"int64_col": "ROOT__",
"int64_too": "COLIDENTIFIER",
"numeric_col": "COLIDENTIFIER_1",
}
)[
[
"Table_Suffix",
"file_path",
"PARTITIONDATE",
"ROW_TIMESTAMP",
"ROOT__",
"COLIDENTIFIER",
"COLIDENTIFIER_1",
]
]
pd.testing.assert_frame_equal(bf_result, expected)
def test_to_gbq_w_flexible_column_names(
scalars_df_index, dataset_id: str, bigquery_client
):
"""Test the `to_gbq` API when dealing with flexible column names.
This test is for BigQuery-backed storage nodes.
See: https://cloud.google.com/bigquery/docs/schemas#flexible-column-names
"""
destination_table = f"{dataset_id}.test_to_gbq_w_flexible_column_names"
renamed_columns = {
# First column in Japanese (tests unicode).
"bool_col": "最初のカラム",
"bytes_col": "col with space",
# Dots aren't allowed in BigQuery column names, so these should be translated
"date_col": "col.with.dots",
"datetime_col": "col-with-hyphens",
"geography_col": "1start_with_number",
"int64_col": "col_with_underscore",
# Just numbers.
"int64_too": "123",
}
bf_df = scalars_df_index[renamed_columns.keys()].rename(columns=renamed_columns)
assert list(bf_df.columns) == list(renamed_columns.values())
bf_df.to_gbq(destination_table, index=False)
table = bigquery_client.get_table(destination_table)
columns = [field.name for field in table.schema]
assert columns == [
"最初のカラム",
"col with space",
# Dots aren't allowed in BigQuery column names, so these should be translated
"col_with_dots",
"col-with-hyphens",
"1start_with_number",
"col_with_underscore",
"123",
]
def test_to_gbq_w_flexible_column_names_local_node(
session, dataset_id: str, bigquery_client
):
"""Test the `to_gbq` API when dealing with flexible column names.
This test is for local nodes, e.g. read_pandas(), since those may go through
a different code path compared to data that starts in BigQuery.
See: https://cloud.google.com/bigquery/docs/schemas#flexible-column-names
"""
destination_table = f"{dataset_id}.test_to_gbq_w_flexible_column_names_local_node"
data = {
# First column in Japanese (tests unicode).
"最初のカラム": [1, 2, 3],
"col with space": [4, 5, 6],
# Dots aren't allowed in BigQuery column names, so these should be translated
"col.with.dots": [7, 8, 9],
"col-with-hyphens": [10, 11, 12],
"1start_with_number": [13, 14, 15],
"col_with_underscore": [16, 17, 18],
"123": [19, 20, 21],
}
pd_df = pd.DataFrame(data)
assert list(pd_df.columns) == list(data.keys())
bf_df = session.read_pandas(pd_df)
assert list(bf_df.columns) == list(data.keys())
bf_df.to_gbq(destination_table, index=False)
table = bigquery_client.get_table(destination_table)
columns = [field.name for field in table.schema]
assert columns == [
"最初のカラム",
"col with space",
# Dots aren't allowed in BigQuery column names, so these should be translated
"col_with_dots",
"col-with-hyphens",
"1start_with_number",
"col_with_underscore",
"123",
]
def test_to_gbq_w_None_column_names(
scalars_df_index, scalars_pandas_df_index, dataset_id
):
"""Test the `to_gbq` API with None as a column name."""
destination_table = f"{dataset_id}.test_to_gbq_w_none_column_names"
scalars_df_index = scalars_df_index.rename(columns={"int64_too": None})
scalars_df_index.to_gbq(destination_table, if_exists="replace")
bf_result = bpd.read_gbq(destination_table, index_col="rowindex").to_pandas()
pd.testing.assert_series_equal(
scalars_pandas_df_index["int64_col"], bf_result["int64_col"]
)
pd.testing.assert_series_equal(
scalars_pandas_df_index["int64_too"],
bf_result["bigframes_unnamed_column"],
check_names=False,
)
@pytest.mark.parametrize(
"clustering_columns",
[
pytest.param(["int64_col", "geography_col"]),
pytest.param(
["float64_col"],
marks=pytest.mark.xfail(raises=google.api_core.exceptions.BadRequest),
),
pytest.param(
["int64_col", "int64_col"],
marks=pytest.mark.xfail(raises=ValueError),
),
],
)
def test_to_gbq_w_clustering(
scalars_df_default_index,
dataset_id,
bigquery_client,
clustering_columns,
):
"""Test the `to_gbq` API for creating clustered tables."""
destination_table = (
f"{dataset_id}.test_to_gbq_clustering_{'_'.join(clustering_columns)}"
)
scalars_df_default_index.to_gbq(
destination_table, clustering_columns=clustering_columns
)
table = bigquery_client.get_table(destination_table)
assert list(table.clustering_fields) == clustering_columns
assert table.expires is None
def test_to_gbq_w_clustering_no_destination(
scalars_df_default_index,
bigquery_client,
):
"""Test the `to_gbq` API for creating clustered tables without destination."""
clustering_columns = ["int64_col", "geography_col"]
destination_table = scalars_df_default_index.to_gbq(
clustering_columns=clustering_columns
)
table = bigquery_client.get_table(destination_table)
assert list(table.clustering_fields) == clustering_columns
assert table.expires is not None
def test_to_gbq_w_clustering_existing_table(
scalars_df_default_index,
dataset_id,
bigquery_client,
):
destination_table = f"{dataset_id}.test_to_gbq_w_clustering_existing_table"
scalars_df_default_index.to_gbq(destination_table)
table = bigquery_client.get_table(destination_table)
assert table.clustering_fields is None
assert table.expires is None
with pytest.raises(ValueError, match="Table clustering fields cannot be changed"):
clustering_columns = ["int64_col"]
scalars_df_default_index.to_gbq(
destination_table,
if_exists="replace",
clustering_columns=clustering_columns,
)
def test_to_gbq_w_invalid_destination_table(scalars_df_index):
with pytest.raises(ValueError):
scalars_df_index.to_gbq("table_id")
def test_to_gbq_w_json(bigquery_client):
"""Test the `to_gbq` API can get a JSON column."""
s1 = bpd.Series([1, 2, 3, 4])
s2 = bpd.Series(
['"a"', "1", "false", '["a", {"b": 1}]', '{"c": [1, 2, 3]}'],
dtype=dtypes.JSON_DTYPE,
)
df = bpd.DataFrame({"id": s1, "json_col": s2})
destination_table = df.to_gbq()
table = bigquery_client.get_table(destination_table)
assert table.schema[1].name == "json_col"
assert table.schema[1].field_type == "JSON"
def test_to_gbq_with_timedelta(bigquery_client, dataset_id):
destination_table = f"{dataset_id}.test_to_gbq_with_timedelta"
s1 = bpd.Series([1, 2, 3, 4])
s2 = bpd.to_timedelta(bpd.Series([1, 2, 3, 4]), unit="s")
df = bpd.DataFrame({"id": s1, "timedelta_col": s2})
df.to_gbq(destination_table)
table = bigquery_client.get_table(destination_table)
assert table.schema[1].name == "timedelta_col"
assert table.schema[1].field_type == "INTEGER"
assert dtypes.TIMEDELTA_DESCRIPTION_TAG in table.schema[1].description
def test_gbq_round_trip_with_timedelta(session, dataset_id):
destination_table = f"{dataset_id}.test_gbq_roundtrip_with_timedelta"
df = pd.DataFrame(
{
"col_1": [1],
"col_2": [pd.Timedelta(1, "s")],
"col_3": [1.1],
}
)
bpd.DataFrame(df).to_gbq(destination_table)
result = session.read_gbq(destination_table)
assert result["col_1"].dtype == dtypes.INT_DTYPE
assert result["col_2"].dtype == dtypes.TIMEDELTA_DTYPE
assert result["col_3"].dtype == dtypes.FLOAT_DTYPE
def test_to_gbq_timedelta_tag_ignored_when_appending(bigquery_client, dataset_id):
# First, create a table
destination_table = f"{dataset_id}.test_to_gbq_timedelta_tag_ignored_when_appending"
schema = [bigquery.SchemaField("my_col", "INTEGER")]
bigquery_client.create_table(bigquery.Table(destination_table, schema))
# Then, append to that table with timedelta values
df = pd.DataFrame(
{
"my_col": [pd.Timedelta(1, "s")],
}
)
bpd.DataFrame(df).to_gbq(destination_table, if_exists="append")
table = bigquery_client.get_table(destination_table)
assert table.schema[0].name == "my_col"
assert table.schema[0].field_type == "INTEGER"
assert table.schema[0].description is None
@pytest.mark.parametrize(
("index"),
[True, False],
)
def test_to_json_index_invalid_orient(
scalars_dfs: Tuple[bigframes.dataframe.DataFrame, pd.DataFrame],
gcs_folder: str,
index: bool,
):
scalars_df, _ = scalars_dfs
path = gcs_folder + f"test_index_df_to_json_index_{index}*.jsonl"
with pytest.raises(ValueError):
scalars_df.to_json(path, index=index, lines=True)
@pytest.mark.parametrize(
("index"),
[True, False],
)
def test_to_json_index_invalid_lines(
scalars_dfs: Tuple[bigframes.dataframe.DataFrame, pd.DataFrame],
gcs_folder: str,
index: bool,
):
scalars_df, _ = scalars_dfs
path = gcs_folder + f"test_index_df_to_json_index_{index}.jsonl"
with pytest.raises(NotImplementedError):
scalars_df.to_json(path, index=index)
@pytest.mark.parametrize(
("index"),
[True, False],
)
def test_to_json_index_records_orient(
scalars_dfs: Tuple[bigframes.dataframe.DataFrame, pd.DataFrame],
gcs_folder: str,
index: bool,
):
"""Test the `to_json` API with the `index` parameter.
Uses the scalable options orient='records' and lines=True.
"""
scalars_df, scalars_pandas_df = scalars_dfs
path = gcs_folder + f"test_index_df_to_json_index_{index}*.jsonl"
scalars_df.to_json(path, index=index, orient="records", lines=True)
gcs_df = pd.read_json(
utils.get_first_file_from_wildcard(path),
lines=True,
convert_dates=["datetime_col"],
)
utils.convert_pandas_dtypes(gcs_df, bytes_col=True)
if index and scalars_df.index.name is not None:
gcs_df = gcs_df.set_index(scalars_df.index.name)
assert len(gcs_df.index) == len(scalars_pandas_df.index)
pd.testing.assert_index_equal(gcs_df.columns, scalars_pandas_df.columns)
gcs_df.index.name = scalars_df.index.name
gcs_df.index = gcs_df.index.astype("Int64")
scalars_pandas_df.index = scalars_pandas_df.index.astype("Int64")
# Ordering should be maintained for tables smaller than 1 GB.
pd.testing.assert_frame_equal(gcs_df, scalars_pandas_df)
@pytest.mark.parametrize(
("index"),
[True, False],
)
def test_to_parquet_index(scalars_dfs, gcs_folder, index):
"""Test the `to_parquet` API with the `index` parameter."""
scalars_df, scalars_pandas_df = scalars_dfs
scalars_pandas_df = scalars_pandas_df.copy()
path = gcs_folder + f"test_index_df_to_parquet_{index}*.parquet"
# TODO(b/268693993): Type GEOGRAPHY is not currently supported for parquet.
scalars_df = scalars_df.drop(columns="geography_col")