Skip to content

fix: preserve metric_names order in trials_dataframe columns - #6786

Merged
sawa3030 merged 1 commit into
optuna:masterfrom
rkfshakti:fix/trials-dataframe-metric-order-6785
Aug 5, 2026
Merged

sawa3030 merged 1 commit into
optuna:masterfrom
rkfshakti:fix/trials-dataframe-metric-order-6785

Conversation

@rkfshakti

Copy link
Copy Markdown
Contributor

Problem

Study.trials_dataframe() sorted value columns alphabetically instead of preserving the order set by Study.set_metric_names(). For metric names like [train, test], the columns came out as [values_test, values_train] instead of [values_train, values_test].

Root cause

_create_records_and_aggregate_column() in optuna/study/_dataframe.py used sorted() on all column sets including the values attribute. When metric_names is set, the column tuples are (df_column, metric_name), and sorted() orders by metric_name alphabetically — not by the user-defined order.

Fix

When metric_names is set, the values columns are emitted in metric_names order instead of being sorted. All other column sets (params, attrs, etc.) and the metric_names is None case (integer indices) still use sorted() as before.

Verification

study = optuna.create_study(directions=[minimize, minimize])
study.set_metric_names([train, test])
study.optimize(objective, n_trials=3)
list(study.trials_dataframe().columns)
# Before: [..., values_test, values_train, ...]
# After:  [..., values_train, values_test, ...]

Also tested with reversed order ([test, train] → [values_test, values_train]) and without metric names (unchanged: [values_0, values_1]).

Fixes #6785

trials_dataframe() sorted value columns alphabetically instead of
preserving the order set by set_metric_names(). For metric names like
['train', 'test'], the columns came out as ['values_test', 'values_train']
instead of ['values_train', 'values_test'].

The root cause was that _create_records_and_aggregate_column() used
sorted() on all column sets including the 'values' attribute. When
metric_names is set, the column tuples are (df_column, metric_name), and
sorted() orders by metric_name alphabetically.

The fix preserves metric_names order for the 'values' attribute when
metric_names is set, and falls back to sorted() for all other cases
(including when metric_names is None, where columns are integer indices).

Fixes #6785
@not522

not522 commented Aug 4, 2026

Copy link
Copy Markdown
Member

@sawa3030 Could you review this PR?

@not522 not522 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@not522 not522 removed their assignment Aug 5, 2026
@not522 not522 added the bug Issue/PR about behavior that is broken. Not for typos/examples/CI/test but for Optuna itself. label Aug 5, 2026

@sawa3030 sawa3030 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@sawa3030
sawa3030 merged commit 245d439 into optuna:master Aug 5, 2026
14 checks passed
@sawa3030 sawa3030 added this to the v5.0.0 milestone Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issue/PR about behavior that is broken. Not for typos/examples/CI/test but for Optuna itself.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

optuna.Study.trials_dataframe doesn't save ordering from metric names with optuna.Study.set_metric_names

3 participants