Skip to content

round method throws error if DataFrame includes Int64Dtype #31478

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
shouyu opened this issue Jan 31, 2020 · 5 comments
Closed

round method throws error if DataFrame includes Int64Dtype #31478

shouyu opened this issue Jan 31, 2020 · 5 comments
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Bug NA - MaskedArrays Related to pd.NA and nullable extension arrays

Comments

@shouyu
Copy link

shouyu commented Jan 31, 2020

Code Sample, a copy-pastable example if possible

>>> import pandas as pd
>>> s = pd.Series([1, 2, None], dtype="Int64")
>>> s.round(1)
AttributeError: 'float' object has no attribute 'rint'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/site-packages/pandas/core/series.py", line 2146, in round
    result = com.values_from_object(self).round(decimals)
TypeError: loop of ufunc does not support argument 0 of type float which has no callable rint method

Problem description

round method throws above error.
Expected behavior is same to int64 dtype and ignore NaN.

>>> s = pd.Series([1, 2], dtype="int64")
>>> s.round(1)
0    1
1    2
dtype: int64

Expected Output

>>> s = pd.Series([1, 2, None], dtype="Int64")
>>> s.round(1)
>>> s
0       1
1       2
2    <NA>
dtype: Int64

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.8.1.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.13-arch1-1
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.0
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 45.1.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@TomAugspurger
Copy link
Contributor

Do you know if np.round is a ufunc? If so, we could use that, since that will call np.round(s.array) and wrap it correctly.

However, we have

In [23]: np.round(s.array)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
AttributeError: 'int' object has no attribute 'rint'

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
<ipython-input-23-841bf54cafae> in <module>
----> 1 np.round(s.array)

<__array_function__ internals> in round_(*args, **kwargs)

~/Envs/pandas-dev/lib/python3.7/site-packages/numpy/core/fromnumeric.py in round_(a, decimals, out)
   3599     around : equivalent function; see for details.
   3600     """
-> 3601     return around(a, decimals=decimals, out=out)
   3602
   3603

<__array_function__ internals> in around(*args, **kwargs)

~/Envs/pandas-dev/lib/python3.7/site-packages/numpy/core/fromnumeric.py in around(a, decimals, out)
   3224
   3225     """
-> 3226     return _wrapfunc(a, 'round', decimals=decimals, out=out)
   3227
   3228

~/Envs/pandas-dev/lib/python3.7/site-packages/numpy/core/fromnumeric.py in _wrapfunc(obj, method, *args, **kwds)
     53     bound = getattr(obj, method, None)
     54     if bound is None:
---> 55         return _wrapit(obj, method, *args, **kwds)
     56
     57     try:

~/Envs/pandas-dev/lib/python3.7/site-packages/numpy/core/fromnumeric.py in _wrapit(obj, method, *args, **kwds)
     42     except AttributeError:
     43         wrap = None
---> 44     result = getattr(asarray(obj), method)(*args, **kwds)
     45     if wrap:
     46         if not isinstance(result, mu.ndarray):

TypeError: loop of ufunc does not support argument 0 of type int which has no callable rint method

numpy/numpy#13877 looks somewhat relevant...

@TomAugspurger TomAugspurger added NA - MaskedArrays Related to pd.NA and nullable extension arrays Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff labels Jan 31, 2020
@TomAugspurger TomAugspurger added this to the Contributions Welcome milestone Jan 31, 2020
@jbrockmendel
Copy link
Member

Do you know if np.round is a ufunc?

Looks like it isnt.

>>> np.round
<function round_ at 0x11c1eee60>

@MarcoGorelli MarcoGorelli modified the milestones: Contributions Welcome, 1.0.1 Feb 2, 2020
@TomAugspurger
Copy link
Contributor

@MarcoGorelli this is also failing on 0.25.3, so I don't think it's appropriate to target for 1.0.1


I think our choices here are to either

  1. Add an ExtensionArray.round method
  2. Implement __array_function__ on some of our EAs.

@TomAugspurger TomAugspurger modified the milestones: 1.0.1, Contributions Welcome Feb 3, 2020
@MarcoGorelli
Copy link
Member

Sorry, I have no recollection of having modified the milestone, I must have clicked by accident, will be careful to not do this again

@mroeschke mroeschke added the Bug label Apr 28, 2020
@mzeitlin11
Copy link
Member

Fixed with tests in #38844

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Bug NA - MaskedArrays Related to pd.NA and nullable extension arrays
Projects
None yet
Development

No branches or pull requests

6 participants