Thanks to visit codestin.com
Credit goes to github.com

Skip to content

numpy/core/_methods.py:57: RuntimeWarning: invalid value encountered in double_scalars #2157

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
mmokrejs opened this issue Jun 27, 2013 · 2 comments

Comments

@mmokrejs
Copy link

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)
@WeatherGod
Copy link
Member

Perhaps you would be interested in MaskedArrays? Take a peek at
np.ma.masked_invalid().

Cheers!
Ben Root
On Jun 27, 2013 8:29 AM, "mmokrejs" [email protected] wrote:

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 #961https://github.com/matplotlib/matplotlib/issues/961but 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)


Reply to this email directly or view it on GitHubhttps://github.com//issues/2157
.

@tacaswell
Copy link
Member

Closing as this should have been reported to numpy, not to mpl.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants