Repository navigation
[tune] Fix get_dataframe crashing on an all-NaN metric with pandas 3 - #67291
Conversation
With pandas 3, idxmax/idxmin raise instead of returning NaN when every value is NaN, so ResultGrid.get_dataframe(filter_metric=..., filter_mode=...) failed whenever one trial, such as a diverged run, only reported NaN for the metric. I skip such trials with the existing warning before calling idxmax/idxmin, which is what the TypeError fallback from ray-project#9381 did on pandas 2, and add a regression test. Assisted-by: Claude Assisted-by: GPT-6 Astra Signed-off-by: mottopanikeiku <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request handles cases where a trial diverged and only reported NaN values by checking if all metric values are NaN before calling idxmax() or idxmin(), preventing errors in pandas>=3. It also adds a test to verify this behavior. Feedback suggests retaining the removed try-except block around df.iloc[idx] to guard against potential TypeError exceptions when the DataFrame has non-integer index labels.
| logger.warning( | ||
| "Warning: Non-numerical value(s) encountered for {}".format(path) | ||
| ) | ||
| rows[path] = df.iloc[idx].to_dict() |
There was a problem hiding this comment.
Removing the try-except block around df.iloc[idx] reduces the robustness of the code. While idx is expected to be an integer under standard RangeIndex usage, if the DataFrame has non-integer index labels (e.g., strings or timestamps), idxmax() or idxmin() will return a non-integer label. Calling df.iloc[idx] with a non-integer will then raise a TypeError. Keeping the try-except block acts as a defensive guard against such unexpected index types or other edge cases.
try:
rows[path] = df.iloc[idx].to_dict()
except TypeError:
logger.warning(
"Warning: Non-numerical value(s) encountered for {}".format(path)
)There was a problem hiding this comment.
the trial dataframes come from pd.json_normalize / pd.read_csv in _fetch_trial_dataframe, so they always have a RangeIndex and idx is a position here. the except TypeError only ever caught a NaN idx (its comment said so), and that case is now handled before idxmax/idxmin. keeping it would turn a real indexing bug into a misleading "non-numerical value" warning, so i left it out.
pseudo-rnd-thoughts
left a comment
There was a problem hiding this comment.
Thanks for the PR, LGTM
|
@pseudo-rnd-thoughts mark you maintain so many repos i contribute to i always get confused seeing your comments |
Description
With pandas 3,
Series.idxmax()/idxmin()raiseValueError: Encountered all NA valueswhen every value is NaN (pandas 2 returned NaN with a FutureWarning).ExperimentAnalysis._retrieve_rowsrelied on the old behavior: the NaN index madedf.iloc[idx]raiseTypeError, which was caught to skip the trial with a warning (#9381). On pandas 3 theValueErrorescapes, soResultGrid.get_dataframe(filter_metric=..., filter_mode=...)andExperimentAnalysis.dataframe(metric, mode)fail as soon as one trial only reported NaN for the metric, for example a run that diverged in its first epoch. Ray allowspandas >= 2.2.3, and a fresh install ofray[tune]currently gets pandas 3.1.0.This PR checks for an all-NaN metric before calling
idxmax/idxminand skips that trial with the same warning as before. TheTypeErrorfallback only handled that case, so it is removed.Observed in a small learning-rate sweep (lr in {0.01, 0.1, 1000} on the scikit-learn diabetes data; lr=1000 reports NaN every epoch), calling
get_dataframe(filter_metric="val_mse", filter_mode="min"):ValueError: Encountered all NA valuesI found this by reading and testing the code.
Related issues
Related to #9364 and #9381 (the original NaN handling, which this restores on pandas 3).
Additional information
Not a duplicate: I searched issues and PRs for "Encountered all NA values",
_retrieve_rows,idxmin/idxmaxand NaN withget_dataframe, and checked the files changed by the open Tune PRs; none touch this code path.Tests (test files from this branch, run against the Ray 2.59.0 wheel with this branch's
experiment_analysis.pyloaded on top; that file did not change between 2.59.0 and master):python -m pytest -q test_result_grid.py1 failed, 8 passed(test_result_grid_df_all_nan_metric - ValueError: Encountered all NA values)9 passedpython -m pytest -q test_experiment_analysis.pyafter the fix, pandas 3.1.0:33 passedI made this PR and directed its development, with AI assistance from Claude and GPT-6 Astra.