Skip to content

Object dtype for empty describe #27184

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 3 commits into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9644,6 +9644,7 @@ def describe_categorical_1d(data):
objcounts = data.value_counts()
count_unique = len(objcounts[objcounts != 0])
result = [data.count(), count_unique]
dtype = None
if result[1] > 0:
top, freq = objcounts.index[0], objcounts.iloc[0]

Expand All @@ -9668,9 +9669,10 @@ def describe_categorical_1d(data):
# to maintain output shape consistency
else:
names += ['top', 'freq']
result += [None, None]
result += [np.nan, np.nan]
dtype = 'object'

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

def describe_1d(data):
if is_bool_dtype(data):
Expand Down
14 changes: 13 additions & 1 deletion pandas/tests/frame/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,17 @@ def test_bool_describe_in_mixed_frame(self):
index=['count', 'unique', 'top', 'freq'])
tm.assert_frame_equal(result, expected)

def test_describe_empty_object(self):
# https://github.com/pandas-dev/pandas/issues/27183
df = pd.DataFrame({"A": [None, None]}, dtype=object)
result = df.describe()
expected = pd.DataFrame({"A": [0, 0, np.nan, np.nan]}, dtype=object,
index=['count', 'unique', 'top', 'freq'])
tm.assert_frame_equal(result, expected)

result = df.iloc[:0].describe()
tm.assert_frame_equal(result, expected)

def test_describe_bool_frame(self):
# GH 13891
df = pd.DataFrame({
Expand Down Expand Up @@ -595,7 +606,8 @@ def test_describe_empty_categorical_column(self):
df = pd.DataFrame({"empty_col": Categorical([])})
result = df.describe()
expected = DataFrame({'empty_col': [0, 0, None, None]},
index=['count', 'unique', 'top', 'freq'])
index=['count', 'unique', 'top', 'freq'],
dtype='object')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added in 0.25.0. Returning object dtype isn't an (additional) API change from 0.24

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should these be np.nan? (and not None); I am not sure we actually check this in the comparator.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wasn't clear to me whether I should use None or NaN. Arguments for NaN instead of None

  1. it's our most consistent missing value marker
  2. We often translate None to NaN implicitly

but I don't know what's best.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right I think we should np.nan; however I don't remember if we actually test this in assert_series_equal; maybe the check_exact arg does this but I don't remember.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch

In [4]: a = pd.Series([None], dtype=object)

In [5]: b = pd.Series([np.nan], dtype=object)

In [6]: pd.util.testing.assert_series_equal(a, b)

tm.assert_frame_equal(result, expected)

def test_describe_categorical_columns(self):
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/series/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ def test_describe(self):
index=['count', 'unique', 'top', 'freq'])
tm.assert_series_equal(result, expected)

def test_describe_empty_object(self):
# https://github.com/pandas-dev/pandas/issues/27183
s = pd.Series([None, None], dtype=object)
result = s.describe()
expected = pd.Series([0, 0, np.nan, np.nan], dtype=object,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually you use np.nan here

index=['count', 'unique', 'top', 'freq'])
tm.assert_series_equal(result, expected)

result = s[:0].describe()
tm.assert_series_equal(result, expected)

def test_describe_with_tz(self, tz_naive_fixture):
# GH 21332
tz = tz_naive_fixture
Expand Down