MCPcopy Create free account
hub / github.com/apache/arrow / test_to_pylist_bulk_paths

Function test_to_pylist_bulk_paths

python/pyarrow/tests/test_array.py:468–523  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

466
467
468def test_to_pylist_bulk_paths():
469 # GH-50326: to_pylist converts through scalar-free _getitem_py
470 # specializations; the result must match the per-scalar conversion
471 # exactly.
472 arrays = [
473 pa.array([[1, None, 3], None, [], [4]], type=pa.list_(pa.int32())),
474 pa.array([["a", None], None, [], ["bcd", ""]],
475 type=pa.list_(pa.string())),
476 pa.array([["a", None], None, [], ["bcd", ""]],
477 type=pa.large_list(pa.large_string())),
478 pa.array([[1, None], None, [3, 4]], type=pa.list_(pa.int32(), 2)),
479 pa.array([[[1], [2, None]], None, [None, [3]]],
480 type=pa.list_(pa.list_(pa.int32()))),
481 pa.array([[("k1", 1), ("k2", None)], None, []],
482 type=pa.map_(pa.string(), pa.int32())),
483 pa.array(["a", None, "", "\N{GRINNING FACE} \N{SNOWMAN}"],
484 type=pa.string()),
485 pa.array(["a", None, "", "\N{GRINNING FACE} \N{SNOWMAN}"],
486 type=pa.large_string()),
487 pa.array([b"a\x00b", None, b"", b"\xff"], type=pa.binary()),
488 pa.array([b"a\x00b", None, b""], type=pa.large_binary()),
489 # View types store short values inline and long values out-of-line;
490 # cover both, plus NUL bytes and non-ASCII data.
491 pa.array(["a", None, "", "\N{GRINNING FACE} \N{SNOWMAN}",
492 "long string exceeding the inline view size"],
493 type=pa.string_view()),
494 pa.array([b"a\x00b", None, b"", b"\xff",
495 b"long binary value exceeding the inline view size"],
496 type=pa.binary_view()),
497 pa.array([[b"x", None, b"\x00y"], None, []],
498 type=pa.list_(pa.binary())),
499 pa.array([1, None, -(2**62), 2**62], type=pa.int64()),
500 pa.array([0, None, 2**63 + 7], type=pa.uint64()),
501 pa.array([-128, 127, None], type=pa.int8()),
502 pa.array([1.5, None, -0.5], type=pa.float64()),
503 pa.array([1.5, None], type=pa.float32()),
504 pa.array([True, None, False], type=pa.bool_()),
505 pa.array([{"a": 1, "b": "x"}, None, {"a": None, "b": None}],
506 type=pa.struct([("a", pa.int32()), ("b", pa.string())])),
507 pa.array([], type=pa.list_(pa.int32())),
508 pa.array([None, None], type=pa.list_(pa.string())),
509 ]
510 for arr in arrays:
511 for view in (arr, arr.slice(1), arr.slice(0, 2), arr.slice(2)):
512 assert view.to_pylist() == [x.as_py() for x in view]
513
514 # Values inside numeric lists must stay Python ints/None, never floats
515 result = pa.array([[1, None, 3]], type=pa.list_(pa.int32())).to_pylist()
516 assert result == [[1, None, 3]]
517 assert [type(x) for x in result[0]] == [int, type(None), int]
518
519 # Duplicate struct field names raise like StructScalar.as_py does
520 dup = pa.StructArray.from_arrays(
521 [pa.array([1, 2]), pa.array(["a", "b"])], names=["x", "x"])
522 with pytest.raises(ValueError, match="duplicate field names"):
523 dup.to_pylist()
524
525

Callers

nothing calls this directly

Calls 5

as_pyMethod · 0.80
typeEnum · 0.50
arrayMethod · 0.45
stringMethod · 0.45
sliceMethod · 0.45

Tested by

no test coverage detected