Skip to content

Commit

Permalink
Better error for invalid column names in index
Browse files Browse the repository at this point in the history
  • Loading branch information
AKuederle committed Jun 16, 2024
1 parent 21ef6cc commit f695ec0
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions tpcp/_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,16 @@ def _create_check_index(self):
"explicitly using `sort_values`."
)

invalid_elements = [s for s in index_1.columns if not s.isidentifier() or iskeyword(s)]
invalid_elements = [
s for s in index_1.columns if not isinstance(s, str) or not s.isidentifier() or iskeyword(s)
]
if invalid_elements:
warnings.warn(
f"Some of your index columns are not valid Python attribute names: {invalid_elements}. "
"This will cause issues when using further methods such as `get_subset`, `group_label`, "
"`group_labels`, and `datapoint_label`.\n"
"To be a valid Python attribute a string should only consist of alphanumeric letters and underscores. "
"To be a valid Python attribute, you index column labels need to be a string that only consist of "
"alphanumeric letters and underscores. "
"Strings starting with a number, containing spaces or special characters are not allowed."
"Furthermore, they must not shadow a built-in Python keyword.",
RuntimeWarning,
Expand Down Expand Up @@ -137,7 +140,6 @@ def group_label(self) -> tuple[str, ...]:
"""Get the current group label.
The group is defined by the current groupby settings.
If the dataset is not grouped, this is equivalent to `datapoint_label`.
Note, this attribute can only be used, if there is just a single group.
This will return a named tuple. The tuple will contain only one entry if there is only a single groupby column
Expand Down

0 comments on commit f695ec0

Please sign in to comment.