Skip to content

Commit 59d0b5a

Browse files
committed
Add NestSeriesAccessor.list_lengths
1 parent c7535c2 commit 59d0b5a

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/nested_pandas/series/accessor.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ def to_flat(self, fields: list[str] | None = None) -> pd.DataFrame:
114114

115115
return pd.DataFrame(flat_series)
116116

117+
@property
118+
def list_lengths(self) -> np.ndarray:
119+
"""Lengths of the list arrays"""
120+
return self._series.array.list_lengths
121+
117122
@property
118123
def flat_length(self) -> int:
119124
"""Length of the flat arrays"""

tests/nested_pandas/series/test_accessor.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,19 @@ def test_fields():
286286
assert_array_equal(series.nest.fields, ["a", "b"])
287287

288288

289+
def test_list_lengths():
290+
"""Test that the .nest.list_lengths attribute works."""
291+
series = pack_seq(
292+
[
293+
pd.DataFrame({"a": [1, 2, 3], "b": [1.0, 5.0, 6.0], "c": ["a", "b", "c"]}),
294+
None,
295+
pd.DataFrame({"a": [1, 2], "b": [None, 0.0], "c": ["a", "b"]}),
296+
]
297+
)
298+
assert series.shape == (3,)
299+
assert series.nest.list_lengths == [3, 0, 2]
300+
301+
289302
def test_flat_length():
290303
"""Test that the .nest.flat_length attribute works."""
291304
struct_array = pa.StructArray.from_arrays(

0 commit comments

Comments
 (0)