Skip to content

BUG: Error when using nanmax() and nanmin() on an object array with an axis specified. #9008

@WarrenWeckesser

Description

@WarrenWeckesser

This was the underlying cause of a problem reported on stackoverflow: http://stackoverflow.com/questions/43659827/numpy-error-when-specifying-axis-in-nanmax-while-nansum-works-an-the-same-case

The error is raised when applying nanmax() or nanmin() to an array with object data type and specifying an axis.

Here's the example from my answer:

In [2]: import numpy as np

In [3]: np.__version__
Out[3]: '1.13.0.dev0+bca7922'

In [4]: a = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=object)

In [5]: np.nanmax(a, axis=0)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-a020f98a2536> in <module>()
----> 1 np.nanmax(a, axis=0)

/Users/warren/miniconda3numpy/lib/python3.5/site-packages/numpy-1.13.0.dev0+bca7922-py3.5-macosx-10.6-x86_64.egg/numpy/lib/nanfunctions.py in nanmax(a, axis, out, keepdims)
    343         # Fast, but not safe for subclasses of ndarray
    344         res = np.fmax.reduce(a, axis=axis, out=out, **kwargs)
--> 345         if np.isnan(res).any():
    346             warnings.warn("All-NaN slice encountered", RuntimeWarning, stacklevel=2)
    347     else:

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

There is no error when the axis is not given:

In [6]: np.nanmax(a)
Out[6]: 4.0

nansum() handles the axis without an error:

In [7]: np.nansum(a, axis=0)
Out[7]: array([4.0, 6.0], dtype=object)

There are several other issues involving the nan-functions and object arrays, but I couldn't tell if this issue is a duplicate. Sorry for the noise if it is.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions