Fix crash when wrapping a pandas NA scalar (#429)#432
Open
gaoflow wants to merge 1 commit into
Open
Conversation
In _wrap_text_to_colwidths the empty-string check compared each cell directly to "". For a pandas NA scalar, cell == "" returns a non-bool NA whose truthiness then raises "boolean value of NA is ambiguous" inside the surrounding or, so tabulate crashed whenever maxcolwidths was set and any cell was pd.NA. Guard the comparison with isinstance(cell, str) so only real strings are tested for emptiness; the NA then flows through the normal _type path and renders as <NA>, matching how plain tabulate (without maxcolwidths) already displays it.
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.
What
Fixes #429:
tabulate(df, maxcolwidths=...)raisedTypeError: boolean value of NA is ambiguouswhenever a cell was a pandasNAscalar (e.g. afloat32[pyarrow]column with a missing value).Why
In
_wrap_text_to_colwidthseach cell is classified with:For a pandas
NA,cell == ""does not return a plainbool— it returnsNA, and forcing that to a truth value inside theorraisesTypeError: boolean value of NA is ambiguous. So any table withmaxcolwidthsset crashed on apd.NAcell.Fix
Guard the empty-string test with
isinstance(cell, str)so only real strings are compared to"". ANAthen flows through the normal_typepath and renders as<NA>— exactly how plaintabulate(withoutmaxcolwidths) already displays it, so the two paths now agree.Verified against the issue's exact reproducer (a
float32[pyarrow]column): themaxcolwidthsoutput now matches the non-maxcolwidthsoutput, and aNAmixed with real floats still renders the real values correctly.Tests
Added
test_wrap_pandas_na_valueintest/test_textwrapper.py(skips if pandas is unavailable). It reproduces the crash onmaster(TypeError: boolean value of NA is ambiguousat the same line) and passes with the fix. The wrapping/textwrapper test modules are all green.This change was prepared by an AI agent under my direction; I reviewed and verified it.