Skip to content
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

API: ignore empty range/object dtype in Index setop operations (string dtype compat) #60797

Conversation

jorisvandenbossche
Copy link
Member

@jorisvandenbossche jorisvandenbossche commented Jan 26, 2025

Exploring one option to address #60338

This makes that inserting/union etc with an empty RangeIndex or empty Index with object dtype (the types of index that get created for empty objects by default), ignores the dtype of that empty index.
This improves the workflow of creating an empty dataframe and adding columns or resetting the index (you can see the removed xfails). Also most of the test changes are changes that are essentially reverting previous edits (typically adding an explicit dtype=object) to get the tests passing for infer_string, and I think almost all those test changes show a better behaviour now.

I still want to add some explicit tests for the original use case (although everything is implicitly already covered in other tests, I think)

@jorisvandenbossche jorisvandenbossche added this to the 2.3 milestone Jan 26, 2025
@jorisvandenbossche jorisvandenbossche added Strings String extension data type and string data Index Related to the Index class or subclasses setops union, intersection, difference, symmetric_difference labels Jan 26, 2025
Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

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

lgtm - I think this is a pretty reasonable workaround for the time being

@rhshadrach
Copy link
Member

Agreed. While it's undesirable to me to ignore empty object dtype when using e.g. concat, using StringDtype should make having object dtype in the first place much more of an edge case. And the impact of this on e.g. adding columns to an empty DataFrame is worth the tradeoff.

@jorisvandenbossche jorisvandenbossche marked this pull request as ready for review January 29, 2025 20:02
Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

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

lgtm, needs a whatsnew, yea? I'd suggest a section at the top, I expect this to be somewhat commonly hit.

@jorisvandenbossche
Copy link
Member Author

Added a whatsnew note (for 2.3, didn't directly find a suitable existing section, but we will have to clean up that file anyway)

@jorisvandenbossche jorisvandenbossche merged commit ee06e71 into pandas-dev:main Feb 17, 2025
42 checks passed
@jorisvandenbossche jorisvandenbossche deleted the string-dtype-index-union-with-empty-object branch February 17, 2025 09:15
Copy link

lumberbot-app bot commented Feb 17, 2025

Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

  1. Checkout backport branch and update it.
git checkout 2.3.x
git pull
  1. Cherry pick the first parent branch of the this PR on top of the older branch:
git cherry-pick -x -m1 ee06e714fcb35e0b6d321b3edd454eb0e363e5e4
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
git commit -am 'Backport PR #60797: API: ignore empty range/object dtype in Index setop operations (string dtype compat)'
  1. Push to a named branch:
git push YOURFORK 2.3.x:auto-backport-of-pr-60797-on-2.3.x
  1. Create a PR against branch 2.3.x, I would have named this PR:

"Backport PR #60797 on branch 2.3.x (API: ignore empty range/object dtype in Index setop operations (string dtype compat))"

And apply the correct labels and milestones.

Congratulations — you did some good work! Hopefully your backport PR will be tested by the continuous integration and merged soon!

Remember to remove the Still Needs Manual Backport label once the PR gets merged.

If these instructions are inaccurate, feel free to suggest an improvement.

@jorisvandenbossche
Copy link
Member Author

Manual backport -> #60948

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Index Related to the Index class or subclasses setops union, intersection, difference, symmetric_difference Strings String extension data type and string data
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants