Skip to content

Ensure uniqueness of column names to avoid losing data during serialization #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

svlandeg
Copy link
Contributor

@svlandeg svlandeg commented Apr 3, 2025

When a table is parsed with duplicate column names, these will both be preserved in the pandas DataFrame format. However, when converting this to a dictionary in encode_df, pandas will raise a warning:

\spacy_layout\util.py:36: UserWarning: DataFrame columns are not unique, some columns will be omitted.

And the "duplicate" columns will be removed, even if the values in the column are different than the other column with the same heading.

This may be somewhat surprising for users, so this PR instead ensures that columns are unique by appending (2), (3) etc to non-unique column names, effectively making them unique, before serializing them to the dictionary.

Open questions:

  • Instead of doing the conversion automatically, should this functionality be behind a feature flag? If so - what should be the default?
  • When doing the conversion automatically, should spacy-layout issue a warning about the renaming of the columns?

doc_bin = DocBin(docs=[old_doc], store_user_data=True)
new_doc = list(doc_bin.get_docs(nlp.vocab))[0]
new_table = new_doc._.tables[0]._.data
assert list(new_table.columns) == ['Index', 'Value', 'Value (2)', 'Index (2)', 'Value (3)', 'Value (4)']
Copy link
Contributor Author

@svlandeg svlandeg Apr 3, 2025

Choose a reason for hiding this comment

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

on main, the result would be

['Index', 'Value']

i.e. 4 columns would have just been removed from the serialized output.

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.

1 participant