Repository navigation
fix: preserve metric_names order in trials_dataframe columns - #6786
Merged
sawa3030 merged 1 commit intoAug 5, 2026
Merged
Conversation
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
Member
|
@sawa3030 Could you review this PR? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Study.trials_dataframe()sorted value columns alphabetically instead of preserving the order set byStudy.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()inoptuna/study/_dataframe.pyusedsorted()on all column sets including thevaluesattribute. Whenmetric_namesis set, the column tuples are(df_column, metric_name), andsorted()orders bymetric_namealphabetically — not by the user-defined order.Fix
When
metric_namesis set, thevaluescolumns are emitted inmetric_namesorder instead of being sorted. All other column sets (params, attrs, etc.) and themetric_names is Nonecase (integer indices) still usesorted()as before.Verification
Also tested with reversed order (
[test, train]→[values_test, values_train]) and without metric names (unchanged:[values_0, values_1]).Fixes #6785