Skip to content

Commit 09635e6

Browse files
committed
refactor: simplify _classify_columns logic in _flatten.py
1 parent 6d28d28 commit 09635e6

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

bigframes/display/_flatten.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,22 +165,24 @@ def _classify_columns(
165165
Returns:
166166
A ColumnClassification object containing lists of column names for each category.
167167
"""
168-
# Maps column names to their structural category to simplify list building.
169-
categories: dict[str, str] = {}
170168

171-
for col, dtype in dataframe.dtypes.items():
172-
col_name = str(col)
169+
def get_category(dtype: pd.api.extensions.ExtensionDtype) -> str:
173170
pa_type = getattr(dtype, "pyarrow_dtype", None)
171+
if pa_type:
172+
if pa.types.is_struct(pa_type):
173+
return "struct"
174+
if pa.types.is_list(pa_type):
175+
return (
176+
"array_of_struct"
177+
if pa.types.is_struct(pa_type.value_type)
178+
else "array"
179+
)
180+
return "clear"
174181

175-
if not pa_type:
176-
categories[col_name] = "clear"
177-
elif pa.types.is_struct(pa_type):
178-
categories[col_name] = "struct"
179-
elif pa.types.is_list(pa_type):
180-
is_struct_array = pa.types.is_struct(pa_type.value_type)
181-
categories[col_name] = "array_of_struct" if is_struct_array else "array"
182-
else:
183-
categories[col_name] = "clear"
182+
# Maps column names to their structural category to simplify list building.
183+
categories = {
184+
str(col): get_category(dtype) for col, dtype in dataframe.dtypes.items()
185+
}
184186

185187
return ColumnClassification(
186188
struct_columns=tuple(c for c, cat in categories.items() if cat == "struct"),

0 commit comments

Comments
 (0)