8000 BUG: Fixed an issue wherein certain `nan<x>` functions could fail for object arrays by BvB93 · Pull Request #19821 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Fixed an issue wherein certain nan<x> functions could fail for object arrays #19821

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 5 commits into from
Sep 5, 2021
Merged
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
TST: Expand the old TestNanFunctions_IntTypes test with non-integer…
… number types
  • Loading branch information
Bas van Beek committed Sep 3, 2021
commit fffcb6e2bd45bf423f626ebf77a2e3142de636c4
123 changes: 52 additions & 71 deletions numpy/lib/tests/test_nanfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,79 +231,60 @@ class MyNDArray(np.ndarray):
assert_(res.shape == ())


class TestNanFunctions_IntTypes:

int_types = (np.int8, np.int16, np.int32, np.int64, np.uint8,
np.uint16, np.uint32, np.uint64)
@pytest.mark.parametrize(
"dtype",
np.typecodes["AllInteger"] + np.typecodes["AllFloat"] + "O",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np.typecodes['AllFloat'][1:] would remove 'e', but is not quite as robust.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, as of fffcb6e the tests seem to work fine with np.float16.

)
class TestNanFunctions_NumberTypes:

mat = np.array([127, 39, 93, 87, 46])

def integer_arrays(self):
for dtype in self.int_types:
yield self.mat.astype(dtype)

def test_nanmin(self):
tgt = np.min(self.mat)
for mat in self.integer_arrays():
assert_equal(np.nanmin(mat), tgt)

def test_nanmax(self):
tgt = np.max(self.mat)
for mat in self.integer_arrays():
assert_equal(np.nanmax(mat), tgt)

def test_nanargmin(self):
tgt = np.argmin(self.mat)
for mat in self.integer_arrays():
assert_equal(np.nanargmin(mat), tgt)

def test_nanargmax(self):
tgt = np.argmax(self.mat)
for mat in self.integer_arrays():
assert_equal(np.nanargmax(mat), tgt)

def test_nansum(self):
tgt = np.sum(self.mat)
for mat in self.integer_arrays():
assert_equal(np.nansum(mat), tgt)

def test_nanprod(self):
tgt = np.prod(self.mat)
for mat in self.integer_arrays():
assert_equal(np.nanprod(mat), tgt)

def test_nancumsum(self):
tgt = np.cumsum(self.mat)
for mat in self.integer_arrays():
assert_equal(np.nancumsum(mat), tgt)

def test_nancumprod(self):
tgt = np.cumprod(self.mat)
for mat in self.integer_arrays():
assert_equal(np.nancumprod(mat), tgt)

def test_nanmean(self):
tgt = np.mean(self.mat)
for mat in self.integer_arrays():
assert_equal(np.nanmean(mat), tgt)

def test_nanvar(self):
tgt = np.var(self.mat)
for mat in self.integer_arrays():
assert_equal(np.nanvar(mat), tgt)

tgt = np.var(mat, ddof=1)
for mat in self.integer_arrays():
assert_equal(np.nanvar(mat, ddof=1), tgt)

def test_nanstd(self):
tgt = np.std(self.mat)
for mat in self.integer_arrays():
assert_equal(np.nanstd(mat), tgt)

tgt = np.std(self.mat, ddof=1)
for mat in self.integer_arrays():
assert_equal(np.nanstd(mat, ddof=1), tgt)
mat.setflags(write=False)

nanfuncs = {
np.nanmin: np.min,
np.nanmax: np.max,
np.nanargmin: np.argmin,
np.nanargmax: np.argmax,
np.nansum: np.sum,
np.nanprod: np.prod,
np.nancumsum: np.cumsum,
np.nancumprod: np.cumprod,
np.nanmean: np.mean,
np.nanvar: np.var,
np.nanstd: np.std,
}
nanfunc_ids = [i.__name__ for i in nanfuncs]

@pytest.mark.parametrize("nanfunc,func", nanfuncs.items(), ids=nanfunc_ids)
def test_nanfunc(self, dtype, nanfunc, func):
if nanfunc is np.nanprod and dtype == "e":
pytest.xfail(reason="overflow encountered in reduce")

mat = self.mat.astype(dtype)
tgt = func(mat)
out = nanfunc(mat)

assert_almost_equal(out, tgt)
if dtype == "O":
assert type(out) is type(tgt)
else:
assert out.dtype == tgt.dtype

@pytest.mark.parametrize(
"nanfunc,func",
[(np.nanvar, np.var), (np.nanstd, np.std)],
ids=["nanvar", "nanstd"],
)
def test_nanfunc_ddof(self, dtype, nanfunc, func):
mat = self.mat.astype(dtype)
tgt = func(mat, ddof=1)
out = nanfunc(mat, ddof=1)

assert_almost_equal(out, tgt)
if dtype == "O":
assert type(out) is type(tgt)
else:
assert out.dtype == tgt.dtype


class SharedNanFunctionsTestsMixin:
Expand Down
0