BUG: describe/Description handles 0-row (empty) input gracefully (#9891)#9899
Merged
bashtage merged 2 commits intoJul 20, 2026
Merged
Conversation
Description.numeric raised on 0-row input: df.apply(_mode) hit pandas' empty-result path and returned a 2-length result for a 1-length index (ValueError), and for all-categorical input the empty Jarque-Bera frame had no columns so jb[2]/jb[3] raised KeyError. Short-circuit both undefined computations to NaN when there are no observations, so describe now returns an empty/NaN summary (matching pandas.DataFrame.describe) instead of an opaque error. Adds a parametrized regression test.
bashtage
requested changes
Jul 20, 2026
bashtage
left a comment
Member
There was a problem hiding this comment.
Some simplification and clean up would help.
- Remove the legacy 'pandas before 1.0' branch in the mode computation; df.apply(...).T is always a DataFrame on supported pandas. - Replace 'df.shape[0] > 0 and df.shape[1] > 0' with 'df.size'. Addresses bashtage's review on statsmodels#9899.
Member
|
Thanks. |
bashtage
approved these changes
Jul 20, 2026
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.
Closes #9891.
Problem
describe/Descriptionraised an opaque error, deep from pandas/numpy, when given a 0-row DataFrame — even thoughpandas.DataFrame.describe()handles the same input fine and returns a frame of NaNs. The message depended on the column dtype, hinting at two separate unhandled spots:ValueError: Length of values (2) does not match length of index (1)KeyError: 2Root cause
Both live in
Description.numeric(whichdescribecomputes by default):df.apply(_mode)on a 0-row frame takes pandas' empty-result path, which calls_modeon an empty column;_modereturns a 2-tuple, so pandas tries to fit 2 values into the 1-length column index →ValueError.jbhas no columns andjb[2]/jb[3](skew/kurtosis) →KeyError: 2.Fix
Short-circuit both undefined computations to NaN when there are no observations:
df.shape[0] == 0, setmode/mode_countsto NaN of the right length instead of callingapply;describenow returns an empty/NaN summary (nobs=0, missing=0, other stats NaN), matchingpandas.describe. Non-empty inputs are completely unchanged.Tests
Added a parametrized
test_empty_rowscovering 0-row numeric, categorical, and mixed frames (asserts no error, correct columns, nobs/missing = 0, NaN stats, anddescribe == Description(...).frame). Fulltest_descriptivestats.pypasses (18 tests).