BUG: fix convert_dtypes dropping values from sliced mixed-dtype DataFrames#64712
Open
moktamd wants to merge 2 commits intopandas-dev:mainfrom
Open
BUG: fix convert_dtypes dropping values from sliced mixed-dtype DataFrames#64712moktamd wants to merge 2 commits intopandas-dev:mainfrom
moktamd wants to merge 2 commits intopandas-dev:mainfrom
Conversation
…rames After slicing a DataFrame with mixed dtypes and calling convert_dtypes(), columns backed by ExtensionArrays (e.g. Arrow strings) could lose values because Block.convert_dtypes used `self.shape[0]` (the original pre-conversion block's row count) instead of `blk.shape[0]` (the post-conversion block's row count) when deciding whether to split. This caused an unnecessary _split() call on single-row ExtensionBlocks, which sliced the 1-D backing array instead of selecting a row from a 2-D array, silently truncating the data. Closes pandas-dev#64702
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
convert_dtypes#64702Summary
After slicing a mixed-dtype DataFrame and calling
convert_dtypes(), columns converted to ExtensionArray-backed types (e.g. Arrow strings) could silently lose values.The root cause is in
Block.convert_dtypes: afterself.convert()splits the original object block into type-specific blocks, the loop still referencesself.shape[0]andself.dtypeinstead ofblk.shape[0]andblk.dtype. When the original block had multiple rows (one per column in the consolidated object block), the converted single-row ExtensionBlock was unnecessarily passed through_split(), which sliced the 1-D backing array as if selecting a row from a 2-D array, truncating the data.Fix: use
blk.shape[0]andblk.dtypeto reference the post-conversion block rather than the pre-conversionself.Repro from the issue: