diff --git a/pandas/core/series.py b/pandas/core/series.py index c0a013e387a04..30e91ecf717d4 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -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) diff --git a/pandas/tests/test_series.py b/pandas/tests/test_series.py index 5ad88a67c1042..667072d0453ab 100644 --- a/pandas/tests/test_series.py +++ b/pandas/tests/test_series.py @@ -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():