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

Function test_table_group_by

python/pyarrow/tests/test_table.py:2870–3023  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2868
2869@pytest.mark.acero
2870def test_table_group_by():
2871 def sorted_by_keys(d):
2872 # Ensure a guaranteed order of keys for aggregation results.
2873 if "keys2" in d:
2874 keys = tuple(zip(d["keys"], d["keys2"]))
2875 else:
2876 keys = d["keys"]
2877 sorted_keys = sorted(keys)
2878 sorted_d = {"keys": sorted(d["keys"])}
2879 for entry in d:
2880 if entry == "keys":
2881 continue
2882 values = dict(zip(keys, d[entry]))
2883 for k in sorted_keys:
2884 sorted_d.setdefault(entry, []).append(values[k])
2885 return sorted_d
2886
2887 table = pa.table([
2888 pa.array(["a", "a", "b", "b", "c"]),
2889 pa.array(["X", "X", "Y", "Z", "Z"]),
2890 pa.array([1, 2, 3, 4, 5]),
2891 pa.array([10, 20, 30, 40, 50])
2892 ], names=["keys", "keys2", "values", "bigvalues"])
2893
2894 r = table.group_by("keys").aggregate([
2895 ("values", "hash_sum")
2896 ])
2897 assert sorted_by_keys(r.to_pydict()) == {
2898 "keys": ["a", "b", "c"],
2899 "values_sum": [3, 7, 5]
2900 }
2901
2902 r = table.group_by("keys").aggregate([
2903 ("values", "hash_sum"),
2904 ("values", "hash_count")
2905 ])
2906 assert sorted_by_keys(r.to_pydict()) == {
2907 "keys": ["a", "b", "c"],
2908 "values_sum": [3, 7, 5],
2909 "values_count": [2, 2, 1]
2910 }
2911
2912 # Test without hash_ prefix
2913 r = table.group_by("keys").aggregate([
2914 ("values", "sum")
2915 ])
2916 assert sorted_by_keys(r.to_pydict()) == {
2917 "keys": ["a", "b", "c"],
2918 "values_sum": [3, 7, 5]
2919 }
2920
2921 r = table.group_by("keys").aggregate([
2922 ("values", "max"),
2923 ("bigvalues", "sum")
2924 ])
2925 assert sorted_by_keys(r.to_pydict()) == {
2926 "keys": ["a", "b", "c"],
2927 "values_max": [2, 4, 5],

Callers

nothing calls this directly

Calls 5

sorted_by_keysFunction · 0.85
aggregateMethod · 0.80
CountOptionsMethod · 0.80
to_batchesMethod · 0.80
arrayMethod · 0.45

Tested by

no test coverage detected