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 68
Expand file tree
/
Copy pathtest_null_index.py
More file actions
436 lines (347 loc) · 15.6 KB
/
test_null_index.py
File metadata and controls
436 lines (347 loc) · 15.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
# Copyright 2024 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 io
import pandas as pd
import pytest
import bigframes.exceptions
import bigframes.pandas as bpd
def test_null_index_to_gbq(session, scalars_df_null_index, dataset_id_not_created):
dataset_id = dataset_id_not_created
destination_table = f"{dataset_id}.scalars_df_unindexed"
result_table = scalars_df_null_index.to_gbq(
destination_table, clustering_columns=["int64_col"]
)
assert (
result_table == destination_table
if destination_table
else result_table is not None
)
loaded_scalars_df_index = session.read_gbq(result_table)
assert not loaded_scalars_df_index.empty
def test_null_index_materialize(scalars_df_null_index, scalars_pandas_df_default_index):
bf_result = scalars_df_null_index.to_pandas()
pd.testing.assert_frame_equal(
bf_result, scalars_pandas_df_default_index, check_index_type=False
)
def test_null_index_info(scalars_df_null_index):
expected = (
"<class 'bigframes.dataframe.DataFrame'>\n"
"NullIndex\n"
"Data columns (total 14 columns):\n"
" # Column Non-Null Count Dtype\n"
"--- ------------- ---------------- ------------------------------\n"
" 0 bool_col 8 non-null boolean\n"
" 1 bytes_col 6 non-null binary[pyarrow]\n"
" 2 date_col 7 non-null date32[day][pyarrow]\n"
" 3 datetime_col 6 non-null timestamp[us][pyarrow]\n"
" 4 geography_col 4 non-null geometry\n"
" 5 int64_col 8 non-null Int64\n"
" 6 int64_too 9 non-null Int64\n"
" 7 numeric_col 6 non-null decimal128(38, 9)[pyarrow]\n"
" 8 float64_col 7 non-null Float64\n"
" 9 rowindex_2 9 non-null Int64\n"
" 10 string_col 8 non-null string\n"
" 11 time_col 6 non-null time64[us][pyarrow]\n"
" 12 timestamp_col 6 non-null timestamp[us, tz=UTC][pyarrow]\n"
" 13 duration_col 7 non-null duration[us][pyarrow]\n"
"dtypes: Float64(1), Int64(3), binary[pyarrow](1), boolean(1), date32[day][pyarrow](1), decimal128(38, 9)[pyarrow](1), duration[us][pyarrow](1), geometry(1), string(1), time64[us][pyarrow](1), timestamp[us, tz=UTC][pyarrow](1), timestamp[us][pyarrow](1)\n"
"memory usage: 1269 bytes\n"
)
bf_result = io.StringIO()
scalars_df_null_index.drop(columns="rowindex").info(buf=bf_result)
assert expected == bf_result.getvalue()
def test_null_index_series_repr(scalars_df_null_index, scalars_pandas_df_default_index):
bf_result = scalars_df_null_index["int64_too"].head(5).__repr__()
pd_result = (
scalars_pandas_df_default_index["int64_too"]
.head(5)
.to_string(dtype=True, index=False, length=False, name=True)
)
assert bf_result == pd_result
def test_null_index_dataframe_repr(
scalars_df_null_index, scalars_pandas_df_default_index
):
bf_result = scalars_df_null_index[["int64_too", "int64_col"]].head(5).__repr__()
pd_result = (
scalars_pandas_df_default_index[["int64_too", "int64_col"]]
.head(5)
.to_string(index=False)
)
assert bf_result == pd_result + "\n\n[5 rows x 2 columns]"
def test_null_index_reset_index(scalars_df_null_index, scalars_pandas_df_default_index):
bf_result = scalars_df_null_index.reset_index().to_pandas()
pd_result = scalars_pandas_df_default_index.reset_index(drop=True)
pd.testing.assert_frame_equal(bf_result, pd_result, check_index_type=False)
def test_null_index_set_index(scalars_df_null_index, scalars_pandas_df_default_index):
bf_result = scalars_df_null_index.set_index("int64_col").to_pandas()
pd_result = scalars_pandas_df_default_index.set_index("int64_col")
pd.testing.assert_frame_equal(bf_result, pd_result)
def test_null_index_concat(scalars_df_null_index, scalars_pandas_df_default_index):
bf_result = bpd.concat(
[scalars_df_null_index, scalars_df_null_index], axis=0
).to_pandas()
pd_result = pd.concat(
[scalars_pandas_df_default_index, scalars_pandas_df_default_index], axis=0
)
pd.testing.assert_frame_equal(bf_result, pd_result.reset_index(drop=True))
def test_null_index_aggregate(scalars_df_null_index, scalars_pandas_df_default_index):
bf_result = scalars_df_null_index.count().to_pandas()
pd_result = scalars_pandas_df_default_index.count()
pd_result.index = pd_result.index.astype("string[pyarrow]")
pd.testing.assert_series_equal(
bf_result, pd_result, check_dtype=False, check_index_type=False
)
def test_null_index_binop_series_axis_0(
scalars_df_null_index, scalars_pandas_df_default_index
):
bf_result = (
scalars_df_null_index[["int64_col", "int64_too"]]
.add(scalars_df_null_index["int64_col"], axis=0)
.to_pandas()
)
pd_result = scalars_pandas_df_default_index[["int64_col", "int64_too"]].add(
scalars_pandas_df_default_index.int64_col, axis=0
)
pd.testing.assert_frame_equal(
bf_result, pd_result, check_dtype=False, check_index_type=False
)
def test_null_index_groupby_aggregate(
scalars_df_null_index, scalars_pandas_df_default_index
):
bf_result = scalars_df_null_index.groupby("int64_col").count().to_pandas()
pd_result = scalars_pandas_df_default_index.groupby("int64_col").count()
pd.testing.assert_frame_equal(bf_result, pd_result, check_dtype=False)
def test_null_index_analytic(scalars_df_null_index, scalars_pandas_df_default_index):
# TODO: supply a reason why this isn't compatible with pandas 1.x
pytest.importorskip("pandas", minversion="2.0.0")
bf_result = scalars_df_null_index["int64_col"].cumsum().to_pandas()
pd_result = scalars_pandas_df_default_index["int64_col"].cumsum()
pd.testing.assert_series_equal(
bf_result, pd_result.reset_index(drop=True), check_dtype=False
)
def test_null_index_groupby_analytic(
scalars_df_null_index, scalars_pandas_df_default_index
):
bf_result = (
scalars_df_null_index.groupby("bool_col")["int64_col"].cummax().to_pandas()
)
pd_result = scalars_pandas_df_default_index.groupby("bool_col")[
"int64_col"
].cummax()
pd.testing.assert_series_equal(
bf_result, pd_result.reset_index(drop=True), check_dtype=False
)
def test_null_index_merge_left_null_index_object(
scalars_df_null_index, scalars_df_default_index, scalars_pandas_df_default_index
):
df1 = scalars_df_null_index[scalars_df_null_index["int64_col"] > 0]
df1_pd = scalars_pandas_df_default_index[
scalars_pandas_df_default_index["int64_col"] > 0
]
assert not df1._has_index
df2 = scalars_df_default_index[scalars_df_default_index["int64_col"] <= 55555]
df2_pd = scalars_pandas_df_default_index[
scalars_pandas_df_default_index["int64_col"] <= 55555
]
assert df2._has_index
got = df1.merge(df2, how="inner", on="bool_col")
expected = df1_pd.merge(df2_pd, how="inner", on="bool_col")
# Combining any NULL index object should result in a NULL index.
# This keeps us from generating an index if the user joins a large
# BigQuery table against small local data, for example.
assert not got._has_index
assert got.shape == expected.shape
@pytest.mark.parametrize(
("expr",),
[
("new_col = int64_col + int64_too",),
("new_col = (rowindex > 3) | bool_col",),
("int64_too = bool_col\nnew_col2 = rowindex",),
],
)
def test_null_index_df_eval(
scalars_df_null_index, scalars_pandas_df_default_index, expr
):
# TODO: supply a reason why this isn't compatible with pandas 1.x
pytest.importorskip("pandas", minversion="2.0.0")
bf_result = scalars_df_null_index.eval(expr).to_pandas()
pd_result = scalars_pandas_df_default_index.eval(expr)
pd.testing.assert_frame_equal(bf_result, pd_result, check_index_type=False)
def test_null_index_merge_right_null_index_object(
scalars_df_null_index, scalars_df_default_index, scalars_pandas_df_default_index
):
df1 = scalars_df_default_index[scalars_df_default_index["int64_col"] > 0]
df1_pd = scalars_pandas_df_default_index[
scalars_pandas_df_default_index["int64_col"] > 0
]
assert df1._has_index
df2 = scalars_df_null_index[scalars_df_null_index["int64_col"] <= 55555]
df2_pd = scalars_pandas_df_default_index[
scalars_pandas_df_default_index["int64_col"] <= 55555
]
assert not df2._has_index
got = df1.merge(df2, how="left", on="bool_col")
expected = df1_pd.merge(df2_pd, how="left", on="bool_col")
# Combining any NULL index object should result in a NULL index.
# This keeps us from generating an index if the user joins a large
# BigQuery table against small local data, for example.
assert not got._has_index
assert got.shape == expected.shape
def test_null_index_merge_two_null_index_objects(
scalars_df_null_index, scalars_pandas_df_default_index
):
df1 = scalars_df_null_index[scalars_df_null_index["int64_col"] > 0]
df1_pd = scalars_pandas_df_default_index[
scalars_pandas_df_default_index["int64_col"] > 0
]
assert not df1._has_index
df2 = scalars_df_null_index[scalars_df_null_index["int64_col"] <= 55555]
df2_pd = scalars_pandas_df_default_index[
scalars_pandas_df_default_index["int64_col"] <= 55555
]
assert not df2._has_index
got = df1.merge(df2, how="outer", on="bool_col")
expected = df1_pd.merge(df2_pd, how="outer", on="bool_col")
assert not got._has_index
assert got.shape == expected.shape
def test_null_index_stack(scalars_df_null_index, scalars_pandas_df_default_index):
# TODO: supply a reason why this isn't compatible with pandas 1.x
pytest.importorskip("pandas", minversion="2.0.0")
stacking_cols = ["int64_col", "int64_too"]
bf_result = scalars_df_null_index[stacking_cols].stack().to_pandas()
pd_result = (
scalars_pandas_df_default_index[stacking_cols]
.stack(future_stack=True)
.droplevel(level=0, axis=0)
)
pd_result.index = pd_result.index.astype(bf_result.index.dtype)
pd.testing.assert_series_equal(
bf_result,
pd_result,
check_dtype=False,
)
def test_null_index_series_self_join(
scalars_df_null_index, scalars_pandas_df_default_index
):
bf_result = scalars_df_null_index[["int64_col"]].join(
scalars_df_null_index[["int64_too"]]
)
pd_result = scalars_pandas_df_default_index[["int64_col"]].join(
scalars_pandas_df_default_index[["int64_too"]]
)
pd.testing.assert_frame_equal(
bf_result.to_pandas(), pd_result.reset_index(drop=True), check_dtype=False
)
def test_null_index_series_self_join_on(
scalars_df_null_index, scalars_pandas_df_default_index
):
# caller doesn't need index, but do need index on arg to join with 'on'
bf_result = scalars_df_null_index[["int64_col", "string_col"]].join(
scalars_df_null_index[["int64_too", "bool_col"]].set_index("int64_too"),
on="int64_col",
)
pd_result = scalars_pandas_df_default_index[["int64_col", "string_col"]].join(
scalars_pandas_df_default_index[["int64_too", "bool_col"]].set_index(
"int64_too"
),
on="int64_col",
)
pd.testing.assert_frame_equal(
bf_result.to_pandas(), pd_result.reset_index(drop=True), check_dtype=False
)
def test_null_index_series_self_aligns(
scalars_df_null_index, scalars_pandas_df_default_index
):
bf_result = scalars_df_null_index["int64_col"] + scalars_df_null_index["int64_too"]
pd_result = (
scalars_pandas_df_default_index["int64_col"]
+ scalars_pandas_df_default_index["int64_too"]
)
pd.testing.assert_series_equal(
bf_result.to_pandas(), pd_result.reset_index(drop=True), check_dtype=False
)
def test_null_index_df_self_aligns(
scalars_df_null_index, scalars_pandas_df_default_index
):
bf_result = (
scalars_df_null_index[["int64_col", "float64_col"]]
+ scalars_df_null_index[["int64_col", "float64_col"]]
)
pd_result = (
scalars_pandas_df_default_index[["int64_col", "float64_col"]]
+ scalars_pandas_df_default_index[["int64_col", "float64_col"]]
)
pd.testing.assert_frame_equal(
bf_result.to_pandas(), pd_result.reset_index(drop=True), check_dtype=False
)
def test_null_index_setitem(scalars_df_null_index, scalars_pandas_df_default_index):
bf_result = scalars_df_null_index.copy()
bf_result["new_col"] = (
scalars_df_null_index["int64_col"] + scalars_df_null_index["float64_col"]
)
pd_result = scalars_pandas_df_default_index.copy()
pd_result["new_col"] = (
scalars_pandas_df_default_index["int64_col"]
+ scalars_pandas_df_default_index["float64_col"]
)
pd.testing.assert_frame_equal(
bf_result.to_pandas(), pd_result.reset_index(drop=True), check_dtype=False
)
def test_null_index_df_concat(scalars_df_null_index, scalars_pandas_df_default_index):
bf_result = bpd.concat([scalars_df_null_index, scalars_df_null_index])
pd_result = pd.concat(
[scalars_pandas_df_default_index, scalars_pandas_df_default_index]
)
pd.testing.assert_frame_equal(
bf_result.to_pandas(), pd_result.reset_index(drop=True), check_dtype=False
)
def test_null_index_map_dict_input(
scalars_df_null_index, scalars_pandas_df_default_index
):
local_map = dict()
# construct a local map, incomplete to cover <NA> behavior
for s in scalars_pandas_df_default_index.string_col[:-3]:
if isinstance(s, str):
local_map[s] = ord(s[0])
pd_result = scalars_pandas_df_default_index.string_col.map(local_map)
pd_result = pd_result.astype("Int64") # pandas type differences
bf_result = scalars_df_null_index.string_col.map(local_map)
pd.testing.assert_series_equal(
bf_result.to_pandas(), pd_result.reset_index(drop=True), check_dtype=False
)
def test_null_index_align_error(scalars_df_null_index):
with pytest.raises(bigframes.exceptions.NullIndexError):
_ = (
scalars_df_null_index["int64_col"]
+ scalars_df_null_index["int64_col"].cumsum()[
scalars_df_null_index["int64_col"] > 3
]
)
def test_null_index_loc_error(scalars_df_null_index):
with pytest.raises(bigframes.exceptions.NullIndexError):
scalars_df_null_index["int64_col"].loc[1]
def test_null_index_at_error(scalars_df_null_index):
with pytest.raises(bigframes.exceptions.NullIndexError):
scalars_df_null_index["int64_col"].at[1]
def test_null_index_idxmin_error(scalars_df_null_index):
with pytest.raises(bigframes.exceptions.NullIndexError):
scalars_df_null_index[["int64_col", "int64_too"]].idxmin()
def test_null_index_index_property(scalars_df_null_index):
with pytest.raises(bigframes.exceptions.NullIndexError):
_ = scalars_df_null_index.index
def test_null_index_transpose(scalars_df_null_index):
with pytest.raises(bigframes.exceptions.NullIndexError):
_ = scalars_df_null_index.T
def test_null_index_contains(scalars_df_null_index):
assert 3 not in scalars_df_null_index