Skip to content

Commit

Permalink
fix: preserve old naming convention for multi-value pivot (this will …
Browse files Browse the repository at this point in the history
…change in 1.0 to no longer redundantly have the column name in the middle) (#14120)
  • Loading branch information
MarcoGorelli committed Jan 31, 2024
1 parent 35ffc05 commit ed672fa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
3 changes: 2 additions & 1 deletion crates/polars-ops/src/frame/pivot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ fn pivot_impl_single_column(
let headers = column_agg.unique_stable()?.cast(&DataType::String)?;
let mut headers = headers.str().unwrap().clone();
if values.len() > 1 {
headers = headers.apply_values(|v| Cow::from(format!("{value_col_name}{sep}{v}")))
// TODO! MILESTONE 1.0: change to `format!("{value_col_name}{sep}{v}")`
headers = headers.apply_values(|v| Cow::from(format!("{value_col_name}{sep}{column}{sep}{v}")))
}

let n_cols = headers.len();
Expand Down
16 changes: 8 additions & 8 deletions py-polars/tests/unit/operations/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ def test_pivot_multiple_values_column_names_5116() -> None:
)
expected = {
"c1": ["A", "B"],
"x1|C": [1, 2],
"x1|D": [3, 4],
"x2|C": [8, 7],
"x2|D": [6, 5],
"x1|c2|C": [1, 2],
"x1|c2|D": [3, 4],
"x2|c2|C": [8, 7],
"x2|c2|D": [6, 5],
}
assert result.to_dict(as_series=False) == expected

Expand All @@ -188,10 +188,10 @@ def test_pivot_duplicate_names_7731() -> None:
).to_dict(as_series=False)
expected = {
"b": [1.5, 2.5],
'a_{"x","x"}': [1, None],
'a_{"x","y"}': [None, 4],
'd_{"x","x"}': [7, None],
'd_{"x","y"}': [None, 8],
'a_{"c","e"}_{"x","x"}': [1, None],
'a_{"c","e"}_{"x","y"}': [None, 4],
'd_{"c","e"}_{"x","x"}': [7, None],
'd_{"c","e"}_{"x","y"}': [None, 8],
}
assert result == expected

Expand Down

0 comments on commit ed672fa

Please sign in to comment.