Skip to content

BUG: numpy RuntimeWarning with Series.round() #14197

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
xflr6 opened this issue Sep 10, 2016 · 17 comments · Fixed by #25432
Closed

BUG: numpy RuntimeWarning with Series.round() #14197

xflr6 opened this issue Sep 10, 2016 · 17 comments · Fixed by #25432
Labels
Bug good first issue Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Milestone

Comments

@xflr6
Copy link
Contributor

xflr6 commented Sep 10, 2016

Maybe needs wrapping in numpy.errstate (in pandas)?

>>> import pandas as pd
>>> pd.__version__
u'0.19.0rc1'
>>> pd.Series([pd.np.nan]).round()

Warning (from warnings module):
  File "C:\Program Files\Python27\lib\site-packages\pandas\core\series.py", line 1340
    result = _values_from_object(self).round(decimals)
RuntimeWarning: invalid value encountered in rint
0   NaN
dtype: float64

Same for DataFrame.round().

@jreback
Copy link
Contributor

jreback commented Sep 10, 2016

need to mask the values before rounding (iow not null ones)

@jreback jreback added Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Difficulty Novice labels Sep 10, 2016
@jreback jreback added this to the 0.19.0 milestone Sep 10, 2016
@tom-bird
Copy link
Contributor

I'll have a look and submit a PR

@tom-bird
Copy link
Contributor

I can't seem to replicate the warning..

Running 2.7.12 and the latest dev version of pandas

@jreback
Copy link
Contributor

jreback commented Sep 15, 2016

hmm, maybe this is on an older version of numpy.

@xflr6 can you pd.show_versions() where you got that error

@xflr6
Copy link
Contributor Author

xflr6 commented Sep 17, 2016

Sure:

>>> import pandas as pd
>>> pd.Series([pd.np.nan]).round()

Warning (from warnings module):
  File "C:\Program Files\Python27\lib\site-packages\pandas\core\series.py", line 1340
    result = _values_from_object(self).round(decimals)
RuntimeWarning: invalid value encountered in rint
0   NaN
dtype: float64
>>> pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.12.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 42 Stepping 7, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: de_DE.cp1252

pandas: 0.19.0rc1
nose: 1.3.7
pip: 8.1.2
setuptools: 27.2.0
Cython: 0.24.1
numpy: 1.11.1
scipy: 0.18.0
statsmodels: 0.8.0rc1
xarray: None
IPython: 5.1.0
sphinx: 1.4.6
patsy: 0.4.1
dateutil: 2.5.3
pytz: 2016.6.1
blosc: None
bottleneck: 1.1.0
tables: None
numexpr: 2.6.1
matplotlib: 1.5.3
openpyxl: 2.4.0
xlrd: 1.0.0
xlwt: None
xlsxwriter: None
lxml: 3.6.4
bs4: 4.5.1
html5lib: 0.999999999
httplib2: 0.9.2
apiclient: None
sqlalchemy: 1.0.15
pymysql: None
psycopg2: 2.6.2 (dt dec pq3 ext lo64)
jinja2: 2.8
boto: None
pandas_datareader: None

In some environments, warnings are silenced, try this before:

>>> import warnings
>>> warnings.simplefilter('error')

@jreback jreback modified the milestones: 0.19.0, Next Major Release Sep 28, 2016
@xflr6
Copy link
Contributor Author

xflr6 commented Oct 3, 2016

Seems to be a Windows-specific upstream issue: numpy/numpy#7882

>>> import numpy as np
>>> np.ma.masked_invalid([np.nan]).round()

Warning (from warnings module):
  File "C:\Program Files\Python27\lib\site-packages\numpy\ma\core.py", line 5192
    result = self._data.round(decimals=decimals, out=out).view(type(self))
RuntimeWarning: invalid value encountered in rint
masked_array(data = [--],
             mask = [ True],
       fill_value = 1e+20)
>>> np.rint(np.nan)

Warning (from warnings module):
  File "__main__", line 2
RuntimeWarning: invalid value encountered in rint
nan

@gandhis1
Copy link
Contributor

Correct me if I'm wrong, but doesn't Pandas usually ignore NA values in most calculations? So if you're trying to do some math, like rounding, what comes in as an NA would go out as an NA. Not sure why a warning is necessary here when it isn't necessary for multiplication, division, etc.

@jreback
Copy link
Contributor

jreback commented Jan 31, 2017

but hats exactly the point; pandas needs a change to mask these values before sending to numpy; pull requests are welcome

@mikeyshulman
Copy link

I cannot reproduce this error at HEAD. is it still open?

@xflr6
Copy link
Contributor Author

xflr6 commented Jul 12, 2018

Still open for me on Python 2, not on Python 3 though, see below.

In [1]: import pandas as pd
In [2]: import warnings
   ...: warnings.simplefilter('error')
   ...: pd.Series([pd.np.nan]).round()

RuntimeWarningTraceback (most recent call last)
<ipython-input-2-0aec18ee80a1> in <module>()
      1 import warnings
      2 warnings.simplefilter('error')
----> 3 pd.Series([pd.np.nan]).round()

c:\programme\python27\lib\site-packages\pandas\core\series.pyc in round(self, decimals, *args, **kwargs)
   1826         """
   1827         nv.validate_round(args, kwargs)
-> 1828         result = com._values_from_object(self).round(decimals)
   1829         result = self._constructor(result, index=self.index).__finalize__(self)
   1830 

RuntimeWarning: invalid value encountered in rint

In [3]: pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.15.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 42 Stepping 7, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.23.3
pytest: 3.6.3
pip: 10.0.1
setuptools: None
Cython: 0.28.4
numpy: 1.15.0rc1
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 5.7.0
sphinx: 1.7.5
patsy: None
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: 1.2.1
tables: None
numexpr: 2.6.5
feather: None
matplotlib: 2.2.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.9
pymysql: None
psycopg2: 2.7.5 (dt dec pq3 ext lo64)
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: 0.5.0
pandas_datareader: None
In [1]: import pandas as pd
   ...: import warnings
   ...: warnings.simplefilter('error')
   ...: pd.Series([pd.np.nan]).round()
Out[1]: 
0   NaN
dtype: float64

In [2]: pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.7.0.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 42 Stepping 7, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.23.3
pytest: 3.6.3
pip: 10.0.1
setuptools: None
Cython: 0.28.4
numpy: 1.15.0rc1
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 6.4.0
sphinx: 1.7.5
patsy: None
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: 1.2.1
tables: None
numexpr: 2.6.5
feather: None
matplotlib: 2.2.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 1.0.1
sqlalchemy: None
pymysql: None
psycopg2: 2.7.5 (dt dec pq3 ext lo64)
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: 0.4.1
pandas_datareader: None

@simonjayhawkins
Copy link
Member

if we add a test and skip for py2, could we close this issue?

@jreback
Copy link
Contributor

jreback commented Feb 23, 2019

is this still showing in master? i thought this was fixed a long time ago

@simonjayhawkins
Copy link
Member

is this still showing in master? i thought this was fixed a long time ago

on py2 windows, pandas 0.24.1..

Python 2.7.15 |Anaconda, Inc.| (default, Feb 21 2019, 11:55:13) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> pd.__version__
u'0.24.1'
>>> import warnings
>>> warnings.simplefilter('error')
>>> pd.Series([pd.np.nan]).round()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\simon\Anaconda3\envs\pandas-dev2\lib\site-packages\pandas\core\series.py", line 1999, in round
    result = com.values_from_object(self).round(decimals)
RuntimeWarning: invalid value encountered in rint

i've not got a build env set for py2 so can't check master.

@jreback
Copy link
Contributor

jreback commented Feb 23, 2019

ok i think the problem is that we need to change how round is implemented
need to fill the NaNs round then refill with NaNs

alternatively u can call the numpy function and suppress the warning

@simonjayhawkins
Copy link
Member

but if it's just py2, then moving forward this is a won't fix? so just test for py3 and close issue?

@jreback
Copy link
Contributor

jreback commented Feb 23, 2019

yes that’s fine - is there a warning on py3?

@simonjayhawkins
Copy link
Member

works on my machine! if we add a test we'll know if there's an issue with other platforms.

@jreback jreback removed this from the Contributions Welcome milestone Feb 27, 2019
@jreback jreback added this to the 0.25.0 milestone Feb 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug good first issue Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants