Skip to content

Commit

Permalink
BUG: exclude NAs also with dtype=object in nanmean, others, GH #469
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Dec 11, 2011
1 parent 34db250 commit d665004
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
1 change: 1 addition & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Thanks
- Jev Kuznetsov
- Wouter Overmeire
- Fernando Perez
- Nathan Pinger
- Christian Prinoth
- Joon Ro
- Chang She
Expand Down
55 changes: 25 additions & 30 deletions pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,36 @@
_USE_BOTTLENECK = False

def nansum(values, axis=None, skipna=True, copy=True):
if values.dtype == np.object_:
the_sum = values.sum(axis)
else:
mask = isnull(values)
mask = isnull(values)

if skipna and not issubclass(values.dtype.type, np.integer):
if copy:
values = values.copy()
np.putmask(values, mask, 0)
if skipna and not issubclass(values.dtype.type, np.integer):
if copy:
values = values.copy()
np.putmask(values, mask, 0)

the_sum = values.sum(axis)
the_sum = _maybe_null_out(the_sum, axis, mask)
the_sum = values.sum(axis)
the_sum = _maybe_null_out(the_sum, axis, mask)

return the_sum

def nanmean(values, axis=None, skipna=True, copy=True):
if values.dtype == np.object_:
the_mean = values.sum(axis) / float(values.shape[axis])
else:
mask = isnull(values)

if skipna and not issubclass(values.dtype.type, np.integer):
if copy:
values = values.copy()
np.putmask(values, mask, 0)
mask = isnull(values)

the_sum = values.sum(axis)
count = _get_counts(mask, axis)
if skipna and not issubclass(values.dtype.type, np.integer):
if copy:
values = values.copy()
np.putmask(values, mask, 0)

if axis is not None:
the_mean = the_sum / count
ct_mask = count == 0
if ct_mask.any():
the_mean[ct_mask] = np.nan
else:
the_mean = the_sum / count if count > 0 else np.nan
the_sum = values.sum(axis)
count = _get_counts(mask, axis)

if axis is not None:
the_mean = the_sum / count
ct_mask = count == 0
if ct_mask.any():
the_mean[ct_mask] = np.nan
else:
the_mean = the_sum / count if count > 0 else np.nan
return the_mean

def nanmedian(values, axis=None, skipna=True, copy=True):
Expand Down Expand Up @@ -121,7 +114,8 @@ def nanmin(values, axis=None, skipna=True, copy=True):
# numpy 1.6.1 workaround in Python 3.x
if values.dtype == np.object_: # pragma: no cover
import __builtin__
result = np.apply_along_axis(__builtin__.min, axis, values)
apply_ax = axis if axis is not None else 0
result = np.apply_along_axis(__builtin__.min, apply_ax, values)
else:
result = values.min(axis)

Expand All @@ -136,7 +130,8 @@ def nanmax(values, axis=None, skipna=True, copy=True):
# numpy 1.6.1 workaround in Python 3.x
if values.dtype == np.object_: # pragma: no cover
import __builtin__
result = np.apply_along_axis(__builtin__.max, axis, values)
apply_ax = axis if axis is not None else 0
result = np.apply_along_axis(__builtin__.max, apply_ax, values)
else:
result = values.max(axis)
return _maybe_null_out(result, axis, mask)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3234,7 +3234,7 @@ def test_count(self):
def test_sum(self):
self._check_stat_op('sum', np.sum, has_numeric_only=True)

def test_stat_ops_attempt_obj_array(self):
def test_stat_operators_attempt_obj_array(self):
data = {
'a': [-0.00049987540199591344, -0.0016467257772919831,
0.00067695870775883013],
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,10 @@ def _check_stat_op(self, name, alternate):
allna = self.series * nan
self.assert_(np.isnan(f(allna)))

# dtype=object with None, it works!
s = Series([1, 2, 3, None, 5])
f(s)

def _check_accum_op(self, name):
func = getattr(np, name)
self.assert_(np.array_equal(func(self.ts), func(np.array(self.ts))))
Expand Down

0 comments on commit d665004

Please sign in to comment.