Skip to content

Commit d09e044

Browse files
authored
Merge pull request #197 from lincc-frameworks/set-chunked-field
Fix NestedExtenstionArray.set_flat_field for chunked values
2 parents 61ed4f1 + dde4400 commit d09e044

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/nested_pandas/series/ext_array.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,8 @@ def set_flat_field(self, field: str, value: ArrayLike, *, keep_dtype: bool = Fal
878878
if len(pa_array) != self.flat_length:
879879
raise ValueError("The input must be a struct_scalar or have the same length as the flat arrays")
880880

881+
if isinstance(pa_array, pa.ChunkedArray):
882+
pa_array = pa_array.combine_chunks()
881883
list_array = pa.ListArray.from_arrays(values=pa_array, offsets=self.list_offsets)
882884

883885
return self.set_list_field(field, list_array, keep_dtype=keep_dtype)

tests/nested_pandas/series/test_ext_array.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,25 @@ def test_set_flat_field_keep_dtype_raises_for_new_field():
14541454
ext_array.set_flat_field("c", [True, False, True, False, True, False, True], keep_dtype=False)
14551455

14561456

1457+
def test_set_flat_field_chunked_values():
1458+
"""Tests that set_flat_field works fine with input value which is chunked"""
1459+
struct_array = pa.StructArray.from_arrays(
1460+
arrays=[
1461+
pa.array([np.array([1.0, 2.0, 3.0]), np.array([1.0, 2.0, 3.0, 4.0])]),
1462+
pa.array([-np.array([4.0, 5.0, 6.0]), -np.array([3.0, 4.0, 5.0, 6.0])]),
1463+
],
1464+
names=["a", "b"],
1465+
)
1466+
ext_array = NestedExtensionArray(struct_array)
1467+
1468+
new_chunked_values = pa.chunked_array([["x", "y", "z"], ["x1", "x2", "x3", "x4"]])
1469+
assert new_chunked_values.num_chunks > 1
1470+
new_series = pd.Series(new_chunked_values, dtype=pd.ArrowDtype(new_chunked_values.type))
1471+
1472+
# Previously failed for series with chunked array
1473+
ext_array.set_flat_field("c", new_series)
1474+
1475+
14571476
def test_set_list_field_new_field():
14581477
"""Tests setting a new field with a new "list" array"""
14591478
struct_array = pa.StructArray.from_arrays(

0 commit comments

Comments
 (0)