Skip to content

Commit 4e6a055

Browse files
author
Chang She
committed
BUG: sparse reduction bug pandas-dev#1375
1 parent 1236079 commit 4e6a055

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

pandas/sparse/array.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ def count(self):
336336
if self._null_fill_value:
337337
return valid_spvals
338338
else:
339-
return valid_spvals + (len(self) - len(sp_values))
339+
return valid_spvals + self.sp_index.ngaps
340340

341341
@property
342342
def _null_fill_value(self):
@@ -361,7 +361,7 @@ def sum(self, axis=None, dtype=None, out=None):
361361
if self._null_fill_value:
362362
return sp_sum
363363
else:
364-
nsparse = self.sp_index.npoints
364+
nsparse = self.sp_index.ngaps
365365
return sp_sum + self.fill_value * nsparse
366366

367367
def cumsum(self, axis=0, dtype=None, out=None):
@@ -396,11 +396,9 @@ def mean(self, axis=None, dtype=None, out=None):
396396
if self._null_fill_value:
397397
return sp_sum / ct
398398
else:
399-
nsparse = self.sp_index.npoints
399+
nsparse = self.sp_index.ngaps
400400
return (sp_sum + self.fill_value * nsparse) / (ct + nsparse)
401401

402-
403-
404402
def make_sparse(arr, kind='block', fill_value=nan):
405403
"""
406404
Convert ndarray to sparse format

pandas/sparse/tests/test_array.py

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import pickle
66
import unittest
77

8+
from pandas.core.series import Series
9+
from pandas.core.common import notnull
810
from pandas.sparse.api import SparseArray
911
from pandas.util.testing import assert_almost_equal
1012

pandas/sparse/tests/test_sparse.py

+7
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ def _compare_all(obj):
550550
_compare_with_dense(obj, op)
551551

552552
_compare_all(self.bseries)
553+
553554
self.bseries.sp_values[5:10] = np.NaN
554555
_compare_all(self.bseries)
555556

@@ -561,6 +562,12 @@ def _compare_all(obj):
561562
series.fill_value = 2
562563
_compare_all(series)
563564

565+
nonna = Series(np.random.randn(20)).to_sparse()
566+
_compare_all(nonna)
567+
568+
nonna2 = Series(np.random.randn(20)).to_sparse(fill_value=0)
569+
_compare_all(nonna2)
570+
564571
def test_dropna(self):
565572
sp = SparseSeries([0, 0, 0, nan, nan, 5, 6],
566573
fill_value=0)

pandas/src/sparse.pyx

+8
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ cdef class IntIndex(SparseIndex):
7676
same_indices = np.array_equal(self.indices, other.indices)
7777
return same_length and same_indices
7878

79+
@property
80+
def ngaps(self):
81+
return self.length - self.npoints
82+
7983
def to_int_index(self):
8084
return self
8185

@@ -296,6 +300,10 @@ cdef class BlockIndex(SparseIndex):
296300

297301
return output
298302

303+
@property
304+
def ngaps(self):
305+
return self.length - self.npoints
306+
299307
cpdef check_integrity(self):
300308
'''
301309
Check:

0 commit comments

Comments
 (0)