|
| 1 | +# Copyright 2025 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 | +from __future__ import annotations |
| 16 | + |
| 17 | +import typing |
| 18 | + |
| 19 | +from bigframes import dataframe, dtypes, series |
| 20 | +from bigframes.core.reshape import api as rs |
| 21 | + |
| 22 | + |
| 23 | +def describe( |
| 24 | + input: dataframe.DataFrame | series.Series, |
| 25 | + include: None | typing.Literal["all"], |
| 26 | +) -> dataframe.DataFrame | series.Series: |
| 27 | + if isinstance(input, series.Series): |
| 28 | + # Convert the series to a dataframe, describe it, and cast the result back to a series. |
| 29 | + return series.Series(describe(input.to_frame(), include)._block) |
| 30 | + elif not isinstance(input, dataframe.DataFrame): |
| 31 | + raise TypeError(f"Unsupported type: {type(input)}") |
| 32 | + |
| 33 | + if include is None: |
| 34 | + numeric_df = _select_dtypes( |
| 35 | + input, |
| 36 | + dtypes.NUMERIC_BIGFRAMES_TYPES_RESTRICTIVE |
| 37 | + + dtypes.TEMPORAL_NUMERIC_BIGFRAMES_TYPES, |
| 38 | + ) |
| 39 | + if len(numeric_df.columns) == 0: |
| 40 | + # Describe eligible non-numeric columns |
| 41 | + return _describe_non_numeric(input) |
| 42 | + |
| 43 | + # Otherwise, only describe numeric columns |
| 44 | + return _describe_numeric(input) |
| 45 | + |
| 46 | + elif include == "all": |
| 47 | + numeric_result = _describe_numeric(input) |
| 48 | + non_numeric_result = _describe_non_numeric(input) |
| 49 | + |
| 50 | + if len(numeric_result.columns) == 0: |
| 51 | + return non_numeric_result |
| 52 | + elif len(non_numeric_result.columns) == 0: |
| 53 | + return numeric_result |
| 54 | + else: |
| 55 | + # Use reindex after join to preserve the original column order. |
| 56 | + return rs.concat( |
| 57 | + [non_numeric_result, numeric_result], axis=1 |
| 58 | + )._reindex_columns(input.columns) |
| 59 | + |
| 60 | + else: |
| 61 | + raise ValueError(f"Unsupported include type: {include}") |
| 62 | + |
| 63 | + |
| 64 | +def _describe_numeric(df: dataframe.DataFrame) -> dataframe.DataFrame: |
| 65 | + number_df_result = typing.cast( |
| 66 | + dataframe.DataFrame, |
| 67 | + _select_dtypes(df, dtypes.NUMERIC_BIGFRAMES_TYPES_RESTRICTIVE).agg( |
| 68 | + [ |
| 69 | + "count", |
| 70 | + "mean", |
| 71 | + "std", |
| 72 | + "min", |
| 73 | + "25%", |
| 74 | + "50%", |
| 75 | + "75%", |
| 76 | + "max", |
| 77 | + ] |
| 78 | + ), |
| 79 | + ) |
| 80 | + temporal_df_result = typing.cast( |
| 81 | + dataframe.DataFrame, |
| 82 | + _select_dtypes(df, dtypes.TEMPORAL_NUMERIC_BIGFRAMES_TYPES).agg(["count"]), |
| 83 | + ) |
| 84 | + |
| 85 | + if len(number_df_result.columns) == 0: |
| 86 | + return temporal_df_result |
| 87 | + elif len(temporal_df_result.columns) == 0: |
| 88 | + return number_df_result |
| 89 | + else: |
| 90 | + import bigframes.core.reshape.api as rs |
| 91 | + |
| 92 | + original_columns = _select_dtypes( |
| 93 | + df, |
| 94 | + dtypes.NUMERIC_BIGFRAMES_TYPES_RESTRICTIVE |
| 95 | + + dtypes.TEMPORAL_NUMERIC_BIGFRAMES_TYPES, |
| 96 | + ).columns |
| 97 | + |
| 98 | + # Use reindex after join to preserve the original column order. |
| 99 | + return rs.concat( |
| 100 | + [number_df_result, temporal_df_result], |
| 101 | + axis=1, |
| 102 | + )._reindex_columns(original_columns) |
| 103 | + |
| 104 | + |
| 105 | +def _describe_non_numeric(df: dataframe.DataFrame) -> dataframe.DataFrame: |
| 106 | + return typing.cast( |
| 107 | + dataframe.DataFrame, |
| 108 | + _select_dtypes( |
| 109 | + df, |
| 110 | + [ |
| 111 | + dtypes.STRING_DTYPE, |
| 112 | + dtypes.BOOL_DTYPE, |
| 113 | + dtypes.BYTES_DTYPE, |
| 114 | + dtypes.TIME_DTYPE, |
| 115 | + ], |
| 116 | + ).agg(["count", "nunique"]), |
| 117 | + ) |
| 118 | + |
| 119 | + |
| 120 | +def _select_dtypes( |
| 121 | + df: dataframe.DataFrame, dtypes: typing.Sequence[dtypes.Dtype] |
| 122 | +) -> dataframe.DataFrame: |
| 123 | + """Selects columns without considering inheritance relationships.""" |
| 124 | + columns = [ |
| 125 | + col_id |
| 126 | + for col_id, dtype in zip(df._block.value_columns, df._block.dtypes) |
| 127 | + if dtype in dtypes |
| 128 | + ] |
| 129 | + return dataframe.DataFrame(df._block.select_columns(columns)) |
0 commit comments