Skip to content

Commit 856d7bf

Browse files
committed
Fix flat_length() of ext array
1 parent 6798700 commit 856d7bf

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/nested_pandas/series/ext_array.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def __getitem__(self, item):
241241
return type(self)(self._chunked_array.filter(pa_item), validate=False)
242242
# It should be covered by check_array_indexer above
243243
raise IndexError(
244-
"Only integers, slices and integer or " "boolean arrays are valid indices."
244+
"Only integers, slices and integer or boolean arrays are valid indices."
245245
) # pragma: no cover
246246

247247
if isinstance(item, tuple):
@@ -764,7 +764,9 @@ def field_names(self) -> list[str]:
764764
@property
765765
def flat_length(self) -> int:
766766
"""Length of the flat arrays"""
767-
return sum(chunk.field(0).value_lengths().sum().as_py() for chunk in self._chunked_array.iterchunks())
767+
return sum(
768+
chunk.field(0).value_lengths().sum().as_py() or 0 for chunk in self._chunked_array.iterchunks()
769+
)
768770

769771
@property
770772
def num_chunks(self) -> int:

tests/nested_pandas/series/test_ext_array.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,21 @@ def test_flat_length():
12401240
assert ext_array.flat_length == 7
12411241

12421242

1243+
def test_flat_length_empty_chunk():
1244+
"""Test that the flat length works correctly for an empty chunk."""
1245+
struct_array = pa.StructArray.from_arrays(
1246+
arrays=[
1247+
pa.array([np.array([1.0, 2.0, 3.0]), np.array([1.0, 2.0, 1.0, 2.0])]),
1248+
pa.array([-np.array([4.0, 5.0, 6.0]), -np.array([3.0, 4.0, 5.0, 6.0])]),
1249+
],
1250+
names=["a", "b"],
1251+
)
1252+
empty_struct_array = pa.array([], type=struct_array.type)
1253+
ext_array = NestedExtensionArray(pa.chunked_array([struct_array, empty_struct_array]))
1254+
1255+
assert ext_array.flat_length == 7
1256+
1257+
12431258
def test_num_chunks():
12441259
"""Tests .num_chunks property."""
12451260
struct_array = pa.StructArray.from_arrays(

0 commit comments

Comments
 (0)