Skip to content

Series.describe() fails for empty and None series. #1650

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

Closed
wants to merge 3 commits into from
Closed
Changes from all 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
40 changes: 22 additions & 18 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
@@ -1345,22 +1345,25 @@ def describe(self, percentile_width=50):
from pandas.util.counter import Counter

if self.dtype == object:
names = ['count', 'unique', 'top', 'freq']

names = ['count', 'unique']
objcounts = Counter(self.dropna().values)
top, freq = objcounts.most_common(1)[0]
data = [self.count(), len(objcounts), top, freq]
data = [self.count(), len(objcounts)]
if data[1] > 0:
names += ['top', 'freq']
top, freq = objcounts.most_common(1)[0]
data += [top, freq]

elif issubclass(self.dtype.type, np.datetime64):
names = ['count', 'unique', 'first', 'last', 'top', 'freq']

names = ['count', 'unique']
asint = self.dropna().view('i8')
objcounts = Counter(asint)
top, freq = objcounts.most_common(1)[0]
data = [self.count(), len(objcounts),
lib.Timestamp(asint.min()),
lib.Timestamp(asint.max()),
lib.Timestamp(top), freq]
data = [self.count(), len(objcounts)]
if data[1] > 0:
top, freq = objcounts.most_common(1)[0]
names += ['first', 'last', 'top', 'freq']
data += [lib.Timestamp(asint.min()),
lib.Timestamp(asint.max()),
lib.Timestamp(top), freq]
else:

lb = .5 * (1. - percentile_width/100.)
@@ -1373,13 +1376,14 @@ def pretty_name(x):
else:
return '%.1f%%' % x

names = ['count', 'mean', 'std', 'min',
pretty_name(lb), '50%', pretty_name(ub),
'max']

data = [self.count(), self.mean(), self.std(), self.min(),
self.quantile(lb), self.median(), self.quantile(ub),
self.max()]
names = ['count']
data = [self.count()]
if data[0] > 0:
names += ['mean', 'std', 'min', pretty_name(lb), '50%',
pretty_name(ub), 'max']
data += [self.mean(), self.std(), self.min(),
self.quantile(lb), self.median(), self.quantile(ub),
self.max()]

return Series(data, index=names)

18 changes: 18 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
@@ -210,6 +210,12 @@ def setUp(self):

self.empty = Series([], index=[])

self.noneSeries = Series([None])
self.noneSeries.name = 'None'

self.nanSeries = Series([np.nan])
self.nanSeries.name = 'NaN'

def test_constructor(self):
# Recognize TimeSeries
self.assert_(isinstance(self.ts, TimeSeries))
@@ -1249,6 +1255,18 @@ def test_describe_objects(self):
'top' : min_date}, index=rs.index)
assert_series_equal(rs, xp)

def test_describe_empty(self):
assert_series_equal(self.empty.describe(),
Series([0], index=['count']))

def test_describe_none(self):
assert_series_equal(self.noneSeries.describe(),
Series([0, 0], index=['count', 'unique']))

def test_describe_nan(self):
assert_series_equal(self.nanSeries.describe(),
Series([0], index=['count']))

def test_append(self):
appendedSeries = self.series.append(self.objSeries)
for idx, value in appendedSeries.iteritems():