-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
BUG: fix block fragmentation in DataFrame.astype with dict dtype #63461
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
base: main
Are you sure you want to change the base?
Conversation
…o FIX_astype_block
|
I've implemented a fix that consolidates blocks only when block count explodes (currently, when blocks == columns). I'm unsure if this threshold is optimal. It feels somewhat subjective. Could any maintainer provide guidance on a better criterion please? Thanks! |
rhshadrach
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
| warnings.warn( | ||
| f"astype block consolidation failed: {type(e).__name__}", | ||
| UserWarning, | ||
| stacklevel=2, | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In what case can this fail?
| total_cols = len(self.columns) | ||
| # only when the number of blocks explode do this | ||
| if current_blocks == total_cols and total_cols > 5: | ||
| mgr._consolidate_inplace() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still creating a very fragmented DataFrame and then performing a copy. We would prefer not fragmenting the DataFrame at all in the first place (I think this should be possible).
This PR fixes the issue by actively consolidating blocks of the same dtype after a dictionary-based astype operation. The fix is minimal (the alternative code change I thought is to determine and perform the correct partitioning behavior during the initial transformation. )and I think it's safe.
It adds a call to
_consolidate_inplace()on the result's BlockManager when dtype is a dict.The consolidation is wrapped in a try-except block with a warning to ensure it never breaks the core functionality of astype. Failures are silent and backward compatible.
I tried the Reproducible Example and it worked well. If there is any problem, I'm happy to fix it.