fix(dc): Fix pivot column name tuple issue for single value column
Problem:
- When selecting only 1 value column in Pivot (long-to-wide), column names
were incorrectly formatted as 'FMA___('FMA', 'baseline')' instead of 'FMA___baseline'
- This occurred because pandas pivot_table() sometimes returns tuple column names
even for single value columns, but the code didn't handle this case
Root Cause:
- Line 112-127 in pivot.py assumed single value columns always have simple
string column names, without checking for tuples
- Multi-value column logic (line 129-145) correctly handled tuples with isinstance()
Solution:
- Add isinstance(col, tuple) check in single value column mode
- Extract pivot_value from tuple[1] if column name is a tuple
- Maintain backward compatibility for non-tuple column names
Testing:
- Single value column: FMA___ + tuple -> 'FMA___baseline' (fixed)
- Multiple value columns: Already working correctly (no change)
Impact: Fixes historical bug in Tool C Pivot feature
This commit is contained in:
@@ -110,7 +110,7 @@ def pivot_long_to_wide(
|
||||
|
||||
# ✨ 修复:更健壮的列名展平逻辑
|
||||
if len(value_columns) == 1:
|
||||
# 单个值列:列名是单层的 (pivot_value1, pivot_value2, ...)
|
||||
# 单个值列:列名可能是单层的,也可能是元组
|
||||
print(f'📝 单值列模式:展平列名', flush=True)
|
||||
|
||||
# 获取原始值列名(用于生成新列名)
|
||||
@@ -119,8 +119,15 @@ def pivot_long_to_wide(
|
||||
# 生成新列名:值列名___透视值
|
||||
new_columns = []
|
||||
for col in df_pivot.columns:
|
||||
# col 是透视列的某个值(如 0, 1, 2)
|
||||
new_col_name = f'{value_col_name}___{col}'
|
||||
# ✅ 修复:检查col是否是元组(pandas在某些情况下会返回元组)
|
||||
if isinstance(col, tuple):
|
||||
# 如果是元组 (值列名, 透视值),取第二个元素
|
||||
pivot_value = col[1]
|
||||
new_col_name = f'{value_col_name}___{pivot_value}'
|
||||
else:
|
||||
# 如果不是元组,直接使用(兼容旧行为)
|
||||
new_col_name = f'{value_col_name}___{col}'
|
||||
|
||||
new_columns.append(new_col_name)
|
||||
print(f' 生成列: {new_col_name}', flush=True)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user