Skip to content

Fix the output of df.describe on an empty categorical / object column #26474

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

Merged
merged 10 commits into from
Jun 1, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1477,7 +1477,7 @@ def value_counts(self, dropna=True):

if dropna or clean:
obs = code if clean else code[mask]
count = bincount(obs, minlength=ncat or None)
count = bincount(obs, minlength=ncat or 0)
else:
count = bincount(np.where(mask, code, ncat))
ix = np.append(ix, -1)
Expand Down
6 changes: 6 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9920,6 +9920,12 @@ def describe_categorical_1d(data):
names += ['top', 'freq']
result += [top, freq]

# If the DataFrame is empty, set 'top' and 'freq' to None
# to maintain output shape consistency
else:
names += ['top', 'freq']
result += [None, None]

return pd.Series(result, index=names, name=data.name)

def describe_1d(data):
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/frame/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,14 @@ def test_describe_categorical(self):
result = df3.describe()
tm.assert_numpy_array_equal(result["cat"].values, result["s"].values)

# Ensure the index of an an empty categoric DataFrame column
# also contains (count, unique, top, freq)
df = pd.DataFrame({"empty_col": Categorical([])})
result = df.describe()
expected = DataFrame({'empty_col': [0, 0, None, None]},
index=['count', 'unique', 'top', 'freq'])
tm.assert_frame_equal(result, expected)

def test_describe_categorical_columns(self):
# GH 11558
columns = pd.CategoricalIndex(['int1', 'int2', 'obj'],
Expand All @@ -608,6 +616,7 @@ def test_describe_categorical_columns(self):
index=['count', 'mean', 'std', 'min', '25%',
'50%', '75%', 'max'],
columns=exp_columns)

tm.assert_frame_equal(result, expected)
tm.assert_categorical_equal(result.columns.values,
expected.columns.values)
Expand Down