Skip to content

Commit f34ec91

Browse files
authored
fix: pyspark unpivot with index None (#2090)
1 parent 8fbac37 commit f34ec91

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

narwhals/_spark_like/dataframe.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -420,11 +420,16 @@ def unpivot(
420420
variable_name: str,
421421
value_name: str,
422422
) -> Self:
423-
return self._from_native_frame(
424-
self._native_frame.unpivot(
425-
ids=index,
426-
values=on,
427-
variableColumnName=variable_name,
428-
valueColumnName=value_name,
429-
)
423+
ids = tuple(self.columns) if index is None else tuple(index)
424+
values = (
425+
tuple(set(self.columns).difference(set(ids))) if on is None else tuple(on)
426+
)
427+
unpivoted_native_frame = self._native_frame.unpivot(
428+
ids=ids,
429+
values=values,
430+
variableColumnName=variable_name,
431+
valueColumnName=value_name,
430432
)
433+
if index is None:
434+
unpivoted_native_frame = unpivoted_native_frame.drop(*ids)
435+
return self._from_native_frame(unpivoted_native_frame)

tests/frame/unpivot_test.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,58 @@
2020
"c": [2, 4, 6],
2121
}
2222

23-
expected_b_only = {
23+
expected_on_b_idx_a = {
2424
"a": ["x", "y", "z"],
2525
"variable": ["b", "b", "b"],
2626
"value": [1, 3, 5],
2727
}
2828

29-
expected_b_c = {
29+
expected_on_b_c_idx_a = {
3030
"a": ["x", "y", "z", "x", "y", "z"],
3131
"variable": ["b", "b", "b", "c", "c", "c"],
3232
"value": [1, 3, 5, 2, 4, 6],
3333
}
3434

35+
expected_on_none_idx_a = {
36+
"a": ["x", "y", "z", "x", "y", "z"],
37+
"variable": ["b", "b", "b", "c", "c", "c"],
38+
"value": [1, 3, 5, 2, 4, 6],
39+
}
40+
41+
expected_on_b_c_idx_none = {
42+
"variable": ["b", "b", "b", "c", "c", "c"],
43+
"value": [1, 3, 5, 2, 4, 6],
44+
}
45+
46+
expected_on_none_idx_none = {
47+
"variable": ["a", "a", "a", "b", "b", "b", "c", "c", "c"],
48+
"value": ["x", "y", "z", "1", "3", "5", "2", "4", "6"],
49+
}
50+
3551

3652
@pytest.mark.parametrize(
37-
("on", "expected"),
38-
[("b", expected_b_only), (["b", "c"], expected_b_c), (None, expected_b_c)],
53+
("on", "index", "expected"),
54+
[
55+
("b", ["a"], expected_on_b_idx_a),
56+
(["b", "c"], ["a"], expected_on_b_c_idx_a),
57+
(None, ["a"], expected_on_none_idx_a),
58+
(["b", "c"], None, expected_on_b_c_idx_none),
59+
(None, None, expected_on_none_idx_none),
60+
],
3961
)
40-
def test_unpivot_on(
62+
def test_unpivot(
4163
constructor: Constructor,
4264
on: str | list[str] | None,
65+
index: list[str] | None,
4366
expected: dict[str, list[float]],
67+
request: pytest.FixtureRequest,
4468
) -> None:
69+
if on is None and index is None and "polars" not in str(constructor):
70+
# TODO(2082): add support in other backends
71+
request.applymarker(pytest.mark.xfail)
4572
df = nw.from_native(constructor(data))
46-
result = df.unpivot(on=on, index=["a"]).sort("variable", "a")
73+
sort_columns = ["variable"] if index is None else ["variable", "a"]
74+
result = df.unpivot(on=on, index=index).sort(by=sort_columns)
4775
assert_equal_data(result, expected)
4876

4977

0 commit comments

Comments
 (0)