Skip to content

Commit

Permalink
BUG: allow cummethods to work with bool dtypes
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Aug 7, 2013
1 parent c51a0db commit 9da899b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
2 changes: 2 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ pandas 0.13
(:issue:`3440`)
- Fixed an issue where duplicate indexes were raising when plotting
(:issue:`4486`)
- Fixed an issue where cumsum and cumprod didn't work with bool dtypes
(:issue:`4170`, :issue:`4440`)

pandas 0.12
===========
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1654,7 +1654,8 @@ def cumsum(self, axis=0, dtype=None, out=None, skipna=True):
"""
arr = self.values.copy()

do_mask = skipna and not issubclass(self.dtype.type, np.integer)
do_mask = skipna and not issubclass(self.dtype.type,
(np.integer, np.bool_))
if do_mask:
mask = isnull(arr)
np.putmask(arr, mask, 0.)
Expand Down Expand Up @@ -1683,7 +1684,8 @@ def cumprod(self, axis=0, dtype=None, out=None, skipna=True):
"""
arr = self.values.copy()

do_mask = skipna and not issubclass(self.dtype.type, np.integer)
do_mask = skipna and not issubclass(self.dtype.type,
(np.integer, np.bool_))
if do_mask:
mask = isnull(arr)
np.putmask(arr, mask, 1.)
Expand Down
33 changes: 33 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3963,6 +3963,39 @@ def test_bfill(self):
ts[2] = np.NaN
assert_series_equal(ts.bfill(), ts.fillna(method='bfill'))

def test_cummethods_bool(self):
def cummin(x):
return np.minimum.accumulate(x)

def cummax(x):
return np.maximum.accumulate(x)

from itertools import product
a = pd.Series([False, False, False, True, True, False, False])
b = ~a
c = pd.Series([False] * len(b))
d = ~c
methods = {'cumsum': np.cumsum, 'cumprod': np.cumprod,
'cummin': cummin, 'cummax': cummax}
args = product((a, b, c, d), methods)
for s, method in args:
expected = Series(methods[method](s.values))
result = getattr(s, method)()
assert_series_equal(result, expected)

e = pd.Series([False, True, nan, False])
cse = pd.Series([0, 1, nan, 1], dtype=object)
cpe = pd.Series([False, 0, nan, 0])
cmin = pd.Series([False, False, nan, False])
cmax = pd.Series([False, True, nan, True])
expecteds = {'cumsum': cse, 'cumprod': cpe, 'cummin': cmin,
'cummax': cmax}

for method in methods:
res = getattr(e, method)()
assert_series_equal(res, expecteds[method])


def test_replace(self):
N = 100
ser = Series(np.random.randn(N))
Expand Down

0 comments on commit 9da899b

Please sign in to comment.