Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: preserve old naming convention for multi-value pivot (this will change in 1.0 to no longer redundantly have the column name in the middle) #14120

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading