Skip to content

Commit c11adc6

Browse files
committed
More compat pyarrow<18
1 parent d1982ea commit c11adc6

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ dependencies = [
2121
"numpy>=2",
2222
# We use internal pd._libs.missing and experimental ArrowExtensionArray
2323
"pandas>=2.2.3,<2.4",
24-
"pyarrow>=16", # remove struct_field_names when upgraded to 18+
24+
"pyarrow>=16", # remove struct_field_names() and struct_fields() when upgraded to 18+
2525
"universal_pathlib>=0.2",
2626
]
2727

src/nested_pandas/series/utils.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ def struct_field_names(struct_type: pa.StructType) -> list[str]:
2020
return [f.name for f in struct_type]
2121

2222

23+
def struct_fields(struct_type: pa.StructType) -> list[pa.Field]:
24+
"""Return fields of a pyarrow.StructType in a pyarrow<18-compatible way.
25+
26+
Note: Once we bump our pyarrow requirement to ">=18", this helper can be
27+
replaced with direct usage of ``struct_type.fields`` throughout the codebase.
28+
"""
29+
return [struct_type.field(i) for i in range(struct_type.num_fields)]
30+
31+
2332
def is_pa_type_a_list(pa_type: pa.DataType) -> bool:
2433
"""Check if the given pyarrow type is a list type.
2534
@@ -108,7 +117,7 @@ def align_struct_list_offsets(array: pa.StructArray) -> pa.StructArray:
108117
value_lengths = list_array.value_lengths()
109118
elif not value_lengths.equals(list_array.value_lengths()):
110119
raise ValueError(
111-
f"List lengths do not match for struct fields {array.type.fields[0].name} and {field.name}",
120+
f"List lengths do not match for struct fields {array.type.field(0).name} and {field.name}",
112121
)
113122

114123
list_arrays.append(
@@ -119,7 +128,7 @@ def align_struct_list_offsets(array: pa.StructArray) -> pa.StructArray:
119128
)
120129
new_array = pa.StructArray.from_arrays(
121130
arrays=list_arrays,
122-
fields=array.type.fields,
131+
fields=struct_fields(array.type),
123132
)
124133
return new_array
125134

@@ -286,7 +295,7 @@ def validate_struct_list_type(t: pa.StructType) -> None:
286295
if not pa.types.is_struct(t):
287296
raise ValueError(f"Expected a StructType, got {t}")
288297

289-
for field in t.fields:
298+
for field in struct_fields(t):
290299
if not is_pa_type_a_list(field.type):
291300
raise ValueError(f"Expected a ListType for field {field.name}, got {field.type}")
292301

0 commit comments

Comments
 (0)