Skip to content

Control "Mean of empty slice" RunTimeWarning with np.errstate #10709

@JonathanShor

Description

@JonathanShor

As there already exists a mechanism in numpy to manage warnings for possibly anomalous inputs, it seems odd that it doesn't work for all warnings. For example, nanmean raises a warning if given a slice of all nan, and apparently the only way to silence it is to catch it with the python warnings mechanism. np.errstate, despite claiming to handle divide by zero errors, does not:

Python 3.6.2 
In [1]: import numpy as np
In [2]: np.__version__
Out[2]: '1.13.1'

In [3]: allnan = np.array([np.nan, np.nan])
In [4]: with np.errstate(divide='ignore', invalid='ignore'):
   ...:     print(np.nanmean(allnan))
   ...:     
/Users/jonathanshor/anaconda2/envs/Python3/bin/ipython:2: RuntimeWarning: Mean of empty slice
  if __name__ == '__main__':
nan

I would suggest this could fall under the divide case, but invalid seems plausible as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions