Skip to content

Commit

Permalink
Better error msg on unknown columns on converting RunList to Experiment
Browse files Browse the repository at this point in the history
Summary_columns often have many mistakes or missing keys, so we let the
error message more informative and helpful by showing close matches.
  • Loading branch information
wookayin committed Nov 7, 2023
1 parent 18a031e commit 94ae5a0
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions expt/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,9 +925,22 @@ def from_runs(
)

if summary_columns:
df = df[['hypothesis', *summary_columns]]

return cls.from_dataframe(df, name=name)
try:
df = df[['hypothesis', *summary_columns]]
except KeyError:
# summary_columns often have many mistakes or missing keys,
# so let the error message more informative.
missing_cols = [col for col in summary_columns if col not in df.keys()]
suggestions = [
difflib.get_close_matches(col, df.keys()) for col in missing_cols
]
linesep = '\n'
raise KeyError(
f"Some columns do not exist in the dataframe: {missing_cols}. "
f"Close matches = {linesep.join(str(s) for s in suggestions)}"
) from None

return cls.from_dataframe(cast(pd.DataFrame, df), name=name)

@classmethod
def from_dataframe(
Expand Down

0 comments on commit 94ae5a0

Please sign in to comment.