diff --git a/doc/source/whatsnew/v0.17.0.txt b/doc/source/whatsnew/v0.17.0.txt index 9b87c6c1332ab..dd151682339e1 100644 --- a/doc/source/whatsnew/v0.17.0.txt +++ b/doc/source/whatsnew/v0.17.0.txt @@ -83,7 +83,7 @@ Bug Fixes - Bug in `plot` not defaulting to matplotlib `axes.grid` setting (:issue:`9792`) - Bug in ``Series.align`` resets ``name`` when ``fill_value`` is specified (:issue:`10067`) - +- Bug in ``SparseSeries.abs`` resets ``name`` (:issue:`10241`) - Bug in GroupBy.get_group raises ValueError when group key contains NaT (:issue:`6992`) diff --git a/pandas/sparse/series.py b/pandas/sparse/series.py index 2c328e51b5090..f53cc66bee961 100644 --- a/pandas/sparse/series.py +++ b/pandas/sparse/series.py @@ -399,7 +399,7 @@ def abs(self): res_sp_values = np.abs(self.sp_values) return self._constructor(res_sp_values, index=self.index, sparse_index=self.sp_index, - fill_value=self.fill_value) + fill_value=self.fill_value).__finalize__(self) def get(self, label, default=None): """ diff --git a/pandas/sparse/tests/test_sparse.py b/pandas/sparse/tests/test_sparse.py index dd1d10f3d15ed..a7a78ba226a0b 100644 --- a/pandas/sparse/tests/test_sparse.py +++ b/pandas/sparse/tests/test_sparse.py @@ -509,6 +509,21 @@ def _check_inplace_op(iop, op): _check_inplace_op( getattr(operator, "i%s" % op), getattr(operator, op)) + def test_abs(self): + s = SparseSeries([1, 2, -3], name='x') + expected = SparseSeries([1, 2, 3], name='x') + result = s.abs() + assert_sp_series_equal(result, expected) + self.assertEqual(result.name, 'x') + + result = abs(s) + assert_sp_series_equal(result, expected) + self.assertEqual(result.name, 'x') + + result = np.abs(s) + assert_sp_series_equal(result, expected) + self.assertEqual(result.name, 'x') + def test_reindex(self): def _compare_with_series(sps, new_index): spsre = sps.reindex(new_index) diff --git a/pandas/tests/test_panel.py b/pandas/tests/test_panel.py index d7d83887298b1..57fd465993e14 100644 --- a/pandas/tests/test_panel.py +++ b/pandas/tests/test_panel.py @@ -404,6 +404,8 @@ def test_abs(self): expected = np.abs(s) assert_series_equal(result, expected) assert_series_equal(result2, expected) + self.assertEqual(result.name, 'A') + self.assertEqual(result2.name, 'A') class CheckIndexing(object):