Description
I was annoyed by numpy giving me sometimes this weird message. It turned out to be related to average() while not to median(). I see a pull #961 but containing same warning messages in comment from 'jdh2358'.
Anyway, here is my testcase:
$ python
Python 2.7.3 (default, Apr 20 2013, 18:28:22)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import numpy as np
np.average([])
/usr/lib64/python2.7/site-packages/numpy/core/_methods.py:57: RuntimeWarning: invalid value encountered in double_scalars
ret = ret / float(rcount)
nan
np.median([])
nan
Sure, sometimes upstream code passes an empty list but I believe that is perfectly valid requirement. Please make your code more defensive. ;-)
Personally am not happy at all with those NaN values but if you insist, return NaN in case of average() like you already do for median(). I zap them myself with None.
A more general note on why I don't like NaN and how I got into the issue:
def fix_clean_lists(indexes, func, _in, _out):
"""Overwrite NaN values (numpy.float64) with None or keep the float for
average or round the float to integer for medians.
"""
if _in:
if func == 'median':
for _indexpos in indexes:
if _in[_indexpos]:
_v = np.median(_in[_indexpos])
try:
_rounded = int(_v)
except ValueError:
# get rid rid of NaN which is a numpy.float64
_out[_indexpos] = None
else:
_out[_indexpos] = _rounded
elif func == 'average':
for _indexpos in indexes:
if _in[_indexpos]:
# needed to get rid of:
# fix_clean_lists() entered here for func average for [[], [], [], [], []]
# /usr/lib64/python2.7/site-packages/numpy/core/_methods.py:57: RuntimeWarning: invalid value encountered in double_scalars
# ret = ret / float(rcount)
_v = np.average(_in[_indexpos])
try:
_avg = int(_v)
except ValueError:
# get rid rid of NaN which is a numpy.float64
_out[_indexpos] = None
else:
_out[_indexpos] = round(_v, 2)