Skip to content

BUG: bug in nanops.var with ddof=1 and 1 elements would sometimes return inf rather than nan (GH6136) #6204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ Bug Fixes
- Consistency with dtypes in setting an empty DataFrame (:issue:`6171`)
- Bug in selecting on a multi-index ``HDFStore`` even in the prescence of under
specificed column spec (:issue:`6169`)
- Bug in ``nanops.var`` with ``ddof=1`` and 1 elements would sometimes return ``inf``
rather than ``nan`` on some platforms (:issue:`6136`)

pandas 0.13.0
-------------
Expand Down
15 changes: 13 additions & 2 deletions pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,25 @@ def nanvar(values, axis=None, skipna=True, ddof=1):
else:
count = float(values.size - mask.sum())

d = count-ddof
if skipna:
values = values.copy()
np.putmask(values, mask, 0)

# always return NaN, never inf
if np.isscalar(count):
if count <= ddof:
count = np.nan
d = np.nan
else:
mask = count <= ddof
if mask.any():
np.putmask(d, mask, np.nan)
np.putmask(count, mask, np.nan)

X = _ensure_numeric(values.sum(axis))
XX = _ensure_numeric((values ** 2).sum(axis))
return np.fabs((XX - X ** 2 / count) / (count - ddof))

return np.fabs((XX - X ** 2 / count) / d)

@bottleneck_switch()
def nanmin(values, axis=None, skipna=True):
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1832,6 +1832,14 @@ def test_var_std(self):
expected = np.var(self.ts.values, ddof=4)
assert_almost_equal(result, expected)

# 1 - element series with ddof=1
s = self.ts.iloc[[0]]
result = s.var(ddof=1)
self.assert_(isnull(result))

result = s.std(ddof=1)
self.assert_(isnull(result))

def test_skew(self):
_skip_if_no_scipy()

Expand Down