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

Function test_write_table

python/pyarrow/tests/test_dataset.py:4759–4803  ·  view source on GitHub ↗
(tempdir)

Source from the content-addressed store, hash-verified

4757
4758
4759def test_write_table(tempdir):
4760 table = pa.table([
4761 pa.array(range(20)), pa.array(random.random() for _ in range(20)),
4762 pa.array(['a'] * 10 + ['b'] * 10)
4763 ], names=["f1", "f2", "part"])
4764
4765 base_dir = tempdir / 'single'
4766 ds.write_dataset(table, base_dir,
4767 basename_template='dat_{i}.arrow', format="feather")
4768 # check that all files are present
4769 file_paths = list(base_dir.rglob("*"))
4770 expected_paths = [base_dir / "dat_0.arrow"]
4771 assert set(file_paths) == set(expected_paths)
4772 # check Table roundtrip
4773 result = ds.dataset(base_dir, format="ipc").to_table()
4774 assert result.equals(table)
4775
4776 # with partitioning
4777 base_dir = tempdir / 'partitioned'
4778 expected_paths = [
4779 base_dir / "part=a", base_dir / "part=a" / "dat_0.arrow",
4780 base_dir / "part=b", base_dir / "part=b" / "dat_0.arrow"
4781 ]
4782
4783 visited_paths = []
4784 visited_sizes = []
4785
4786 def file_visitor(written_file):
4787 visited_paths.append(written_file.path)
4788 visited_sizes.append(written_file.size)
4789
4790 partitioning = ds.partitioning(
4791 pa.schema([("part", pa.string())]), flavor="hive")
4792 ds.write_dataset(table, base_dir, format="feather",
4793 basename_template='dat_{i}.arrow',
4794 partitioning=partitioning, file_visitor=file_visitor)
4795 file_paths = list(base_dir.rglob("*"))
4796 assert set(file_paths) == set(expected_paths)
4797 actual_sizes = [os.path.getsize(path) for path in visited_paths]
4798 assert visited_sizes == actual_sizes
4799 result = ds.dataset(base_dir, format="ipc", partitioning=partitioning)
4800 assert result.to_table().equals(table)
4801 assert len(visited_paths) == 2
4802 for visited_path in visited_paths:
4803 assert pathlib.Path(visited_path) in expected_paths
4804
4805
4806def test_write_table_multiple_fragments(tempdir):

Callers

nothing calls this directly

Calls 9

listFunction · 0.85
lenFunction · 0.85
equalsMethod · 0.80
partitioningMethod · 0.80
arrayMethod · 0.45
to_tableMethod · 0.45
schemaMethod · 0.45
stringMethod · 0.45
PathMethod · 0.45

Tested by

no test coverage detected