forked from googleapis/python-bigquery-dataframes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.py
More file actions
511 lines (434 loc) · 17 KB
/
base.py
File metadata and controls
511 lines (434 loc) · 17 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
# 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.
"""An index based on a single column."""
from __future__ import annotations
import typing
from typing import Hashable, Optional, Sequence, Union
import bigframes_vendored.constants as constants
import bigframes_vendored.pandas.core.indexes.base as vendored_pandas_index
import google.cloud.bigquery as bigquery
import numpy as np
import pandas
import bigframes.core.block_transforms as block_ops
import bigframes.core.blocks as blocks
import bigframes.core.expression as ex
import bigframes.core.ordering as order
import bigframes.core.utils as utils
import bigframes.core.validations as validations
import bigframes.dtypes
import bigframes.formatting_helpers as formatter
import bigframes.operations as ops
import bigframes.operations.aggregations as agg_ops
if typing.TYPE_CHECKING:
import bigframes.dataframe
import bigframes.series
class Index(vendored_pandas_index.Index):
__doc__ = vendored_pandas_index.Index.__doc__
_query_job = None
_block: blocks.Block
_linked_frame: Union[
bigframes.dataframe.DataFrame, bigframes.series.Series, None
] = None
# Overrided on __new__ to create subclasses like pandas does
def __new__(
cls,
data=None,
dtype=None,
*,
name=None,
session=None,
):
import bigframes.dataframe as df
import bigframes.series as series
if isinstance(data, blocks.Block):
block = data.select_columns([])
elif isinstance(data, df.DataFrame):
raise ValueError("Cannot construct index from dataframe.")
elif isinstance(data, series.Series) or isinstance(data, Index):
if isinstance(data, series.Series):
block = data._block
block = block.set_index(
col_ids=[data._value_column],
)
elif isinstance(data, Index):
block = data._block
index = Index(data=block)
name = data.name if name is None else name
if name is not None:
index.name = name
if dtype is not None:
index = index.astype(dtype)
block = index._block
elif isinstance(data, pandas.Index):
pd_df = pandas.DataFrame(index=data)
block = df.DataFrame(pd_df, session=session)._block
else:
pd_index = pandas.Index(data=data, dtype=dtype, name=name)
pd_df = pandas.DataFrame(index=pd_index)
block = df.DataFrame(pd_df, session=session)._block
# TODO: Support more index subtypes
from bigframes.core.indexes.multi import MultiIndex
if len(block._index_columns) <= 1:
klass = cls
else:
klass = MultiIndex
result = typing.cast(Index, object.__new__(klass))
result._query_job = None
result._block = block
block.session._register_object(result)
return result
@classmethod
def from_frame(
cls, frame: Union[bigframes.series.Series, bigframes.dataframe.DataFrame]
) -> Index:
if len(frame._block.index_columns) == 0:
raise bigframes.exceptions.NullIndexError(
"Cannot access index properties with Null Index. Set an index using set_index."
)
frame._block._throw_if_null_index("from_frame")
index = Index(frame._block)
index._linked_frame = frame
return index
@property
def _session(self):
return self._block.session
@property
def name(self) -> blocks.Label:
names = self.names
if len(names) == 1:
return self.names[0]
else:
# pandas returns None for MultiIndex.name.
return None
@name.setter
def name(self, value: blocks.Label):
self.names = [value]
@property
def names(self) -> typing.Sequence[blocks.Label]:
"""Returns the names of the Index."""
return self._block._index_labels
@names.setter
def names(self, values: typing.Sequence[blocks.Label]):
new_block = self._block.with_index_labels(values)
if self._linked_frame is not None:
self._linked_frame._set_block(
self._linked_frame._block.with_index_labels(values)
)
self._block = new_block
@property
def nlevels(self) -> int:
return len(self._block.index_columns)
@property
def values(self) -> np.ndarray:
return self.to_numpy()
@property
def ndim(self) -> int:
return 1
@property
def shape(self) -> typing.Tuple[int]:
return (self._block.shape[0],)
@property
def dtype(self):
return self._block.index.dtypes[0] if self.nlevels == 1 else np.dtype("O")
@property
def dtypes(self) -> pandas.Series:
return pandas.Series(
data=self._block.index.dtypes,
index=typing.cast(typing.Tuple, self._block.index.names),
)
@property
def size(self) -> int:
"""Returns the size of the Index."""
return self.shape[0]
@property
def empty(self) -> bool:
"""Returns True if the Index is empty, otherwise returns False."""
return self.shape[0] == 0
@property
@validations.requires_ordering()
def is_monotonic_increasing(self) -> bool:
"""
Return a boolean if the values are equal or increasing.
Returns:
bool
"""
return typing.cast(
bool,
self._block.is_monotonic_increasing(self._block.index_columns),
)
@property
@validations.requires_ordering()
def is_monotonic_decreasing(self) -> bool:
"""
Return a boolean if the values are equal or decreasing.
Returns:
bool
"""
return typing.cast(
bool,
self._block.is_monotonic_decreasing(self._block.index_columns),
)
@property
def is_unique(self) -> bool:
# TODO: Cache this at block level
# Avoid circular imports
return not self.has_duplicates
@property
def has_duplicates(self) -> bool:
# TODO: Cache this at block level
# Avoid circular imports
import bigframes.core.block_transforms as block_ops
import bigframes.dataframe as df
duplicates_block, indicator = block_ops.indicate_duplicates(
self._block, self._block.index_columns
)
duplicates_block = duplicates_block.select_columns(
[indicator]
).with_column_labels(["is_duplicate"])
duplicates_df = df.DataFrame(duplicates_block)
return duplicates_df["is_duplicate"].any()
@property
def T(self) -> Index:
return self.transpose()
@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._query_job = self._block._compute_dry_run()
return self._query_job
def __repr__(self) -> str:
# Protect against errors with uninitialized Series. See:
# https://github.com/googleapis/python-bigquery-dataframes/issues/728
if not hasattr(self, "_block"):
return object.__repr__(self)
# TODO(swast): Add a timeout here? If the query is taking a long time,
# maybe we just print the job metadata that we have so far?
# TODO(swast): Avoid downloading the whole series by using job
# metadata, like we do with DataFrame.
opts = bigframes.options.display
max_results = opts.max_rows
if opts.repr_mode == "deferred":
return formatter.repr_query_job(self._block._compute_dry_run())
pandas_df, _, query_job = self._block.retrieve_repr_request_results(max_results)
self._query_job = query_job
return repr(pandas_df.index)
def copy(self, name: Optional[Hashable] = None):
copy_index = Index(self._block)
if name is not None:
copy_index.name = name
return copy_index
def to_series(
self, index: Optional[Index] = None, name: Optional[Hashable] = None
) -> bigframes.series.Series:
if self.nlevels != 1:
NotImplementedError(
f"Converting multi-index to series is not yet supported. {constants.FEEDBACK_LINK}"
)
import bigframes.series
name = self.name if name is None else name
if index is None:
return bigframes.series.Series(data=self, index=self, name=name)
else:
return bigframes.series.Series(data=self, index=Index(index), name=name)
def get_level_values(self, level) -> Index:
level_n = level if isinstance(level, int) else self.names.index(level)
block = self._block.drop_levels(
[self._block.index_columns[i] for i in range(self.nlevels) if i != level_n]
)
return Index(block)
def _memory_usage(self) -> int:
(n_rows,) = self.shape
return sum(
self.dtypes.map(
lambda dtype: bigframes.dtypes.DTYPE_BYTE_SIZES.get(dtype, 8) * n_rows
)
)
def transpose(self) -> Index:
return self
def sort_values(self, *, ascending: bool = True, na_position: str = "last"):
if na_position not in ["first", "last"]:
raise ValueError("Param na_position must be one of 'first' or 'last'")
na_last = na_position == "last"
index_columns = self._block.index_columns
ordering = [
order.ascending_over(column, na_last)
if ascending
else order.descending_over(column, na_last)
for column in index_columns
]
return Index(self._block.order_by(ordering))
def astype(
self,
dtype: Union[bigframes.dtypes.DtypeString, bigframes.dtypes.Dtype],
) -> Index:
if self.nlevels > 1:
raise TypeError("Multiindex does not support 'astype'")
return self._apply_unary_expr(
ops.AsTypeOp(to_type=dtype).as_expr(ex.free_var("arg"))
)
def all(self) -> bool:
if self.nlevels > 1:
raise TypeError("Multiindex does not support 'all'")
return typing.cast(bool, self._apply_aggregation(agg_ops.all_op))
def any(self) -> bool:
if self.nlevels > 1:
raise TypeError("Multiindex does not support 'any'")
return typing.cast(bool, self._apply_aggregation(agg_ops.any_op))
def nunique(self) -> int:
return typing.cast(int, self._apply_aggregation(agg_ops.nunique_op))
def max(self) -> typing.Any:
return self._apply_aggregation(agg_ops.max_op)
def min(self) -> typing.Any:
return self._apply_aggregation(agg_ops.min_op)
@validations.requires_ordering()
def argmax(self) -> int:
block, row_nums = self._block.promote_offsets()
block = block.order_by(
[
*[order.descending_over(col) for col in self._block.index_columns],
order.ascending_over(row_nums),
]
)
import bigframes.series as series
return typing.cast(int, series.Series(block.select_column(row_nums)).iloc[0])
@validations.requires_ordering()
def argmin(self) -> int:
block, row_nums = self._block.promote_offsets()
block = block.order_by(
[
*[order.ascending_over(col) for col in self._block.index_columns],
order.ascending_over(row_nums),
]
)
import bigframes.series as series
return typing.cast(int, series.Series(block.select_column(row_nums)).iloc[0])
def value_counts(
self,
normalize: bool = False,
sort: bool = True,
ascending: bool = False,
*,
dropna: bool = True,
):
block = block_ops.value_counts(
self._block,
self._block.index_columns,
normalize=normalize,
ascending=ascending,
dropna=dropna,
)
import bigframes.series as series
return series.Series(block)
def fillna(self, value=None) -> Index:
if self.nlevels > 1:
raise TypeError("Multiindex does not support 'fillna'")
return self._apply_unary_expr(
ops.fillna_op.as_expr(ex.free_var("arg"), ex.const(value))
)
def rename(self, name: Union[str, Sequence[str]]) -> Index:
names = [name] if isinstance(name, str) else list(name)
if len(names) != self.nlevels:
raise ValueError("'name' must be same length as levels")
return Index(self._block.with_index_labels(names))
def drop(
self,
labels: typing.Any,
) -> Index:
# ignore axis, columns params
block = self._block
level_id = self._block.index_columns[0]
if utils.is_list_like(labels):
block, inverse_condition_id = block.apply_unary_op(
level_id, ops.IsInOp(values=tuple(labels), match_nulls=True)
)
block, condition_id = block.apply_unary_op(
inverse_condition_id, ops.invert_op
)
else:
block, condition_id = block.project_expr(
ops.ne_op.as_expr(level_id, ex.const(labels))
)
block = block.filter_by_id(condition_id, keep_null=True)
block = block.drop_columns([condition_id])
return Index(block)
def dropna(self, how: typing.Literal["all", "any"] = "any") -> Index:
if how not in ("any", "all"):
raise ValueError("'how' must be one of 'any', 'all'")
result = block_ops.dropna(self._block, self._block.index_columns, how=how)
return Index(result)
def drop_duplicates(self, *, keep: str = "first") -> Index:
if keep is not False:
validations.enforce_ordered(self, "drop_duplicates")
block = block_ops.drop_duplicates(self._block, self._block.index_columns, keep)
return Index(block)
def isin(self, values) -> Index:
if not utils.is_list_like(values):
raise TypeError(
"only list-like objects are allowed to be passed to "
f"isin(), you passed a [{type(values).__name__}]"
)
return self._apply_unary_expr(
ops.IsInOp(values=tuple(values), match_nulls=True).as_expr(
ex.free_var("arg")
)
).fillna(value=False)
def _apply_unary_expr(
self,
op: ex.Expression,
) -> Index:
"""Applies a unary operator to the index."""
if len(op.free_variables) != 1:
raise ValueError("Expression must have exactly 1 unbound variable.")
unbound_variable = op.free_variables[0]
block = self._block
result_ids = []
for col in self._block.index_columns:
block, result_id = block.project_expr(
op.bind_variables({unbound_variable: ex.deref(col)})
)
result_ids.append(result_id)
block = block.set_index(result_ids, index_labels=self._block.index.names)
return Index(block)
def _apply_aggregation(self, op: agg_ops.UnaryAggregateOp) -> typing.Any:
if self.nlevels > 1:
raise NotImplementedError(f"Multiindex does not yet support {op.name}")
column_id = self._block.index_columns[0]
return self._block.get_stat(column_id, op)
def __getitem__(self, key: int) -> typing.Any:
if isinstance(key, int):
if key != -1:
result_pd_df, _ = self._block.slice(key, key + 1, 1).to_pandas()
else: # special case, want [-1:] instead of [-1:0]
result_pd_df, _ = self._block.slice(key).to_pandas()
if result_pd_df.index.empty:
raise IndexError("single positional indexer is out-of-bounds")
return result_pd_df.index[0]
else:
raise NotImplementedError(f"Index key not supported {key}")
def to_pandas(self) -> pandas.Index:
"""Gets the Index as a pandas Index.
Returns:
pandas.Index:
A pandas Index with all of the labels from this Index.
"""
return self._block.index.to_pandas(ordered=True)
def to_numpy(self, dtype=None, **kwargs) -> np.ndarray:
return self.to_pandas().to_numpy(dtype, **kwargs)
__array__ = to_numpy
def __len__(self):
return self.shape[0]