From 44ecd0f69315be3950ddf98bf35ee69fef926e26 Mon Sep 17 00:00:00 2001 From: Wes McKinney Date: Thu, 3 Nov 2011 17:27:57 -0400 Subject: [PATCH] BUG: work around ndarray.round not preserving subclasses, GH #314 --- pandas/core/series.py | 11 +++++++++++ pandas/tests/test_series.py | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/pandas/core/series.py b/pandas/core/series.py index 81cf9803bc096..9cf18750f717c 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -940,6 +940,17 @@ def _ndarray_statistic(self, funcname, dtype=None, skipna=True): return retVal + def round(self, decimals=0, out=None): + """ + + """ + result = self.values.round(decimals, out=out) + if out is None: + result = Series(result, index=self.index, name=self.name) + + return result + round.__doc__ = np.ndarray.round.__doc__ + def quantile(self, q=0.5): """ Return value at the given quantile, a la scoreatpercentile in diff --git a/pandas/tests/test_series.py b/pandas/tests/test_series.py index 8bbd0a1e1dbde..f76579aff5b1e 100644 --- a/pandas/tests/test_series.py +++ b/pandas/tests/test_series.py @@ -551,6 +551,14 @@ def _check_accum_op(self, name): self.assert_(np.array_equal(result, expected)) + def test_round(self): + # numpy.round doesn't preserve metadata, probably a numpy bug, + # re: GH #314 + result = np.round(self.ts, 2) + expected = Series(np.round(self.ts.values, 2), index=self.ts.index) + assert_series_equal(result, expected) + self.assertEqual(result.name, self.ts.name) + def test_prod_numpy16_bug(self): s = Series([1., 1., 1.] , index=range(3)) result = s.prod()