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

Function test_table_group_by

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

Source from the content-addressed store, hash-verified

2929
2930@pytest.mark.acero
2931def test_table_group_by():
2932 def sorted_by_keys(d):
2933 # Ensure a guaranteed order of keys for aggregation results.
2934 if "keys2" in d:
2935 keys = tuple(zip(d["keys"], d["keys2"]))
2936 else:
2937 keys = d["keys"]
2938 sorted_keys = sorted(keys)
2939 sorted_d = {"keys": sorted(d["keys"])}
2940 for entry in d:
2941 if entry == "keys":
2942 continue
2943 values = dict(zip(keys, d[entry]))
2944 for k in sorted_keys:
2945 sorted_d.setdefault(entry, []).append(values[k])
2946 return sorted_d
2947
2948 table = pa.table([
2949 pa.array(["a", "a", "b", "b", "c"]),
2950 pa.array(["X", "X", "Y", "Z", "Z"]),
2951 pa.array([1, 2, 3, 4, 5]),
2952 pa.array([10, 20, 30, 40, 50])
2953 ], names=["keys", "keys2", "values", "bigvalues"])
2954
2955 r = table.group_by("keys").aggregate([
2956 ("values", "hash_sum")
2957 ])
2958 assert sorted_by_keys(r.to_pydict()) == {
2959 "keys": ["a", "b", "c"],
2960 "values_sum": [3, 7, 5]
2961 }
2962
2963 r = table.group_by("keys").aggregate([
2964 ("values", "hash_sum"),
2965 ("values", "hash_count")
2966 ])
2967 assert sorted_by_keys(r.to_pydict()) == {
2968 "keys": ["a", "b", "c"],
2969 "values_sum": [3, 7, 5],
2970 "values_count": [2, 2, 1]
2971 }
2972
2973 # Test without hash_ prefix
2974 r = table.group_by("keys").aggregate([
2975 ("values", "sum")
2976 ])
2977 assert sorted_by_keys(r.to_pydict()) == {
2978 "keys": ["a", "b", "c"],
2979 "values_sum": [3, 7, 5]
2980 }
2981
2982 r = table.group_by("keys").aggregate([
2983 ("values", "max"),
2984 ("bigvalues", "sum")
2985 ])
2986 assert sorted_by_keys(r.to_pydict()) == {
2987 "keys": ["a", "b", "c"],
2988 "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