Skip to content

Commit 4dfef81

Browse files
committed
test NestedExtensionArray.to_pyarrow_scalar
1 parent 89ba38e commit 4dfef81

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/nested_pandas/series/ext_array.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ def to_pyarrow_scalar(self, list_struct: bool = False) -> pa.ListScalar:
785785
----------
786786
list_struct : bool, optional
787787
If False (default), return list-struct-list scalar,
788-
otherwise list-list-struct scalar.
788+
otherwise list-list-struct scalar.
789789
790790
Returns
791791
-------

tests/nested_pandas/series/test_ext_array.py

+32
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,38 @@ def test_chunked_list_struct_array():
626626
assert ext_array.chunked_list_struct_array.type == ext_array._pyarrow_list_struct_dtype
627627

628628

629+
def test_to_pyarrow_scalar():
630+
"""Test .to_pyarrow_scalar is correct."""
631+
struct_array = pa.StructArray.from_arrays(
632+
arrays=[
633+
pa.array([np.array([1, 2, 3]), np.array([1, 2, 1])]),
634+
pa.array([-np.array([4.0, 5.0, 6.0]), -np.array([3.0, 4.0, 5.0])]),
635+
],
636+
names=["a", "b"],
637+
)
638+
ext_array = NestedExtensionArray(struct_array)
639+
640+
desired_struct_list = pa.scalar(
641+
[
642+
{"a": [1, 2, 3], "b": [-4.0, -5.0, -6.0]},
643+
{"a": [1, 2, 1], "b": [-3.0, -4.0, -5.0]},
644+
],
645+
type=pa.list_(
646+
pa.struct([pa.field("a", pa.list_(pa.int64())), pa.field("b", pa.list_(pa.float64()))])
647+
),
648+
)
649+
desired_list_struct = pa.scalar(
650+
[
651+
[{"a": 1, "b": -4.0}, {"a": 2, "b": -5.0}, {"a": 3, "b": -6.0}],
652+
[{"a": 1, "b": -3.0}, {"a": 2, "b": -4.0}, {"a": 1, "b": -5.0}],
653+
],
654+
type=pa.list_(pa.list_(pa.struct([pa.field("a", pa.int64()), pa.field("b", pa.float64())]))),
655+
)
656+
# pyarrow returns a single bool for ==
657+
assert ext_array.to_pyarrow_scalar(list_struct=False) == desired_struct_list
658+
assert ext_array.to_pyarrow_scalar(list_struct=True) == desired_list_struct
659+
660+
629661
def test_list_offsets_single_chunk():
630662
"""Test that the .list_offset property is correct for a single chunk."""
631663
struct_array = pa.StructArray.from_arrays(

0 commit comments

Comments
 (0)