Skip to content

Commit da30e21

Browse files
committed
BUG: Default to stat axis for SparseDataFrame when axis=None
Closes gh-13048
1 parent 1296ab3 commit da30e21

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

doc/source/whatsnew/v0.18.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ Performance Improvements
7676

7777
Bug Fixes
7878
~~~~~~~~~
79+
- Bug in ``SparseDataFrame`` in which ``axis=None`` did not default to ``axis=0`` (:issue:`13048`)
7980

8081

8182

pandas/sparse/frame.py

+7
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,9 @@ def transpose(self, *args, **kwargs):
651651

652652
@Appender(DataFrame.count.__doc__)
653653
def count(self, axis=0, **kwds):
654+
if axis is None:
655+
axis = self._stat_axis_number
656+
654657
return self.apply(lambda x: x.count(), axis=axis)
655658

656659
def cumsum(self, axis=0, *args, **kwargs):
@@ -667,6 +670,10 @@ def cumsum(self, axis=0, *args, **kwargs):
667670
y : SparseDataFrame
668671
"""
669672
nv.validate_cumsum(args, kwargs)
673+
674+
if axis is None:
675+
axis = self._stat_axis_number
676+
670677
return self.apply(lambda x: x.cumsum(), axis=axis)
671678

672679
def apply(self, func, axis=0, broadcast=False, reduce=False):

pandas/sparse/tests/test_frame.py

+29-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import operator
44

5-
import nose # noqa
65
from numpy import nan
76
import numpy as np
87
import pandas as pd
@@ -768,12 +767,19 @@ def _check(frame, orig):
768767
self._check_all(_check)
769768

770769
def test_count(self):
771-
result = self.frame.count()
772770
dense_result = self.frame.to_dense().count()
771+
772+
result = self.frame.count()
773+
tm.assert_series_equal(result, dense_result)
774+
775+
result = self.frame.count(axis=None)
776+
tm.assert_series_equal(result, dense_result)
777+
778+
result = self.frame.count(axis=0)
773779
tm.assert_series_equal(result, dense_result)
774780

775-
result = self.frame.count(1)
776-
dense_result = self.frame.to_dense().count(1)
781+
result = self.frame.count(axis=1)
782+
dense_result = self.frame.to_dense().count(axis=1)
777783

778784
# win32 don't check dtype
779785
tm.assert_series_equal(result, dense_result, check_dtype=False)
@@ -862,12 +868,19 @@ def setUp(self):
862868
self.frame = SparseDataFrame(self.data, index=self.dates)
863869

864870
def test_cumsum(self):
865-
result = self.frame.cumsum()
866871
expected = SparseDataFrame(self.frame.to_dense().cumsum())
872+
873+
result = self.frame.cumsum()
874+
tm.assert_sp_frame_equal(result, expected)
875+
876+
result = self.frame.cumsum(axis=None)
877+
tm.assert_sp_frame_equal(result, expected)
878+
879+
result = self.frame.cumsum(axis=0)
867880
tm.assert_sp_frame_equal(result, expected)
868881

869882
def test_numpy_cumsum(self):
870-
result = np.cumsum(self.frame, axis=0)
883+
result = np.cumsum(self.frame)
871884
expected = SparseDataFrame(self.frame.to_dense().cumsum())
872885
tm.assert_sp_frame_equal(result, expected)
873886

@@ -879,7 +892,16 @@ def test_numpy_cumsum(self):
879892
tm.assertRaisesRegexp(ValueError, msg, np.cumsum,
880893
self.frame, out=result)
881894

895+
def test_numpy_func_call(self):
896+
# no exception should be raised even though
897+
# numpy passes in 'axis=None' or `axis=-1'
898+
funcs = ['sum', 'cumsum', 'var',
899+
'mean', 'prod', 'cumprod',
900+
'std', 'min', 'max']
901+
for func in funcs:
902+
getattr(np, func)(self.frame)
903+
882904
if __name__ == '__main__':
883-
import nose # noqa
905+
import nose
884906
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
885907
exit=False)

pandas/sparse/tests/test_series.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import operator
44

5-
import nose # noqa
65
from numpy import nan
76
import numpy as np
87
import pandas as pd
@@ -549,6 +548,7 @@ def check(a, b):
549548
def test_binary_operators(self):
550549

551550
# skipping for now #####
551+
import nose
552552
raise nose.SkipTest("skipping sparse binary operators test")
553553

554554
def _check_inplace_op(iop, op):
@@ -1259,7 +1259,17 @@ def test_numpy_cumsum(self):
12591259
tm.assertRaisesRegexp(ValueError, msg, np.cumsum,
12601260
self.zbseries, out=result)
12611261

1262+
def test_numpy_func_call(self):
1263+
# no exception should be raised even though
1264+
# numpy passes in 'axis=None' or `axis=-1'
1265+
funcs = ['sum', 'cumsum', 'var', 'mean',
1266+
'prod', 'cumprod', 'std', 'argsort',
1267+
'argmin', 'argmax', 'min', 'max']
1268+
for func in funcs:
1269+
for series in ('bseries', 'zbseries'):
1270+
getattr(np, func)(getattr(self, series))
1271+
12621272
if __name__ == '__main__':
1263-
import nose # noqa
1273+
import nose
12641274
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
12651275
exit=False)

0 commit comments

Comments
 (0)