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

Function test_dataset_sort_by

python/pyarrow/tests/test_dataset.py:5818–5863  ·  view source on GitHub ↗
(tempdir, dstype)

Source from the content-addressed store, hash-verified

5816 "fs", "mem"
5817])
5818def test_dataset_sort_by(tempdir, dstype):
5819 table = pa.table([
5820 pa.array([3, 1, 4, 2, 5]),
5821 pa.array(["b", "a", "b", "a", "c"]),
5822 ], names=["values", "keys"])
5823
5824 if dstype == "fs":
5825 ds.write_dataset(table, tempdir / "t1", format="ipc")
5826 dt = ds.dataset(tempdir / "t1", format="ipc")
5827 elif dstype == "mem":
5828 dt = ds.dataset(table)
5829 else:
5830 raise NotImplementedError
5831
5832 assert dt.sort_by("values").to_table().to_pydict() == {
5833 "keys": ["a", "a", "b", "b", "c"],
5834 "values": [1, 2, 3, 4, 5]
5835 }
5836
5837 assert dt.sort_by([("values", "descending", "at_end")]).to_table().to_pydict() == {
5838 "keys": ["c", "b", "b", "a", "a"],
5839 "values": [5, 4, 3, 2, 1]
5840 }
5841
5842 assert dt.filter((pc.field("values") < 4)).sort_by(
5843 "values"
5844 ).to_table().to_pydict() == {
5845 "keys": ["a", "a", "b"],
5846 "values": [1, 2, 3]
5847 }
5848
5849 table = pa.Table.from_arrays([
5850 pa.array([5, 7, 7, 35], type=pa.int64()),
5851 pa.array(["foo", "car", "bar", "foobar"])
5852 ], names=["a", "b"])
5853 dt = ds.dataset(table)
5854
5855 sorted_tab = dt.sort_by([("a", "descending", "at_end")])
5856 sorted_tab_dict = sorted_tab.to_table().to_pydict()
5857 assert sorted_tab_dict["a"] == [35, 7, 7, 5]
5858 assert sorted_tab_dict["b"] == ["foobar", "car", "bar", "foo"]
5859
5860 sorted_tab = dt.sort_by([("a", "ascending", "at_end")])
5861 sorted_tab_dict = sorted_tab.to_table().to_pydict()
5862 assert sorted_tab_dict["a"] == [5, 7, 7, 35]
5863 assert sorted_tab_dict["b"] == ["foo", "car", "bar", "foobar"]
5864
5865
5866def test_checksum_write_dataset_read_dataset_to_table(tempdir):

Callers

nothing calls this directly

Calls 4

filterMethod · 0.80
arrayMethod · 0.45
to_tableMethod · 0.45
fieldMethod · 0.45

Tested by

no test coverage detected