diff --git a/doc/source/whatsnew/v0.18.2.txt b/doc/source/whatsnew/v0.18.2.txt index a5734dc1db200..3c66f18dc05f0 100644 --- a/doc/source/whatsnew/v0.18.2.txt +++ b/doc/source/whatsnew/v0.18.2.txt @@ -76,6 +76,7 @@ Performance Improvements Bug Fixes ~~~~~~~~~ +- Bug in ``SparseDataFrame`` in which ``axis=None`` did not default to ``axis=0`` (:issue:`13048`) diff --git a/pandas/sparse/frame.py b/pandas/sparse/frame.py index 2e2a2c3e8846c..52a6e6edf0896 100644 --- a/pandas/sparse/frame.py +++ b/pandas/sparse/frame.py @@ -651,6 +651,9 @@ def transpose(self, *args, **kwargs): @Appender(DataFrame.count.__doc__) def count(self, axis=0, **kwds): + if axis is None: + axis = self._stat_axis_number + return self.apply(lambda x: x.count(), axis=axis) def cumsum(self, axis=0, *args, **kwargs): @@ -667,6 +670,10 @@ def cumsum(self, axis=0, *args, **kwargs): y : SparseDataFrame """ nv.validate_cumsum(args, kwargs) + + if axis is None: + axis = self._stat_axis_number + return self.apply(lambda x: x.cumsum(), axis=axis) def apply(self, func, axis=0, broadcast=False, reduce=False): diff --git a/pandas/sparse/tests/test_frame.py b/pandas/sparse/tests/test_frame.py index 07b97affa62e9..fde4ad15e1185 100644 --- a/pandas/sparse/tests/test_frame.py +++ b/pandas/sparse/tests/test_frame.py @@ -2,7 +2,6 @@ import operator -import nose # noqa from numpy import nan import numpy as np import pandas as pd @@ -768,12 +767,19 @@ def _check(frame, orig): self._check_all(_check) def test_count(self): - result = self.frame.count() dense_result = self.frame.to_dense().count() + + result = self.frame.count() + tm.assert_series_equal(result, dense_result) + + result = self.frame.count(axis=None) + tm.assert_series_equal(result, dense_result) + + result = self.frame.count(axis=0) tm.assert_series_equal(result, dense_result) - result = self.frame.count(1) - dense_result = self.frame.to_dense().count(1) + result = self.frame.count(axis=1) + dense_result = self.frame.to_dense().count(axis=1) # win32 don't check dtype tm.assert_series_equal(result, dense_result, check_dtype=False) @@ -862,12 +868,19 @@ def setUp(self): self.frame = SparseDataFrame(self.data, index=self.dates) def test_cumsum(self): - result = self.frame.cumsum() expected = SparseDataFrame(self.frame.to_dense().cumsum()) + + result = self.frame.cumsum() + tm.assert_sp_frame_equal(result, expected) + + result = self.frame.cumsum(axis=None) + tm.assert_sp_frame_equal(result, expected) + + result = self.frame.cumsum(axis=0) tm.assert_sp_frame_equal(result, expected) def test_numpy_cumsum(self): - result = np.cumsum(self.frame, axis=0) + result = np.cumsum(self.frame) expected = SparseDataFrame(self.frame.to_dense().cumsum()) tm.assert_sp_frame_equal(result, expected) @@ -879,7 +892,16 @@ def test_numpy_cumsum(self): tm.assertRaisesRegexp(ValueError, msg, np.cumsum, self.frame, out=result) + def test_numpy_func_call(self): + # no exception should be raised even though + # numpy passes in 'axis=None' or `axis=-1' + funcs = ['sum', 'cumsum', 'var', + 'mean', 'prod', 'cumprod', + 'std', 'min', 'max'] + for func in funcs: + getattr(np, func)(self.frame) + if __name__ == '__main__': - import nose # noqa + import nose nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'], exit=False) diff --git a/pandas/sparse/tests/test_series.py b/pandas/sparse/tests/test_series.py index 9a53f50c6432e..44bc51077ef3e 100644 --- a/pandas/sparse/tests/test_series.py +++ b/pandas/sparse/tests/test_series.py @@ -2,7 +2,6 @@ import operator -import nose # noqa from numpy import nan import numpy as np import pandas as pd @@ -549,6 +548,7 @@ def check(a, b): def test_binary_operators(self): # skipping for now ##### + import nose raise nose.SkipTest("skipping sparse binary operators test") def _check_inplace_op(iop, op): @@ -1259,7 +1259,17 @@ def test_numpy_cumsum(self): tm.assertRaisesRegexp(ValueError, msg, np.cumsum, self.zbseries, out=result) + def test_numpy_func_call(self): + # no exception should be raised even though + # numpy passes in 'axis=None' or `axis=-1' + funcs = ['sum', 'cumsum', 'var', 'mean', + 'prod', 'cumprod', 'std', 'argsort', + 'argmin', 'argmax', 'min', 'max'] + for func in funcs: + for series in ('bseries', 'zbseries'): + getattr(np, func)(getattr(self, series)) + if __name__ == '__main__': - import nose # noqa + import nose nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'], exit=False)