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

Function test_table_group_by

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

Source from the content-addressed store, hash-verified

2821
2822@pytest.mark.acero
2823def test_table_group_by():
2824 def sorted_by_keys(d):
2825 # Ensure a guaranteed order of keys for aggregation results.
2826 if "keys2" in d:
2827 keys = tuple(zip(d["keys"], d["keys2"]))
2828 else:
2829 keys = d["keys"]
2830 sorted_keys = sorted(keys)
2831 sorted_d = {"keys": sorted(d["keys"])}
2832 for entry in d:
2833 if entry == "keys":
2834 continue
2835 values = dict(zip(keys, d[entry]))
2836 for k in sorted_keys:
2837 sorted_d.setdefault(entry, []).append(values[k])
2838 return sorted_d
2839
2840 table = pa.table([
2841 pa.array(["a", "a", "b", "b", "c"]),
2842 pa.array(["X", "X", "Y", "Z", "Z"]),
2843 pa.array([1, 2, 3, 4, 5]),
2844 pa.array([10, 20, 30, 40, 50])
2845 ], names=["keys", "keys2", "values", "bigvalues"])
2846
2847 r = table.group_by("keys").aggregate([
2848 ("values", "hash_sum")
2849 ])
2850 assert sorted_by_keys(r.to_pydict()) == {
2851 "keys": ["a", "b", "c"],
2852 "values_sum": [3, 7, 5]
2853 }
2854
2855 r = table.group_by("keys").aggregate([
2856 ("values", "hash_sum"),
2857 ("values", "hash_count")
2858 ])
2859 assert sorted_by_keys(r.to_pydict()) == {
2860 "keys": ["a", "b", "c"],
2861 "values_sum": [3, 7, 5],
2862 "values_count": [2, 2, 1]
2863 }
2864
2865 # Test without hash_ prefix
2866 r = table.group_by("keys").aggregate([
2867 ("values", "sum")
2868 ])
2869 assert sorted_by_keys(r.to_pydict()) == {
2870 "keys": ["a", "b", "c"],
2871 "values_sum": [3, 7, 5]
2872 }
2873
2874 r = table.group_by("keys").aggregate([
2875 ("values", "max"),
2876 ("bigvalues", "sum")
2877 ])
2878 assert sorted_by_keys(r.to_pydict()) == {
2879 "keys": ["a", "b", "c"],
2880 "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