|
| 1 | +from rayforce import I64, Symbol, Table, Vector |
| 2 | + |
| 3 | + |
| 4 | +def _get_pivot_values(result, index_col: str) -> dict: |
| 5 | + index_values = [v.to_python() for v in result[index_col]] |
| 6 | + columns = [str(c) for c in result.columns() if str(c) != index_col] |
| 7 | + result_dict = {} |
| 8 | + for i, idx_val in enumerate(index_values): |
| 9 | + result_dict[idx_val] = {col: result[col][i].to_python() for col in columns} |
| 10 | + return result_dict |
| 11 | + |
| 12 | + |
| 13 | +def test_pivot_simple(): |
| 14 | + table = Table( |
| 15 | + { |
| 16 | + "symbol": Vector(items=["AAPL", "AAPL", "GOOG", "GOOG"], ray_type=Symbol), |
| 17 | + "metric": Vector(items=["price", "volume", "price", "volume"], ray_type=Symbol), |
| 18 | + "value": Vector(items=[150, 1000, 2800, 500], ray_type=I64), |
| 19 | + } |
| 20 | + ) |
| 21 | + |
| 22 | + result = table.pivot(index="symbol", columns="metric", values="value") |
| 23 | + |
| 24 | + columns = [str(c) for c in result.columns()] |
| 25 | + assert "symbol" in columns |
| 26 | + assert "price" in columns |
| 27 | + assert "volume" in columns |
| 28 | + assert len(result) == 2 |
| 29 | + |
| 30 | + result_dict = _get_pivot_values(result, "symbol") |
| 31 | + assert result_dict["AAPL"]["price"] == 150 |
| 32 | + assert result_dict["AAPL"]["volume"] == 1000 |
| 33 | + assert result_dict["GOOG"]["price"] == 2800 |
| 34 | + assert result_dict["GOOG"]["volume"] == 500 |
| 35 | + |
| 36 | + |
| 37 | +def test_pivot_with_multiple_index_columns(): |
| 38 | + table = Table( |
| 39 | + { |
| 40 | + "date": Vector( |
| 41 | + items=["2024-01-01", "2024-01-01", "2024-01-02", "2024-01-02"], ray_type=Symbol |
| 42 | + ), |
| 43 | + "symbol": Vector(items=["AAPL", "AAPL", "AAPL", "AAPL"], ray_type=Symbol), |
| 44 | + "metric": Vector(items=["open", "close", "open", "close"], ray_type=Symbol), |
| 45 | + "value": Vector(items=[150, 152, 153, 155], ray_type=I64), |
| 46 | + } |
| 47 | + ) |
| 48 | + |
| 49 | + result = table.pivot(index=["date", "symbol"], columns="metric", values="value") |
| 50 | + |
| 51 | + columns = [str(c) for c in result.columns()] |
| 52 | + assert "date" in columns |
| 53 | + assert "symbol" in columns |
| 54 | + assert "open" in columns |
| 55 | + assert "close" in columns |
| 56 | + assert len(result) == 2 |
| 57 | + |
| 58 | + result_dict = _get_pivot_values(result, "date") |
| 59 | + assert result_dict["2024-01-01"]["open"] == 150 |
| 60 | + assert result_dict["2024-01-01"]["close"] == 152 |
| 61 | + assert result_dict["2024-01-02"]["open"] == 153 |
| 62 | + assert result_dict["2024-01-02"]["close"] == 155 |
| 63 | + |
| 64 | + |
| 65 | +def test_pivot_with_sum_aggfunc(): |
| 66 | + table = Table( |
| 67 | + { |
| 68 | + "category": Vector(items=["A", "A", "A", "B", "B"], ray_type=Symbol), |
| 69 | + "type": Vector(items=["x", "x", "y", "x", "y"], ray_type=Symbol), |
| 70 | + "value": Vector(items=[10, 20, 30, 40, 50], ray_type=I64), |
| 71 | + } |
| 72 | + ) |
| 73 | + |
| 74 | + result = table.pivot(index="category", columns="type", values="value", aggfunc="sum") |
| 75 | + |
| 76 | + columns = [str(c) for c in result.columns()] |
| 77 | + assert "x" in columns |
| 78 | + assert "y" in columns |
| 79 | + assert len(result) == 2 |
| 80 | + |
| 81 | + # A has x: 10+20=30, y: 30; B has x: 40, y: 50 |
| 82 | + result_dict = _get_pivot_values(result, "category") |
| 83 | + assert result_dict["A"]["x"] == 30 |
| 84 | + assert result_dict["A"]["y"] == 30 |
| 85 | + assert result_dict["B"]["x"] == 40 |
| 86 | + assert result_dict["B"]["y"] == 50 |
| 87 | + |
| 88 | + |
| 89 | +def test_pivot_with_count_aggfunc(): |
| 90 | + table = Table( |
| 91 | + { |
| 92 | + "category": Vector(items=["A", "A", "A", "B", "B"], ray_type=Symbol), |
| 93 | + "type": Vector(items=["x", "x", "y", "x", "y"], ray_type=Symbol), |
| 94 | + "value": Vector(items=[10, 20, 30, 40, 50], ray_type=I64), |
| 95 | + } |
| 96 | + ) |
| 97 | + |
| 98 | + result = table.pivot(index="category", columns="type", values="value", aggfunc="count") |
| 99 | + |
| 100 | + assert len(result) == 2 |
| 101 | + |
| 102 | + # A has x: 2, y: 1; B has x: 1, y: 1 |
| 103 | + result_dict = _get_pivot_values(result, "category") |
| 104 | + assert result_dict["A"]["x"] == 2 |
| 105 | + assert result_dict["A"]["y"] == 1 |
| 106 | + assert result_dict["B"]["x"] == 1 |
| 107 | + assert result_dict["B"]["y"] == 1 |
| 108 | + |
| 109 | + |
| 110 | +def test_pivot_with_avg_aggfunc(): |
| 111 | + table = Table( |
| 112 | + { |
| 113 | + "category": Vector(items=["A", "A", "B"], ray_type=Symbol), |
| 114 | + "metric": Vector(items=["x", "x", "x"], ray_type=Symbol), |
| 115 | + "value": Vector(items=[10, 20, 30], ray_type=I64), |
| 116 | + } |
| 117 | + ) |
| 118 | + |
| 119 | + result = table.pivot(index="category", columns="metric", values="value", aggfunc="avg") |
| 120 | + |
| 121 | + assert len(result) == 2 |
| 122 | + |
| 123 | + # A has x: (10+20)/2=15, B has x: 30 |
| 124 | + result_dict = _get_pivot_values(result, "category") |
| 125 | + assert result_dict["A"]["x"] == 15 |
| 126 | + assert result_dict["B"]["x"] == 30 |
| 127 | + |
| 128 | + |
| 129 | +def test_pivot_with_min_aggfunc(): |
| 130 | + table = Table( |
| 131 | + { |
| 132 | + "category": Vector(items=["A", "A", "A", "B", "B"], ray_type=Symbol), |
| 133 | + "type": Vector(items=["x", "x", "y", "x", "y"], ray_type=Symbol), |
| 134 | + "value": Vector(items=[10, 20, 30, 40, 50], ray_type=I64), |
| 135 | + } |
| 136 | + ) |
| 137 | + |
| 138 | + result = table.pivot(index="category", columns="type", values="value", aggfunc="min") |
| 139 | + |
| 140 | + # A has x: min(10,20)=10, y: 30; B has x: 40, y: 50 |
| 141 | + result_dict = _get_pivot_values(result, "category") |
| 142 | + assert result_dict["A"]["x"] == 10 |
| 143 | + assert result_dict["A"]["y"] == 30 |
| 144 | + assert result_dict["B"]["x"] == 40 |
| 145 | + assert result_dict["B"]["y"] == 50 |
| 146 | + |
| 147 | + |
| 148 | +def test_pivot_with_max_aggfunc(): |
| 149 | + table = Table( |
| 150 | + { |
| 151 | + "category": Vector(items=["A", "A", "A", "B", "B"], ray_type=Symbol), |
| 152 | + "type": Vector(items=["x", "x", "y", "x", "y"], ray_type=Symbol), |
| 153 | + "value": Vector(items=[10, 20, 30, 40, 50], ray_type=I64), |
| 154 | + } |
| 155 | + ) |
| 156 | + |
| 157 | + result = table.pivot(index="category", columns="type", values="value", aggfunc="max") |
| 158 | + |
| 159 | + # A has x: max(10,20)=20, y: 30; B has x: 40, y: 50 |
| 160 | + result_dict = _get_pivot_values(result, "category") |
| 161 | + assert result_dict["A"]["x"] == 20 |
| 162 | + assert result_dict["A"]["y"] == 30 |
| 163 | + assert result_dict["B"]["x"] == 40 |
| 164 | + assert result_dict["B"]["y"] == 50 |
| 165 | + |
| 166 | + |
| 167 | +def test_pivot_single_value_per_cell(): |
| 168 | + table = Table( |
| 169 | + { |
| 170 | + "row": Vector(items=["r1", "r1", "r2", "r2"], ray_type=Symbol), |
| 171 | + "col": Vector(items=["c1", "c2", "c1", "c2"], ray_type=Symbol), |
| 172 | + "val": Vector(items=[1, 2, 3, 4], ray_type=I64), |
| 173 | + } |
| 174 | + ) |
| 175 | + |
| 176 | + result = table.pivot(index="row", columns="col", values="val") |
| 177 | + |
| 178 | + assert len(result) == 2 |
| 179 | + columns = [str(c) for c in result.columns()] |
| 180 | + assert "c1" in columns |
| 181 | + assert "c2" in columns |
| 182 | + |
| 183 | + result_dict = _get_pivot_values(result, "row") |
| 184 | + assert result_dict["r1"]["c1"] == 1 |
| 185 | + assert result_dict["r1"]["c2"] == 2 |
| 186 | + assert result_dict["r2"]["c1"] == 3 |
| 187 | + assert result_dict["r2"]["c2"] == 4 |
| 188 | + |
| 189 | + |
| 190 | +def test_pivot_preserves_order(): |
| 191 | + table = Table( |
| 192 | + { |
| 193 | + "id": Vector(items=["a", "a", "a"], ray_type=Symbol), |
| 194 | + "key": Vector(items=["third", "first", "second"], ray_type=Symbol), |
| 195 | + "value": Vector(items=[3, 1, 2], ray_type=I64), |
| 196 | + } |
| 197 | + ) |
| 198 | + |
| 199 | + result = table.pivot(index="id", columns="key", values="value", aggfunc="min") |
| 200 | + |
| 201 | + # third, first, second |
| 202 | + columns = [str(c) for c in result.columns()] |
| 203 | + assert "third" in columns |
| 204 | + assert "first" in columns |
| 205 | + assert "second" in columns |
| 206 | + |
| 207 | + result_dict = _get_pivot_values(result, "id") |
| 208 | + assert result_dict["a"]["third"] == 3 |
| 209 | + assert result_dict["a"]["first"] == 1 |
| 210 | + assert result_dict["a"]["second"] == 2 |
| 211 | + |
| 212 | + |
| 213 | +def test_pivot_followed_by_select(): |
| 214 | + table = Table( |
| 215 | + { |
| 216 | + "symbol": Vector(items=["AAPL", "AAPL", "GOOG", "GOOG"], ray_type=Symbol), |
| 217 | + "metric": Vector(items=["price", "volume", "price", "volume"], ray_type=Symbol), |
| 218 | + "value": Vector(items=[150, 1000, 2800, 500], ray_type=I64), |
| 219 | + } |
| 220 | + ) |
| 221 | + |
| 222 | + result = ( |
| 223 | + table.pivot(index="symbol", columns="metric", values="value") |
| 224 | + .select("symbol", "price") |
| 225 | + .execute() |
| 226 | + ) |
| 227 | + columns = [str(c) for c in result.columns()] |
| 228 | + assert "symbol" in columns |
| 229 | + assert "price" in columns |
0 commit comments