-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathtest_series.py
3953 lines (3112 loc) · 118 KB
/
test_series.py
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.
import datetime as dt
import math
import re
import tempfile
import geopandas as gpd # type: ignore
import numpy
from packaging.version import Version
import pandas as pd
import pyarrow as pa # type: ignore
import pytest
import bigframes.pandas
import bigframes.series as series
from tests.system.utils import (
assert_pandas_df_equal,
assert_series_equal,
get_first_file_from_wildcard,
skip_legacy_pandas,
)
def test_series_construct_copy(scalars_dfs):
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = series.Series(
scalars_df["int64_col"], name="test_series", dtype="Float64"
).to_pandas()
pd_result = pd.Series(
scalars_pandas_df["int64_col"], name="test_series", dtype="Float64"
)
pd.testing.assert_series_equal(bf_result, pd_result)
def test_series_construct_nullable_ints():
bf_result = series.Series(
[1, 3, bigframes.pandas.NA], index=[0, 4, bigframes.pandas.NA]
).to_pandas()
# TODO(b/340885567): fix type error
expected_index = pd.Index( # type: ignore
[0, 4, None],
dtype=pd.Int64Dtype(),
)
expected = pd.Series([1, 3, pd.NA], dtype=pd.Int64Dtype(), index=expected_index)
pd.testing.assert_series_equal(bf_result, expected)
def test_series_construct_timestamps():
datetimes = [
dt.datetime(2020, 1, 20, 20, 20, 20, 20),
dt.datetime(2019, 1, 20, 20, 20, 20, 20),
None,
]
bf_result = series.Series(datetimes).to_pandas()
pd_result = pd.Series(datetimes, dtype=pd.ArrowDtype(pa.timestamp("us")))
pd.testing.assert_series_equal(bf_result, pd_result, check_index_type=False)
def test_series_construct_copy_with_index(scalars_dfs):
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = series.Series(
scalars_df["int64_col"],
name="test_series",
dtype="Float64",
index=scalars_df["int64_too"],
).to_pandas()
pd_result = pd.Series(
scalars_pandas_df["int64_col"],
name="test_series",
dtype="Float64",
index=scalars_pandas_df["int64_too"],
)
pd.testing.assert_series_equal(bf_result, pd_result)
def test_series_construct_copy_index(scalars_dfs):
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = series.Series(
scalars_df.index,
name="test_series",
dtype="Float64",
index=scalars_df["int64_too"],
).to_pandas()
pd_result = pd.Series(
scalars_pandas_df.index,
name="test_series",
dtype="Float64",
index=scalars_pandas_df["int64_too"],
)
pd.testing.assert_series_equal(bf_result, pd_result)
def test_series_construct_pandas(scalars_dfs):
_, scalars_pandas_df = scalars_dfs
bf_result = series.Series(
scalars_pandas_df["int64_col"], name="test_series", dtype="Float64"
)
pd_result = pd.Series(
scalars_pandas_df["int64_col"], name="test_series", dtype="Float64"
)
assert bf_result.shape == pd_result.shape
pd.testing.assert_series_equal(bf_result.to_pandas(), pd_result)
def test_series_construct_from_list():
bf_result = series.Series([1, 1, 2, 3, 5, 8, 13], dtype="Int64").to_pandas()
pd_result = pd.Series([1, 1, 2, 3, 5, 8, 13], dtype="Int64")
# BigQuery DataFrame default indices use nullable Int64 always
pd_result.index = pd_result.index.astype("Int64")
pd.testing.assert_series_equal(bf_result, pd_result)
def test_series_construct_reindex():
bf_result = series.Series(
series.Series({1: 10, 2: 30, 3: 30}), index=[3, 2], dtype="Int64"
).to_pandas()
pd_result = pd.Series(pd.Series({1: 10, 2: 30, 3: 30}), index=[3, 2], dtype="Int64")
# BigQuery DataFrame default indices use nullable Int64 always
pd_result.index = pd_result.index.astype("Int64")
pd.testing.assert_series_equal(bf_result, pd_result)
def test_series_construct_from_list_w_index():
bf_result = series.Series(
[1, 1, 2, 3, 5, 8, 13], index=[10, 20, 30, 40, 50, 60, 70], dtype="Int64"
).to_pandas()
pd_result = pd.Series(
[1, 1, 2, 3, 5, 8, 13], index=[10, 20, 30, 40, 50, 60, 70], dtype="Int64"
)
# BigQuery DataFrame default indices use nullable Int64 always
pd_result.index = pd_result.index.astype("Int64")
pd.testing.assert_series_equal(bf_result, pd_result)
def test_series_construct_empty(session: bigframes.Session):
bf_series: series.Series = series.Series(session=session)
pd_series: pd.Series = pd.Series()
bf_result = bf_series.empty
pd_result = pd_series.empty
assert pd_result
assert bf_result == pd_result
def test_series_construct_scalar_no_index():
bf_result = series.Series("hello world", dtype="string[pyarrow]").to_pandas()
pd_result = pd.Series("hello world", dtype="string[pyarrow]")
# BigQuery DataFrame default indices use nullable Int64 always
pd_result.index = pd_result.index.astype("Int64")
pd.testing.assert_series_equal(bf_result, pd_result)
def test_series_construct_scalar_w_index():
bf_result = series.Series(
"hello world", dtype="string[pyarrow]", index=[0, 2, 1]
).to_pandas()
pd_result = pd.Series("hello world", dtype="string[pyarrow]", index=[0, 2, 1])
# BigQuery DataFrame default indices use nullable Int64 always
pd_result.index = pd_result.index.astype("Int64")
pd.testing.assert_series_equal(bf_result, pd_result)
def test_series_construct_nan():
bf_result = series.Series(numpy.nan).to_pandas()
pd_result = pd.Series(numpy.nan)
pd_result.index = pd_result.index.astype("Int64")
pd_result = pd_result.astype("Float64")
pd.testing.assert_series_equal(bf_result, pd_result)
def test_series_construct_from_list_escaped_strings():
"""Check that special characters are supported."""
strings = [
"string\nwith\nnewline",
"string\twith\ttabs",
"string\\with\\backslashes",
]
bf_result = series.Series(strings, name="test_series", dtype="string[pyarrow]")
pd_result = pd.Series(strings, name="test_series", dtype="string[pyarrow]")
# BigQuery DataFrame default indices use nullable Int64 always
pd_result.index = pd_result.index.astype("Int64")
pd.testing.assert_series_equal(bf_result.to_pandas(), pd_result)
@pytest.mark.parametrize(
["col_name", "expected_dtype"],
[
("bool_col", pd.BooleanDtype()),
# TODO(swast): Use a more efficient type.
("bytes_col", pd.ArrowDtype(pa.binary())),
("date_col", pd.ArrowDtype(pa.date32())),
("datetime_col", pd.ArrowDtype(pa.timestamp("us"))),
("float64_col", pd.Float64Dtype()),
("geography_col", gpd.array.GeometryDtype()),
("int64_col", pd.Int64Dtype()),
# TODO(swast): Use a more efficient type.
("numeric_col", pd.ArrowDtype(pa.decimal128(38, 9))),
("int64_too", pd.Int64Dtype()),
("string_col", pd.StringDtype(storage="pyarrow")),
("time_col", pd.ArrowDtype(pa.time64("us"))),
("timestamp_col", pd.ArrowDtype(pa.timestamp("us", tz="UTC"))),
],
)
def test_get_column(scalars_dfs, col_name, expected_dtype):
scalars_df, scalars_pandas_df = scalars_dfs
series = scalars_df[col_name]
series_pandas = series.to_pandas()
assert series_pandas.dtype == expected_dtype
assert series_pandas.shape[0] == scalars_pandas_df.shape[0]
def test_series_get_column_default(scalars_dfs):
scalars_df, _ = scalars_dfs
result = scalars_df.get(123123123123123, "default_val")
assert result == "default_val"
def test_series_equals_identical(scalars_df_index, scalars_pandas_df_index):
bf_result = scalars_df_index.int64_col.equals(scalars_df_index.int64_col)
pd_result = scalars_pandas_df_index.int64_col.equals(
scalars_pandas_df_index.int64_col
)
assert pd_result == bf_result
def test_series_equals_df(scalars_df_index, scalars_pandas_df_index):
bf_result = scalars_df_index["int64_col"].equals(scalars_df_index[["int64_col"]])
pd_result = scalars_pandas_df_index["int64_col"].equals(
scalars_pandas_df_index[["int64_col"]]
)
assert pd_result == bf_result
def test_series_equals_different_dtype(scalars_df_index, scalars_pandas_df_index):
bf_series = scalars_df_index["int64_col"]
pd_series = scalars_pandas_df_index["int64_col"]
bf_result = bf_series.equals(bf_series.astype("Float64"))
pd_result = pd_series.equals(pd_series.astype("Float64"))
assert pd_result == bf_result
def test_series_equals_different_values(scalars_df_index, scalars_pandas_df_index):
bf_series = scalars_df_index["int64_col"]
pd_series = scalars_pandas_df_index["int64_col"]
bf_result = bf_series.equals(bf_series + 1)
pd_result = pd_series.equals(pd_series + 1)
assert pd_result == bf_result
def test_series_get_with_default_index(scalars_dfs):
col_name = "float64_col"
key = 2
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = scalars_df[col_name].get(key)
pd_result = scalars_pandas_df[col_name].get(key)
assert bf_result == pd_result
@pytest.mark.parametrize(
("index_col", "key"),
(
("int64_too", 2),
("string_col", "Hello, World!"),
("int64_too", slice(2, 6)),
),
)
def test_series___getitem__(scalars_dfs, index_col, key):
col_name = "float64_col"
scalars_df, scalars_pandas_df = scalars_dfs
scalars_df = scalars_df.set_index(index_col, drop=False)
scalars_pandas_df = scalars_pandas_df.set_index(index_col, drop=False)
bf_result = scalars_df[col_name][key]
pd_result = scalars_pandas_df[col_name][key]
pd.testing.assert_series_equal(bf_result.to_pandas(), pd_result)
@pytest.mark.parametrize(
("key",),
(
(-2,),
(-1,),
(0,),
(1,),
),
)
def test_series___getitem___with_int_key(scalars_dfs, key):
col_name = "int64_too"
index_col = "string_col"
scalars_df, scalars_pandas_df = scalars_dfs
scalars_df = scalars_df.set_index(index_col, drop=False)
scalars_pandas_df = scalars_pandas_df.set_index(index_col, drop=False)
bf_result = scalars_df[col_name][key]
pd_result = scalars_pandas_df[col_name][key]
assert bf_result == pd_result
def test_series___getitem___with_default_index(scalars_dfs):
col_name = "float64_col"
key = 2
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = scalars_df[col_name][key]
pd_result = scalars_pandas_df[col_name][key]
assert bf_result == pd_result
@pytest.mark.parametrize(
("col_name",),
(
("float64_col",),
("int64_too",),
),
)
def test_abs(scalars_dfs, col_name):
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = scalars_df[col_name].abs().to_pandas()
pd_result = scalars_pandas_df[col_name].abs()
assert_series_equal(pd_result, bf_result)
@pytest.mark.parametrize(
("col_name",),
(
("float64_col",),
("int64_too",),
),
)
def test_series_pos(scalars_dfs, col_name):
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = (+scalars_df[col_name]).to_pandas()
pd_result = +scalars_pandas_df[col_name]
assert_series_equal(pd_result, bf_result)
@pytest.mark.parametrize(
("col_name",),
(
("float64_col",),
("int64_too",),
),
)
def test_series_neg(scalars_dfs, col_name):
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = (-scalars_df[col_name]).to_pandas()
pd_result = -scalars_pandas_df[col_name]
assert_series_equal(pd_result, bf_result)
@pytest.mark.parametrize(
("col_name",),
(
("bool_col",),
("int64_col",),
),
)
def test_series_invert(scalars_dfs, col_name):
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = (~scalars_df[col_name]).to_pandas()
pd_result = ~scalars_pandas_df[col_name]
assert_series_equal(pd_result, bf_result)
def test_fillna(scalars_dfs):
scalars_df, scalars_pandas_df = scalars_dfs
col_name = "string_col"
bf_result = scalars_df[col_name].fillna("Missing").to_pandas()
pd_result = scalars_pandas_df[col_name].fillna("Missing")
assert_series_equal(
pd_result,
bf_result,
)
def test_series_replace_scalar_scalar(scalars_dfs):
scalars_df, scalars_pandas_df = scalars_dfs
col_name = "string_col"
bf_result = (
scalars_df[col_name].replace("Hello, World!", "Howdy, Planet!").to_pandas()
)
pd_result = scalars_pandas_df[col_name].replace("Hello, World!", "Howdy, Planet!")
pd.testing.assert_series_equal(
pd_result,
bf_result,
)
def test_series_replace_regex_scalar(scalars_dfs):
scalars_df, scalars_pandas_df = scalars_dfs
col_name = "string_col"
bf_result = (
scalars_df[col_name].replace("^H.l", "Howdy, Planet!", regex=True).to_pandas()
)
pd_result = scalars_pandas_df[col_name].replace(
"^H.l", "Howdy, Planet!", regex=True
)
pd.testing.assert_series_equal(
pd_result,
bf_result,
)
def test_series_replace_list_scalar(scalars_dfs):
scalars_df, scalars_pandas_df = scalars_dfs
col_name = "string_col"
bf_result = (
scalars_df[col_name]
.replace(["Hello, World!", "T"], "Howdy, Planet!")
.to_pandas()
)
pd_result = scalars_pandas_df[col_name].replace(
["Hello, World!", "T"], "Howdy, Planet!"
)
pd.testing.assert_series_equal(
pd_result,
bf_result,
)
@pytest.mark.parametrize(
("replacement_dict",),
(
({"Hello, World!": "Howdy, Planet!", "T": "R"},),
({},),
),
ids=[
"non-empty",
"empty",
],
)
def test_series_replace_dict(scalars_dfs, replacement_dict):
scalars_df, scalars_pandas_df = scalars_dfs
col_name = "string_col"
bf_result = scalars_df[col_name].replace(replacement_dict).to_pandas()
pd_result = scalars_pandas_df[col_name].replace(replacement_dict)
pd.testing.assert_series_equal(
pd_result,
bf_result,
)
@pytest.mark.parametrize(
("method",),
(
("linear",),
("values",),
("slinear",),
("nearest",),
("zero",),
("pad",),
),
)
def test_series_interpolate(method):
values = [None, 1, 2, None, None, 16, None]
index = [-3.2, 11.4, 3.56, 4, 4.32, 5.55, 76.8]
pd_series = pd.Series(values, index)
bf_series = series.Series(pd_series)
# Pandas can only interpolate on "float64" columns
# https://github.com/pandas-dev/pandas/issues/40252
pd_result = pd_series.astype("float64").interpolate(method=method)
bf_result = bf_series.interpolate(method=method).to_pandas()
# pd uses non-null types, while bf uses nullable types
pd.testing.assert_series_equal(
pd_result,
bf_result,
check_index_type=False,
check_dtype=False,
)
@pytest.mark.parametrize(
("ignore_index",),
(
(True,),
(False,),
),
)
def test_series_dropna(scalars_dfs, ignore_index):
if pd.__version__.startswith("1."):
pytest.skip("ignore_index parameter not supported in pandas 1.x.")
scalars_df, scalars_pandas_df = scalars_dfs
col_name = "string_col"
bf_result = scalars_df[col_name].dropna(ignore_index=ignore_index).to_pandas()
pd_result = scalars_pandas_df[col_name].dropna(ignore_index=ignore_index)
pd.testing.assert_series_equal(pd_result, bf_result, check_index_type=False)
@pytest.mark.parametrize(
("agg",),
(
("sum",),
("size",),
),
)
def test_series_agg_single_string(scalars_dfs, agg):
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = scalars_df["int64_col"].agg(agg)
pd_result = scalars_pandas_df["int64_col"].agg(agg)
assert math.isclose(pd_result, bf_result)
def test_series_agg_multi_string(scalars_dfs):
aggregations = [
"sum",
"mean",
"std",
"var",
"min",
"max",
"nunique",
"count",
"size",
]
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = scalars_df["int64_col"].agg(aggregations).to_pandas()
pd_result = scalars_pandas_df["int64_col"].agg(aggregations)
# Pandas may produce narrower numeric types, but bigframes always produces Float64
pd_result = pd_result.astype("Float64")
pd.testing.assert_series_equal(pd_result, bf_result, check_index_type=False)
@pytest.mark.parametrize(
("col_name",),
(
("string_col",),
("int64_col",),
),
)
def test_max(scalars_dfs, col_name):
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = scalars_df[col_name].max()
pd_result = scalars_pandas_df[col_name].max()
assert pd_result == bf_result
@pytest.mark.parametrize(
("col_name",),
(
("string_col",),
("int64_col",),
),
)
def test_min(scalars_dfs, col_name):
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = scalars_df[col_name].min()
pd_result = scalars_pandas_df[col_name].min()
assert pd_result == bf_result
@pytest.mark.parametrize(
("col_name",),
(
("float64_col",),
("int64_col",),
),
)
def test_std(scalars_dfs, col_name):
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = scalars_df[col_name].std()
pd_result = scalars_pandas_df[col_name].std()
assert math.isclose(pd_result, bf_result)
@pytest.mark.parametrize(
("col_name",),
(
("float64_col",),
("int64_col",),
),
)
def test_kurt(scalars_dfs, col_name):
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = scalars_df[col_name].kurt()
pd_result = scalars_pandas_df[col_name].kurt()
assert math.isclose(pd_result, bf_result)
@pytest.mark.parametrize(
("col_name",),
(
("float64_col",),
("int64_col",),
),
)
def test_skew(scalars_dfs, col_name):
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = scalars_df[col_name].skew()
pd_result = scalars_pandas_df[col_name].skew()
assert math.isclose(pd_result, bf_result)
def test_skew_undefined(scalars_dfs):
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = scalars_df["int64_col"].iloc[:2].skew()
pd_result = scalars_pandas_df["int64_col"].iloc[:2].skew()
# both should be pd.NA
assert pd_result is bf_result
def test_kurt_undefined(scalars_dfs):
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = scalars_df["int64_col"].iloc[:3].kurt()
pd_result = scalars_pandas_df["int64_col"].iloc[:3].kurt()
# both should be pd.NA
assert pd_result is bf_result
@pytest.mark.parametrize(
("col_name",),
(
("float64_col",),
("int64_col",),
),
)
def test_var(scalars_dfs, col_name):
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = scalars_df[col_name].var()
pd_result = scalars_pandas_df[col_name].var()
assert math.isclose(pd_result, bf_result)
@pytest.mark.parametrize(
("col_name",),
(
("bool_col",),
("int64_col",),
),
)
def test_mode_stat(scalars_df_index, scalars_pandas_df_index, col_name):
bf_result = scalars_df_index[col_name].mode().to_pandas()
pd_result = scalars_pandas_df_index[col_name].mode()
## Mode implicitly resets index, and bigframes default indices use nullable Int64
pd_result.index = pd_result.index.astype("Int64")
pd.testing.assert_series_equal(
bf_result,
pd_result,
)
@pytest.mark.parametrize(
("operator"),
[
(lambda x, y: x + y),
(lambda x, y: x - y),
(lambda x, y: x * y),
(lambda x, y: x / y),
(lambda x, y: x // y),
(lambda x, y: x < y),
(lambda x, y: x > y),
(lambda x, y: x <= y),
(lambda x, y: x >= y),
],
ids=[
"add",
"subtract",
"multiply",
"divide",
"floordivide",
"less_than",
"greater_than",
"less_than_equal",
"greater_than_equal",
],
)
@pytest.mark.parametrize(("other_scalar"), [-1, 0, 14, pd.NA])
@pytest.mark.parametrize(("reverse_operands"), [True, False])
def test_series_int_int_operators_scalar(
scalars_dfs, operator, other_scalar, reverse_operands
):
scalars_df, scalars_pandas_df = scalars_dfs
maybe_reversed_op = (lambda x, y: operator(y, x)) if reverse_operands else operator
bf_result = maybe_reversed_op(scalars_df["int64_col"], other_scalar).to_pandas()
pd_result = maybe_reversed_op(scalars_pandas_df["int64_col"], other_scalar)
assert_series_equal(pd_result, bf_result)
def test_series_pow_scalar(scalars_dfs):
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = (scalars_df["int64_col"] ** 2).to_pandas()
pd_result = scalars_pandas_df["int64_col"] ** 2
assert_series_equal(pd_result, bf_result)
def test_series_pow_scalar_reverse(scalars_dfs):
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = (0.8 ** scalars_df["int64_col"]).to_pandas()
pd_result = 0.8 ** scalars_pandas_df["int64_col"]
assert_series_equal(pd_result, bf_result)
@pytest.mark.parametrize(
("operator"),
[
(lambda x, y: x & y),
(lambda x, y: x | y),
(lambda x, y: x ^ y),
],
ids=[
"and",
"or",
"xor",
],
)
@pytest.mark.parametrize(("other_scalar"), [True, False, pd.NA])
@pytest.mark.parametrize(("reverse_operands"), [True, False])
def test_series_bool_bool_operators_scalar(
scalars_dfs, operator, other_scalar, reverse_operands
):
scalars_df, scalars_pandas_df = scalars_dfs
maybe_reversed_op = (lambda x, y: operator(y, x)) if reverse_operands else operator
bf_result = maybe_reversed_op(scalars_df["bool_col"], other_scalar).to_pandas()
pd_result = maybe_reversed_op(scalars_pandas_df["bool_col"], other_scalar)
assert_series_equal(pd_result.astype(pd.BooleanDtype()), bf_result)
@pytest.mark.parametrize(
("operator"),
[
(lambda x, y: x + y),
(lambda x, y: x - y),
(lambda x, y: x * y),
(lambda x, y: x / y),
(lambda x, y: x < y),
(lambda x, y: x > y),
(lambda x, y: x <= y),
(lambda x, y: x >= y),
(lambda x, y: x % y),
(lambda x, y: x // y),
(lambda x, y: x & y),
(lambda x, y: x | y),
(lambda x, y: x ^ y),
],
ids=[
"add",
"subtract",
"multiply",
"divide",
"less_than",
"greater_than",
"less_than_equal",
"greater_than_equal",
"modulo",
"floordivide",
"bitwise_and",
"bitwise_or",
"bitwise_xor",
],
)
def test_series_int_int_operators_series(scalars_dfs, operator):
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = operator(scalars_df["int64_col"], scalars_df["int64_too"]).to_pandas()
pd_result = operator(scalars_pandas_df["int64_col"], scalars_pandas_df["int64_too"])
assert_series_equal(pd_result, bf_result)
@pytest.mark.parametrize(
("col_x",),
[
("int64_col",),
("int64_too",),
("float64_col",),
],
)
@pytest.mark.parametrize(
("col_y",),
[
("int64_col",),
("int64_too",),
("float64_col",),
],
)
@pytest.mark.parametrize(
("method",),
[
("mod",),
("rmod",),
],
)
def test_mods(scalars_dfs, col_x, col_y, method):
scalars_df, scalars_pandas_df = scalars_dfs
x_bf = scalars_df[col_x]
y_bf = scalars_df[col_y]
bf_series = getattr(x_bf, method)(y_bf)
# BigQuery's mod functions return [BIG]NUMERIC values unless both arguments are integers.
# https://cloud.google.com/bigquery/docs/reference/standard-sql/mathematical_functions#mod
if x_bf.dtype == pd.Int64Dtype() and y_bf.dtype == pd.Int64Dtype():
bf_result = bf_series.to_pandas()
else:
bf_result = bf_series.astype("Float64").to_pandas()
pd_result = getattr(scalars_pandas_df[col_x], method)(scalars_pandas_df[col_y])
pd.testing.assert_series_equal(pd_result, bf_result)
# We work around a pandas bug that doesn't handle correlating nullable dtypes by doing this
# manually with dumb self-correlation instead of parameterized as test_mods is above.
def test_series_corr(scalars_dfs):
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = scalars_df["int64_too"].corr(scalars_df["int64_too"])
pd_result = (
scalars_pandas_df["int64_too"]
.astype("int64")
.corr(scalars_pandas_df["int64_too"].astype("int64"))
)
assert math.isclose(pd_result, bf_result)
@skip_legacy_pandas
def test_series_autocorr(scalars_dfs):
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = scalars_df["float64_col"].autocorr(2)
pd_result = scalars_pandas_df["float64_col"].autocorr(2)
assert math.isclose(pd_result, bf_result)
def test_series_cov(scalars_dfs):
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = scalars_df["int64_too"].cov(scalars_df["int64_too"])
pd_result = (
scalars_pandas_df["int64_too"]
.astype("int64")
.cov(scalars_pandas_df["int64_too"].astype("int64"))
)
assert math.isclose(pd_result, bf_result)
@pytest.mark.parametrize(
("col_x",),
[
("int64_col",),
("float64_col",),
],
)
@pytest.mark.parametrize(
("col_y",),
[
("int64_col",),
("float64_col",),
],
)
@pytest.mark.parametrize(
("method",),
[
("divmod",),
("rdivmod",),
],
)
def test_divmods_series(scalars_dfs, col_x, col_y, method):
scalars_df, scalars_pandas_df = scalars_dfs
bf_div_result, bf_mod_result = getattr(scalars_df[col_x], method)(scalars_df[col_y])
pd_div_result, pd_mod_result = getattr(scalars_pandas_df[col_x], method)(
scalars_pandas_df[col_y]
)
# BigQuery's mod functions return NUMERIC values for non-INT64 inputs.
if bf_div_result.dtype == pd.Int64Dtype():
pd.testing.assert_series_equal(pd_div_result, bf_div_result.to_pandas())
else:
pd.testing.assert_series_equal(
pd_div_result, bf_div_result.astype("Float64").to_pandas()
)
if bf_mod_result.dtype == pd.Int64Dtype():
pd.testing.assert_series_equal(pd_mod_result, bf_mod_result.to_pandas())
else:
pd.testing.assert_series_equal(
pd_mod_result, bf_mod_result.astype("Float64").to_pandas()
)
@pytest.mark.parametrize(
("col_x",),
[
("int64_col",),
("float64_col",),
],
)
@pytest.mark.parametrize(
("other",),
[
(-1000,),
(678,),
],
)
@pytest.mark.parametrize(
("method",),
[
("divmod",),
("rdivmod",),
],
)
def test_divmods_scalars(scalars_dfs, col_x, other, method):
scalars_df, scalars_pandas_df = scalars_dfs
bf_div_result, bf_mod_result = getattr(scalars_df[col_x], method)(other)
pd_div_result, pd_mod_result = getattr(scalars_pandas_df[col_x], method)(other)
# BigQuery's mod functions return NUMERIC values for non-INT64 inputs.
if bf_div_result.dtype == pd.Int64Dtype():
pd.testing.assert_series_equal(pd_div_result, bf_div_result.to_pandas())
else:
pd.testing.assert_series_equal(
pd_div_result, bf_div_result.astype("Float64").to_pandas()
)
if bf_mod_result.dtype == pd.Int64Dtype():
pd.testing.assert_series_equal(pd_mod_result, bf_mod_result.to_pandas())
else:
pd.testing.assert_series_equal(
pd_mod_result, bf_mod_result.astype("Float64").to_pandas()
)
@pytest.mark.parametrize(
("other",),
[
(3,),
(-6.2,),
],
)
def test_series_add_scalar(scalars_dfs, other):
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = (scalars_df["float64_col"] + other).to_pandas()
pd_result = scalars_pandas_df["float64_col"] + other
assert_series_equal(pd_result, bf_result)
@pytest.mark.parametrize(
("left_col", "right_col"),
[
("float64_col", "float64_col"),
("int64_col", "float64_col"),
("int64_col", "int64_too"),
],
)
def test_series_add_bigframes_series(scalars_dfs, left_col, right_col):
scalars_df, scalars_pandas_df = scalars_dfs
bf_result = (scalars_df[left_col] + scalars_df[right_col]).to_pandas()
pd_result = scalars_pandas_df[left_col] + scalars_pandas_df[right_col]
assert_series_equal(pd_result, bf_result)
@pytest.mark.parametrize(