-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathdataframe.py
2674 lines (2327 loc) · 98.6 KB
/
dataframe.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.
"""DataFrame is a two dimensional data structure."""
from __future__ import annotations
import re
import textwrap
import typing
from typing import (
Callable,
Dict,
Iterable,
List,
Literal,
Mapping,
Optional,
Sequence,
Tuple,
Union,
)
import google.cloud.bigquery as bigquery
import numpy
import pandas
import bigframes
import bigframes._config.display_options as display_options
import bigframes.constants as constants
import bigframes.core
import bigframes.core.block_transforms as block_ops
import bigframes.core.blocks as blocks
import bigframes.core.groupby as groupby
import bigframes.core.guid
import bigframes.core.indexers as indexers
import bigframes.core.indexes as indexes
import bigframes.core.ordering as order
import bigframes.core.utils as utils
import bigframes.core.window
import bigframes.dtypes
import bigframes.formatting_helpers as formatter
import bigframes.operations as ops
import bigframes.operations.aggregations as agg_ops
import bigframes.series
import bigframes.series as bf_series
import bigframes.session._io.bigquery
import third_party.bigframes_vendored.pandas.core.frame as vendored_pandas_frame
import third_party.bigframes_vendored.pandas.pandas._typing as vendored_pandas_typing
if typing.TYPE_CHECKING:
import bigframes.session
# BigQuery has 1 MB query size limit, 5000 items shouldn't take more than 10% of this depending on data type.
# TODO(tbergeron): Convert to bytes-based limit
MAX_INLINE_DF_SIZE = 5000
LevelType = typing.Union[str, int]
LevelsType = typing.Union[LevelType, typing.Sequence[LevelType]]
SingleItemValue = Union[bigframes.series.Series, int, float, Callable]
ERROR_IO_ONLY_GS_PATHS = f"Only Google Cloud Storage (gs://...) paths are supported. {constants.FEEDBACK_LINK}"
ERROR_IO_REQUIRES_WILDCARD = (
"Google Cloud Storage path must contain a wildcard '*' character. See: "
"https://cloud.google.com/bigquery/docs/reference/standard-sql/other-statements#export_data_statement"
f"{constants.FEEDBACK_LINK}"
)
# Inherits from pandas DataFrame so that we can use the same docstrings.
class DataFrame(vendored_pandas_frame.DataFrame):
__doc__ = vendored_pandas_frame.DataFrame.__doc__
def __init__(
self,
data=None,
index: vendored_pandas_typing.Axes | None = None,
columns: vendored_pandas_typing.Axes | None = None,
dtype: typing.Optional[
bigframes.dtypes.DtypeString | bigframes.dtypes.Dtype
] = None,
copy: typing.Optional[bool] = None,
*,
session: typing.Optional[bigframes.session.Session] = None,
):
if copy is not None and not copy:
raise ValueError(
f"DataFrame constructor only supports copy=True. {constants.FEEDBACK_LINK}"
)
# Check to see if constructing from BigQuery-backed objects before
# falling back to pandas constructor
block = None
if isinstance(data, blocks.Block):
block = data
elif isinstance(data, DataFrame):
block = data._get_block()
# Dict of Series
elif (
utils.is_dict_like(data)
and len(data) >= 1
and any(isinstance(data[key], bf_series.Series) for key in data.keys())
):
if not all(isinstance(data[key], bf_series.Series) for key in data.keys()):
# TODO(tbergeron): Support local list/series data by converting to memtable.
raise NotImplementedError(
f"Cannot mix Series with other types. {constants.FEEDBACK_LINK}"
)
keys = list(data.keys())
first_label, first_series = keys[0], data[keys[0]]
block = (
typing.cast(bf_series.Series, first_series)
._get_block()
.with_column_labels([first_label])
)
for key in keys[1:]:
other = typing.cast(bf_series.Series, data[key])
other_block = other._block.with_column_labels([key])
# Pandas will keep original sorting if all indices are aligned.
# We cannot detect this easily however, and so always sort on index
result_index, _ = block.index.join( # type:ignore
other_block.index, how="outer", sort=True
)
block = result_index._block
if block:
if index:
raise NotImplementedError(
"DataFrame 'index' constructor parameter not supported "
f"when passing BigQuery-backed objects. {constants.FEEDBACK_LINK}"
)
if columns:
block = block.select_columns(list(columns)) # type:ignore
if dtype:
block = block.multi_apply_unary_op(
block.value_columns, ops.AsTypeOp(dtype)
)
self._block = block
else:
import bigframes.pandas
pd_dataframe = pandas.DataFrame(
data=data,
index=index, # type:ignore
columns=columns, # type:ignore
dtype=dtype, # type:ignore
)
if (
pd_dataframe.size < MAX_INLINE_DF_SIZE
# TODO(swast): Workaround data types limitation in inline data.
and not any(
dt.pyarrow_dtype
for dt in pd_dataframe.dtypes
if isinstance(dt, pandas.ArrowDtype)
)
):
self._block = blocks.block_from_local(
pd_dataframe, session or bigframes.pandas.get_global_session()
)
elif session:
self._block = session.read_pandas(pd_dataframe)._get_block()
else:
self._block = bigframes.pandas.read_pandas(pd_dataframe)._get_block()
self._query_job: Optional[bigquery.QueryJob] = None
def __dir__(self):
return dir(type(self)) + [
label
for label in self._block.column_labels
if label and isinstance(label, str)
]
def _ipython_key_completions_(self) -> List[str]:
return list(
[
label
for label in self._block.column_labels
if label and isinstance(label, str)
]
)
def _find_indices(
self,
columns: Union[blocks.Label, Sequence[blocks.Label]],
tolerance: bool = False,
) -> Sequence[int]:
"""Find corresponding indices in df._block.column_labels for column name(s).
Order is kept the same as input names order.
Args:
columns: column name(s)
tolerance: True to pass through columns not found. False to raise
ValueError.
"""
col_ids = self._sql_names(columns, tolerance)
return [self._block.value_columns.index(col_id) for col_id in col_ids]
def _resolve_label_exact(self, label) -> Optional[str]:
"""Returns the column id matching the label if there is exactly
one such column. If there are multiple columns with the same name,
raises an error. If there is no such column, returns None."""
matches = self._block.label_to_col_id.get(label, [])
if len(matches) > 1:
raise ValueError(
f"Multiple columns matching id {label} were found. {constants.FEEDBACK_LINK}"
)
return matches[0] if len(matches) != 0 else None
def _sql_names(
self,
columns: Union[blocks.Label, Sequence[blocks.Label], pandas.Index],
tolerance: bool = False,
) -> Sequence[str]:
"""Retrieve sql name (column name in BQ schema) of column(s)."""
labels = (
columns
if utils.is_list_like(columns) and not isinstance(columns, tuple)
else [columns]
) # type:ignore
results: Sequence[str] = []
for label in labels:
col_ids = self._block.label_to_col_id.get(label, [])
if not tolerance and len(col_ids) == 0:
raise ValueError(f"Column name {label} doesn't exist")
results = (*results, *col_ids)
return results
@property
def index(
self,
) -> indexes.Index:
return indexes.Index(self)
@property
def loc(self) -> indexers.LocDataFrameIndexer:
return indexers.LocDataFrameIndexer(self)
@property
def iloc(self) -> indexers.ILocDataFrameIndexer:
return indexers.ILocDataFrameIndexer(self)
@property
def iat(self) -> indexers.IatDataFrameIndexer:
return indexers.IatDataFrameIndexer(self)
@property
def at(self) -> indexers.AtDataFrameIndexer:
return indexers.AtDataFrameIndexer(self)
@property
def dtypes(self) -> pandas.Series:
return pandas.Series(data=self._block.dtypes, index=self._block.column_labels)
@property
def columns(self) -> pandas.Index:
return self.dtypes.index
@columns.setter
def columns(self, labels: pandas.Index):
new_block = self._block.with_column_labels(labels)
self._set_block(new_block)
@property
def shape(self) -> Tuple[int, int]:
return self._block.shape
@property
def size(self) -> int:
rows, cols = self.shape
return rows * cols
@property
def ndim(self) -> int:
return 2
@property
def empty(self) -> bool:
return self.size == 0
@property
def values(self) -> numpy.ndarray:
return self.to_numpy()
@property
def _session(self) -> bigframes.Session:
return self._get_block().expr._session
def __len__(self):
rows, _ = self.shape
return rows
def astype(
self,
dtype: Union[bigframes.dtypes.DtypeString, bigframes.dtypes.Dtype],
) -> DataFrame:
return self._apply_unary_op(ops.AsTypeOp(dtype))
def _to_sql_query(
self, include_index: bool
) -> Tuple[str, list[str], list[blocks.Label]]:
"""Compiles this DataFrame's expression tree to SQL, optionally
including index columns.
Args:
include_index (bool):
whether to include index columns.
Returns:
a tuple of (sql_string, index_column_id_list, index_column_label_list).
If include_index is set to False, index_column_id_list and index_column_label_list
return empty lists.
"""
return self._block.to_sql_query(include_index)
@property
def sql(self) -> str:
"""Compiles this DataFrame's expression tree to SQL."""
sql, _, _ = self._to_sql_query(include_index=False)
return sql
@property
def query_job(self) -> Optional[bigquery.QueryJob]:
"""BigQuery job metadata for the most recent query.
Returns:
The most recent `QueryJob
<https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.job.QueryJob>`_.
"""
if self._query_job is None:
self._set_internal_query_job(self._compute_dry_run())
return self._query_job
def _set_internal_query_job(self, query_job: bigquery.QueryJob):
self._query_job = query_job
def __getitem__(
self,
key: Union[
blocks.Label,
Sequence[blocks.Label],
# Index of column labels can be treated the same as a sequence of column labels.
pandas.Index,
bigframes.series.Series,
],
): # No return type annotations (like pandas) as type cannot always be determined statically
"""Gets the specified column(s) from the DataFrame."""
# NOTE: This implements the operations described in
# https://pandas.pydata.org/docs/getting_started/intro_tutorials/03_subset_data.html
if isinstance(key, bigframes.series.Series):
return self._getitem_bool_series(key)
if isinstance(key, typing.Hashable):
return self._getitem_label(key)
# Select a subset of columns or re-order columns.
# In Ibis after you apply a projection, any column objects from the
# table before the projection can't be combined with column objects
# from the table after the projection. This is because the table after
# a projection is considered a totally separate table expression.
#
# This is unexpected behavior for a pandas user, who expects their old
# Series objects to still work with the new / mutated DataFrame. We
# avoid applying a projection in Ibis until it's absolutely necessary
# to provide pandas-like semantics.
# TODO(swast): Do we need to apply implicit join when doing a
# projection?
# Select a number of columns as DF.
key = key if utils.is_list_like(key) else [key] # type:ignore
selected_ids: Tuple[str, ...] = ()
for label in key:
col_ids = self._block.label_to_col_id[label]
selected_ids = (*selected_ids, *col_ids)
return DataFrame(self._block.select_columns(selected_ids))
def _getitem_label(self, key: blocks.Label):
col_ids = self._block.cols_matching_label(key)
if len(col_ids) == 0:
raise KeyError(key)
block = self._block.select_columns(col_ids)
if isinstance(self.columns, pandas.MultiIndex):
# Multiindex should drop-level if not selecting entire
key_levels = len(key) if isinstance(key, tuple) else 1
index_levels = self.columns.nlevels
if key_levels < index_levels:
block = block.with_column_labels(
block.column_labels.droplevel(list(range(key_levels)))
)
# Force return DataFrame in this case, even if only single column
return DataFrame(block)
if len(col_ids) == 1:
return bigframes.series.Series(block)
return DataFrame(block)
# Bool Series selects rows
def _getitem_bool_series(self, key: bigframes.series.Series) -> DataFrame:
if not key.dtype == pandas.BooleanDtype():
raise NotImplementedError(
f"Only boolean series currently supported for indexing. {constants.FEEDBACK_LINK}"
)
# TODO: enforce stricter alignment
combined_index, (
get_column_left,
get_column_right,
) = self._block.index.join(key._block.index, how="left")
block = combined_index._block
filter_col_id = get_column_right[key._value_column]
block = block.filter(filter_col_id)
block = block.drop_columns([filter_col_id])
return DataFrame(block)
def __getattr__(self, key: str):
if key in self._block.column_labels:
return self.__getitem__(key)
elif hasattr(pandas.DataFrame, key):
raise AttributeError(
textwrap.dedent(
f"""
BigQuery DataFrames has not yet implemented an equivalent to
'pandas.DataFrame.{key}'. {constants.FEEDBACK_LINK}
"""
)
)
else:
raise AttributeError(key)
def __repr__(self) -> str:
"""Converts a DataFrame to a string. Calls to_pandas.
Only represents the first `bigframes.options.display.max_rows`.
"""
opts = bigframes.options.display
max_results = opts.max_rows
if opts.repr_mode == "deferred":
return formatter.repr_query_job(self.query_job)
# TODO(swast): pass max_columns and get the true column count back. Maybe
# get 1 more column than we have requested so that pandas can add the
# ... for us?
pandas_df, row_count, query_job = self._block.retrieve_repr_request_results(
max_results
)
self._set_internal_query_job(query_job)
column_count = len(pandas_df.columns)
with display_options.pandas_repr(opts):
repr_string = repr(pandas_df)
# Modify the end of the string to reflect count.
lines = repr_string.split("\n")
pattern = re.compile("\\[[0-9]+ rows x [0-9]+ columns\\]")
if pattern.match(lines[-1]):
lines = lines[:-2]
if row_count > len(lines) - 1:
lines.append("...")
lines.append("")
lines.append(f"[{row_count} rows x {column_count} columns]")
return "\n".join(lines)
def _repr_html_(self) -> str:
"""
Returns an html string primarily for use by notebooks for displaying
a representation of the DataFrame. Displays 20 rows by default since
many notebooks are not configured for large tables.
"""
opts = bigframes.options.display
max_results = bigframes.options.display.max_rows
if opts.repr_mode == "deferred":
return formatter.repr_query_job_html(self.query_job)
# TODO(swast): pass max_columns and get the true column count back. Maybe
# get 1 more column than we have requested so that pandas can add the
# ... for us?
pandas_df, row_count, query_job = self._block.retrieve_repr_request_results(
max_results
)
self._set_internal_query_job(query_job)
column_count = len(pandas_df.columns)
with display_options.pandas_repr(opts):
# _repr_html_ stub is missing so mypy thinks it's a Series. Ignore mypy.
html_string = pandas_df._repr_html_() # type:ignore
html_string += f"[{row_count} rows x {column_count} columns in total]"
return html_string
def __setitem__(self, key: str, value: SingleItemValue):
"""Modify or insert a column into the DataFrame.
Note: This does **not** modify the original table the DataFrame was
derived from.
"""
df = self._assign_single_item(key, value)
self._set_block(df._get_block())
def _apply_binop(
self,
other: float | int | bigframes.series.Series | DataFrame,
op,
axis: str | int = "columns",
how: str = "outer",
):
if isinstance(other, (float, int)):
return self._apply_scalar_binop(other, op)
elif isinstance(other, bigframes.series.Series):
return self._apply_series_binop(other, op, axis=axis, how=how)
elif isinstance(other, DataFrame):
return self._apply_dataframe_binop(other, op, how=how)
raise NotImplementedError(
f"binary operation is not implemented on the second operand of type {type(other).__name__}."
f"{constants.FEEDBACK_LINK}"
)
def _apply_scalar_binop(self, other: float | int, op: ops.BinaryOp) -> DataFrame:
block = self._block
partial_op = ops.BinopPartialRight(op, other)
for column_id, label in zip(
self._block.value_columns, self._block.column_labels
):
block, _ = block.apply_unary_op(column_id, partial_op, result_label=label)
block = block.drop_columns([column_id])
return DataFrame(block)
def _apply_series_binop(
self,
other: bigframes.series.Series,
op: ops.BinaryOp,
axis: str | int = "columns",
how: str = "outer",
) -> DataFrame:
if axis not in ("columns", "index", 0, 1):
raise ValueError(f"Invalid input: axis {axis}.")
if axis in ("columns", 1):
raise NotImplementedError(
f"Row Series operations haven't been supported. {constants.FEEDBACK_LINK}"
)
joined_index, (get_column_left, get_column_right) = self._block.index.join(
other._block.index, how=how
)
series_column_id = other._value_column
series_col = get_column_right[series_column_id]
block = joined_index._block
for column_id, label in zip(
self._block.value_columns, self._block.column_labels
):
block, _ = block.apply_binary_op(
get_column_left[column_id],
series_col,
op,
result_label=label,
)
block = block.drop_columns([get_column_left[column_id]])
block = block.drop_columns([series_col])
block = block.with_index_labels(self.index.names)
return DataFrame(block)
def _apply_dataframe_binop(
self, other: DataFrame, op: ops.BinaryOp, how: str = "outer"
) -> DataFrame:
# Join rows
joined_index, (get_column_left, get_column_right) = self._block.index.join(
other._block.index, how=how
)
# join columns schema
# indexers will be none for exact match
columns, lcol_indexer, rcol_indexer = self.columns.join(
other.columns, how=how, return_indexers=True
)
binop_result_ids = []
block = joined_index._block
column_indices = zip(
lcol_indexer if (lcol_indexer is not None) else range(len(columns)),
rcol_indexer if (lcol_indexer is not None) else range(len(columns)),
)
for left_index, right_index in column_indices:
if left_index >= 0 and right_index >= 0: # -1 indices indicate missing
left_col_id = self._block.value_columns[left_index]
right_col_id = other._block.value_columns[right_index]
block, result_col_id = block.apply_binary_op(
get_column_left[left_col_id],
get_column_right[right_col_id],
op,
)
binop_result_ids.append(result_col_id)
elif left_index >= 0:
left_col_id = self._block.value_columns[left_index]
block, result_col_id = block.apply_unary_op(
get_column_left[left_col_id],
ops.partial_right(op, None),
)
binop_result_ids.append(result_col_id)
elif right_index >= 0:
right_col_id = other._block.value_columns[right_index]
block, result_col_id = block.apply_unary_op(
get_column_right[right_col_id],
ops.partial_left(op, None),
)
binop_result_ids.append(result_col_id)
else:
# Should not be possible
raise ValueError("No right or left index.")
block = block.select_columns(binop_result_ids).with_column_labels(columns)
return DataFrame(block)
def eq(self, other: typing.Any, axis: str | int = "columns") -> DataFrame:
return self._apply_binop(other, ops.eq_op, axis=axis)
def ne(self, other: typing.Any, axis: str | int = "columns") -> DataFrame:
return self._apply_binop(other, ops.ne_op, axis=axis)
__eq__ = eq # type: ignore
__ne__ = ne # type: ignore
def le(self, other: typing.Any, axis: str | int = "columns") -> DataFrame:
return self._apply_binop(other, ops.le_op, axis=axis)
def lt(self, other: typing.Any, axis: str | int = "columns") -> DataFrame:
return self._apply_binop(other, ops.lt_op, axis=axis)
def ge(self, other: typing.Any, axis: str | int = "columns") -> DataFrame:
return self._apply_binop(other, ops.ge_op, axis=axis)
def gt(self, other: typing.Any, axis: str | int = "columns") -> DataFrame:
return self._apply_binop(other, ops.gt_op, axis=axis)
__lt__ = lt
__le__ = le
__gt__ = gt
__ge__ = ge
def add(
self,
other: float | int | bigframes.series.Series | DataFrame,
axis: str | int = "columns",
) -> DataFrame:
# TODO(swast): Support fill_value parameter.
# TODO(swast): Support level parameter with MultiIndex.
return self._apply_binop(other, ops.add_op, axis=axis)
__radd__ = __add__ = radd = add
def sub(
self,
other: float | int | bigframes.series.Series | DataFrame,
axis: str | int = "columns",
) -> DataFrame:
return self._apply_binop(other, ops.sub_op, axis=axis)
__sub__ = subtract = sub
def rsub(
self,
other: float | int | bigframes.series.Series | DataFrame,
axis: str | int = "columns",
) -> DataFrame:
return self._apply_binop(other, ops.reverse(ops.sub_op), axis=axis)
__rsub__ = rsub
def mul(
self,
other: float | int | bigframes.series.Series | DataFrame,
axis: str | int = "columns",
) -> DataFrame:
return self._apply_binop(other, ops.mul_op, axis=axis)
__rmul__ = __mul__ = rmul = multiply = mul
def truediv(
self,
other: float | int | bigframes.series.Series | DataFrame,
axis: str | int = "columns",
) -> DataFrame:
return self._apply_binop(other, ops.div_op, axis=axis)
div = divide = __truediv__ = truediv
def rtruediv(
self,
other: float | int | bigframes.series.Series | DataFrame,
axis: str | int = "columns",
) -> DataFrame:
return self._apply_binop(other, ops.reverse(ops.div_op), axis=axis)
__rtruediv__ = rdiv = rtruediv
def floordiv(
self,
other: float | int | bigframes.series.Series | DataFrame,
axis: str | int = "columns",
) -> DataFrame:
return self._apply_binop(other, ops.floordiv_op, axis=axis)
__floordiv__ = floordiv
def rfloordiv(
self,
other: float | int | bigframes.series.Series | DataFrame,
axis: str | int = "columns",
) -> DataFrame:
return self._apply_binop(other, ops.reverse(ops.floordiv_op), axis=axis)
__rfloordiv__ = rfloordiv
def mod(self, other: int | bigframes.series.Series | DataFrame, axis: str | int = "columns") -> DataFrame: # type: ignore
return self._apply_binop(other, ops.mod_op, axis=axis)
def rmod(self, other: int | bigframes.series.Series | DataFrame, axis: str | int = "columns") -> DataFrame: # type: ignore
return self._apply_binop(other, ops.reverse(ops.mod_op), axis=axis)
__mod__ = mod
__rmod__ = rmod
def pow(
self, other: int | bigframes.series.Series, axis: str | int = "columns"
) -> DataFrame:
return self._apply_binop(other, ops.pow_op, axis=axis)
def rpow(
self, other: int | bigframes.series.Series, axis: str | int = "columns"
) -> DataFrame:
return self._apply_binop(other, ops.reverse(ops.pow_op), axis=axis)
__pow__ = pow
__rpow__ = rpow
def align(
self,
other: typing.Union[DataFrame, bigframes.series.Series],
join: str = "outer",
axis: typing.Union[str, int, None] = None,
) -> typing.Tuple[
typing.Union[DataFrame, bigframes.series.Series],
typing.Union[DataFrame, bigframes.series.Series],
]:
axis_n = utils.get_axis_number(axis) if axis else None
if axis_n == 1 and isinstance(other, bigframes.series.Series):
raise NotImplementedError(
f"align with series and axis=1 not supported. {constants.FEEDBACK_LINK}"
)
left_block, right_block = block_ops.align(
self._block, other._block, join=join, axis=axis
)
return DataFrame(left_block), other.__class__(right_block)
def update(self, other, join: str = "left", overwrite=True, filter_func=None):
other = other if isinstance(other, DataFrame) else DataFrame(other)
if join != "left":
raise ValueError("Only 'left' join supported for update")
if filter_func is not None: # Will always take other if possible
def update_func(
left: bigframes.series.Series, right: bigframes.series.Series
) -> bigframes.series.Series:
return left.mask(right.notna() & filter_func(left), right)
elif overwrite:
def update_func(
left: bigframes.series.Series, right: bigframes.series.Series
) -> bigframes.series.Series:
return left.mask(right.notna(), right)
else:
def update_func(
left: bigframes.series.Series, right: bigframes.series.Series
) -> bigframes.series.Series:
return left.mask(left.isna(), right)
result = self.combine(other, update_func, how=join)
self._set_block(result._block)
def combine(
self,
other: DataFrame,
func: typing.Callable[
[bigframes.series.Series, bigframes.series.Series], bigframes.series.Series
],
fill_value=None,
overwrite: bool = True,
*,
how: str = "outer",
) -> DataFrame:
l_aligned, r_aligned = block_ops.align(self._block, other._block, join=how)
other_missing_labels = self._block.column_labels.difference(
other._block.column_labels
)
l_frame = DataFrame(l_aligned)
r_frame = DataFrame(r_aligned)
results = []
for (label, lseries), (_, rseries) in zip(l_frame.items(), r_frame.items()):
if not ((label in other_missing_labels) and not overwrite):
if fill_value is not None:
result = func(
lseries.fillna(fill_value), rseries.fillna(fill_value)
)
else:
result = func(lseries, rseries)
else:
result = (
lseries.fillna(fill_value) if fill_value is not None else lseries
)
results.append(result)
if all([isinstance(val, bigframes.series.Series) for val in results]):
import bigframes.core.reshape as rs
return rs.concat(results, axis=1)
else:
raise ValueError("'func' must return Series")
def combine_first(self, other: DataFrame):
return self._apply_dataframe_binop(other, ops.fillna_op)
def to_pandas(
self,
max_download_size: Optional[int] = None,
sampling_method: Optional[str] = None,
random_state: Optional[int] = None,
) -> pandas.DataFrame:
"""Write DataFrame to pandas DataFrame.
Args:
max_download_size (int, default None):
Download size threshold in MB. If max_download_size is exceeded when downloading data
(e.g., to_pandas()), the data will be downsampled if
bigframes.options.sampling.enable_downsampling is True, otherwise, an error will be
raised. If set to a value other than None, this will supersede the global config.
sampling_method (str, default None):
Downsampling algorithms to be chosen from, the choices are: "head": This algorithm
returns a portion of the data from the beginning. It is fast and requires minimal
computations to perform the downsampling; "uniform": This algorithm returns uniform
random samples of the data. If set to a value other than None, this will supersede
the global config.
random_state (int, default None):
The seed for the uniform downsampling algorithm. If provided, the uniform method may
take longer to execute and require more computation. If set to a value other than
None, this will supersede the global config.
Returns:
pandas.DataFrame: A pandas DataFrame with all rows and columns of this DataFrame if the
data_sampling_threshold_mb is not exceeded; otherwise, a pandas DataFrame with
downsampled rows and all columns of this DataFrame.
"""
# TODO(orrbradford): Optimize this in future. Potentially some cases where we can return the stored query job
df, query_job = self._block.to_pandas(
max_download_size=max_download_size,
sampling_method=sampling_method,
random_state=random_state,
)
self._set_internal_query_job(query_job)
return df.set_axis(self._block.column_labels, axis=1, copy=False)
def to_pandas_batches(self) -> Iterable[pandas.DataFrame]:
"""Stream DataFrame results to an iterable of pandas DataFrame"""
return self._block.to_pandas_batches()
def _compute_dry_run(self) -> bigquery.QueryJob:
return self._block._compute_dry_run()
def copy(self) -> DataFrame:
return DataFrame(self._block)
def head(self, n: int = 5) -> DataFrame:
return typing.cast(DataFrame, self.iloc[:n])
def tail(self, n: int = 5) -> DataFrame:
return typing.cast(DataFrame, self.iloc[-n:])
def nlargest(
self,
n: int,
columns: typing.Union[blocks.Label, typing.Sequence[blocks.Label]],
keep: str = "first",
) -> DataFrame:
if keep not in ("first", "last", "all"):
raise ValueError("'keep must be one of 'first', 'last', or 'all'")
column_ids = self._sql_names(columns)
return DataFrame(block_ops.nlargest(self._block, n, column_ids, keep=keep))
def nsmallest(
self,
n: int,
columns: typing.Union[blocks.Label, typing.Sequence[blocks.Label]],
keep: str = "first",
) -> DataFrame:
if keep not in ("first", "last", "all"):
raise ValueError("'keep must be one of 'first', 'last', or 'all'")
column_ids = self._sql_names(columns)
return DataFrame(block_ops.nsmallest(self._block, n, column_ids, keep=keep))
def drop(
self,
labels: typing.Any = None,
*,
axis: typing.Union[int, str] = 0,
index: typing.Any = None,
columns: Union[blocks.Label, Sequence[blocks.Label]] = None,
level: typing.Optional[LevelType] = None,
) -> DataFrame:
if labels:
if index or columns:
raise ValueError("Cannot specify both 'labels' and 'index'/'columns")
axis_n = utils.get_axis_number(axis)
if axis_n == 0:
index = labels
else:
columns = labels
block = self._block
if index is not None:
level_id = self._resolve_levels(level or 0)[0]
if utils.is_list_like(index):
block, inverse_condition_id = block.apply_unary_op(
level_id, ops.IsInOp(index, match_nulls=True)
)
block, condition_id = block.apply_unary_op(
inverse_condition_id, ops.invert_op
)
elif isinstance(index, indexes.Index):
return self._drop_by_index(index)
else:
block, condition_id = block.apply_unary_op(
level_id, ops.partial_right(ops.ne_op, index)
)
block = block.filter(condition_id, keep_null=True).select_columns(
self._block.value_columns
)
if columns:
block = block.drop_columns(self._sql_names(columns))
if index is None and not columns:
raise ValueError("Must specify 'labels' or 'index'/'columns")
return DataFrame(block)
def _drop_by_index(self, index: indexes.Index) -> DataFrame:
block = index._data._get_block()
block, ordering_col = block.promote_offsets()
joined_index, (get_column_left, get_column_right) = self._block.index.join(
block.index
)
new_ordering_col = get_column_right[ordering_col]
drop_block = joined_index._block
drop_block, drop_col = drop_block.apply_unary_op(
new_ordering_col,
ops.isnull_op,
)
drop_block = drop_block.filter(drop_col)
original_columns = [
get_column_left[column] for column in self._block.value_columns
]
drop_block = drop_block.select_columns(original_columns)
return DataFrame(drop_block)
def droplevel(self, level: LevelsType, axis: int | str = 0):
axis_n = utils.get_axis_number(axis)