Skip to content

BUG: Fix to_sql passing incorrect index column name#65290

Open
tianlangqin wants to merge 1 commit intopandas-dev:mainfrom
hbd9577:BUG-to-sql-index-name
Open

BUG: Fix to_sql passing incorrect index column name#65290
tianlangqin wants to merge 1 commit intopandas-dev:mainfrom
hbd9577:BUG-to-sql-index-name

Conversation

@tianlangqin
Copy link
Copy Markdown
Contributor

When DataFrame.to_sql is called with index=True and a callable method on a DataFrame with MultiIndex columns, the keys list passed to the callable contained tuple-stringified index names (e.g., "('index', '')") instead of the actual SQL column name ("index").

This is because insert_data() called reset_index() on the temporary frame, which pads scalar index names to tuples to match the MultiIndex column levels. Then list(map(str, temp.columns)) converted those tuples to strings, meaning there is a mismatch with the SQL column names.

I fixed this by explicitly constructing the column_names list directly from self.index and the original self.frame.columns. This bypasses the tuple padding from reset_index, which was causing the bug.

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: to_sql does gives incorrect column name for index when callable passed in to method

1 participant