Skip to content

fix: keep DataFrame column names that are str subclasses - #1441

Open
Abtektas wants to merge 2 commits into
astroautomata:masterfrom
Abtektas:fix/str-subclass-column-names
Open

Abtektas wants to merge 2 commits into
astroautomata:masterfrom
Abtektas:fix/str-subclass-column-names

Conversation

@Abtektas

Copy link
Copy Markdown

Summary

Fixes #887

scikit-learn only keeps feature names when every column name is exactly of type str, so DataFrame columns named by StrEnum members (or any other str subclass) lost their names: equations used x0, x1, and predict on the same DataFrame raised Input X contains NaN, because it reindexes the columns by feature_names_in_. variable_names gave no way around it, since it is reset when X is a DataFrame.

fit now converts such column names to plain strings, next to the existing handling of spaces. The caller's DataFrame is not changed.

Tests

test_str_subclass_dataframe_columns fits and predicts with column names from a str mixin Enum (StrEnum needs Python 3.11, and CI also runs 3.10; scikit-learn ignores both the same way). It fails without the change.

Assisted-by: Claude:claude-opus-5-5

scikit-learn only keeps feature names when every column name is exactly
of type str, so StrEnum (and other str subclass) column names were
dropped: the search used x0, x1, and predict on the same DataFrame
raised "Input X contains NaN", because it reindexes the columns by
x0, x1. Convert such column names to plain strings in fit.

Fixes astroautomata#887

Assisted-by: Claude:claude-opus-5-5
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-19T22:58:26.257867Z 0916a98 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0916a9877a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread pysr/sr.py Outdated
if any(isinstance(c, str) and type(c) is not str for c in X.columns):
X = X.copy(deep=False)
X.columns = [
str.__str__(c) if isinstance(c, str) else c for c in X.columns

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve support for mixed non-string column labels

When a DataFrame combines a str subclass with any non-string label (for example [Cols.A, 0]) on scikit-learn 1.2+, this conversion turns only Cols.A into an exact str. Scikit-learn then sees mixed str/int feature-name types and rejects fit, whereas the parent code saw no exact str type and accepted the data by falling back to generated feature names. Restrict this normalization to columns that are all string-like, or normalize non-string labels consistently, so existing mixed-label training does not fail.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch, thanks. With columns like [Cols.A, 0], converting only Cols.A gave scikit-learn a mix of str and int names, and fit raised a TypeError. On master the same frame is accepted and the names are ignored (x0, x1). a9d1218 converts only when every column name is a string, so mixed names keep the current behaviour, and it adds a test for that case.

Converting the str subclass names next to a non-string name, as in
[Cols.A, 0], gave scikit-learn a mix of str and int names, and it
raised a TypeError. Such a DataFrame was accepted before, with the
names ignored and x0, x1 used instead. Convert only when every column
name is a string, and test the mixed case.

Assisted-by: Claude:claude-opus-5-5

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: variable_names ignored at least with StrEnums

1 participant