8000 Adding np.nanmean(), nanstd(), and nanvar(), gh-3297 branched. by charris · Pull Request #3416 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Adding np.nanmean(), nanstd(), and nanvar(), gh-3297 branched. #3416

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

Closed
wants to merge 6 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Tests now checks the warning state
  • Loading branch information
WeatherGod authored and charris committed Jun 24, 2013
commit f58f89433656405fbd0a4298b48b4c8a32ff4df1
44 changes: 34 additions & 10 deletions numpy/core/tests/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import platform
from decimal import Decimal
import warnings

import numpy as np
from numpy.core import *
Expand Down Expand Up @@ -177,6 +178,11 @@ def test_mean(self):
assert_(all(mean(A,0) == array([2.5,3.5,4.5])))
assert_(all(mean(A,1) == array([2.,5.])))

with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always', '', RuntimeWarning)
assert_(isnan(mean([])))
assert_(w[0].category is RuntimeWarning)

def test_nanmean(self):
A = [[1, nan, nan], [nan, 4, 5]]
assert_(nanmean(A) == (10.0 / 3))
Expand All @@ -189,6 +195,11 @@ def test_std(self):
assert_almost_equal(std(A,0), array([1.5, 1.5, 1.5]))
assert_almost_equal(std(A,1), array([0.81649658, 0.81649658]))

with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always', '', RuntimeWarning)
assert_(isnan(std([])))
assert_(w[0].category is RuntimeWarning)

def test_nanstd(self):
A = [[1, nan, nan], [nan, 4, 5]]
assert_almost_equal(nanstd(A), 1.699673171197595)
Expand All @@ -201,6 +212,11 @@ def test_var(self):
assert_almost_equal(var(A,0), array([2.25, 2.25, 2.25]))
assert_almost_equal(var(A,1), array([0.66666667, 0.66666667]))

with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always', '', RuntimeWarning)
assert_(isnan(mean([])))
assert_(w[0].category is RuntimeWarning)

def test_nanvar(self):
A = [[1, nan, nan], [nan, 4, 5]]
assert_almost_equal(nanvar(A), 2.88888888889)
Expand Down Expand Up @@ -1395,13 +1411,16 @@ def test_basic(self):
assert_almost_equal(nanmean(self.A),self.real_mean)

def test_allnans(self):
assert_(isnan(nanmean(self.B)))
with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always', '', RuntimeWarning)
assert_(isnan(nanmean(self.B)))
assert_(w[0].category is RuntimeWarning)

def test_empty(self):
assert_(isnan(nanmean(array([]))))



with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always', '', RuntimeWarning)
assert_(isnan(nanmean(array([]))))
assert_(w[0].category is RuntimeWarning)

class TestStdVar(TestCase):
def setUp(self):
Expand Down Expand Up @@ -1447,13 +1466,18 @@ def test_ddof2(self):
self.real_var*sum(~isnan(self.A))/float(sum(~isnan(self.A))-2))

def test_allnans(self):
assert_(isnan(nanvar(self.B)))
assert_(isnan(nanstd(self.B)))
with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always', '', RuntimeWarning)
assert_(isnan(nanvar(self.B)))
assert_(isnan(nanstd(self.B)))
assert_(w[0].category is RuntimeWarning)

def test_empty(self):
assert_(isnan(nanvar(array([]))))
assert_(isnan(nanstd(array([]))))

with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always', '', RuntimeWarning)
assert_(isnan(nanvar(array([]))))
assert_(isnan(nanstd(array([]))))
assert_(w[0].category is RuntimeWarning)

class TestStdVarComplex(TestCase):
def test_basic(self):
Expand Down
0