|
| 1 | +# Copyright 2024 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +"""This module integrates BigQuery built-in functions for use with DataFrame objects, |
| 17 | +such as array functions: |
| 18 | +https://cloud.google.com/bigquery/docs/reference/standard-sql/array_functions. """ |
| 19 | + |
| 20 | + |
| 21 | +from __future__ import annotations |
| 22 | + |
| 23 | +import typing |
| 24 | + |
| 25 | +import bigframes.operations as ops |
| 26 | + |
| 27 | +if typing.TYPE_CHECKING: |
| 28 | + import bigframes.series as series |
| 29 | + |
| 30 | + |
| 31 | +def array_length(series: series.Series) -> series.Series: |
| 32 | + """Compute the length of each array element in the Series. |
| 33 | +
|
| 34 | + **Examples:** |
| 35 | +
|
| 36 | + >>> import bigframes.pandas as bpd |
| 37 | + >>> import bigframes.bigquery as bbq |
| 38 | + >>> bpd.options.display.progress_bar = None |
| 39 | +
|
| 40 | + >>> s = bpd.Series([[1, 2, 8, 3], [], [3, 4]]) |
| 41 | + >>> bbq.array_length(s) |
| 42 | + 0 4 |
| 43 | + 1 0 |
| 44 | + 2 2 |
| 45 | + dtype: Int64 |
| 46 | +
|
| 47 | + You can also apply this function directly to Series. |
| 48 | +
|
| 49 | + >>> s.apply(bbq.array_length, by_row=False) |
| 50 | + 0 4 |
| 51 | + 1 0 |
| 52 | + 2 2 |
| 53 | + dtype: Int64 |
| 54 | +
|
| 55 | + Returns: |
| 56 | + bigframes.series.Series: A Series of integer values indicating |
| 57 | + the length of each element in the Series. |
| 58 | +
|
| 59 | + """ |
| 60 | + return series._apply_unary_op(ops.len_op) |
0 commit comments