-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
BUG: groupby.sum() is inconsistent with df.sum() for large integers #34681
Comments
Thanks for the report. There's a cast to float64
I think i occurs somewhere in
@tom-weiss are you interested in debugging any further? |
Oh, perhaps it's as simple as us just not having an int64-dtype |
At first glance seems logical; not sure why it's not there to begin with |
I am happy to help debug and assist as I can but am entirely unfamiliar with the inner workings of the pandas codebase. |
The line of interest is here: pandas/pandas/_libs/groupby.pyx Line 440 in 6efb0b2
I don't know the history as to why only complex / floating are listed here. This may be better served just using Cython.numeric instead of a custom fused type In any case if you wanted to try, I would suggest adding a test for the issue in pandas/tests/groupby/aggregate/test_aggregate.py and changing the lines above as described to see if you can get it to work |
So the issue appears to be how to handle NAN values. It's easy enough to switch to a numeric but then I guess issues on line 482:
I can't assign type 'long double' to 'uint64_t' |
You can qualify the preceding condition with something like |
So it's quite easy to fix this to work with Integers but the required pandas functionality with max_count in the groupby() method is to return NaN for values that don't reach min_count(): Does anyone know how I can return the NAN in the case where I want to do integer arithmetic? |
we always cast to float64 because sum can overflow int64 with large inputs ; we do try to cast back if possible i think |
@jreback yes I can see the results cast back to int64 but we're getting rounding errors that are throwing the results out. These are not present in the standard df.sum() path. |
the soln of course is to add a groupby int64 sum kernel |
Is there any easy way to make int64 nullable in cython? The _groupby_add function in pandas/_libs/groupby.pyx uses numpy types which are not-nullable for integers. I'm not clear how I could implement the new nullable integer type into this: My curent code is here:
|
nullables are not supported directly in cython, this can be worked around by actually passing the mask to the cython functions, this is will require some reworking of the code and calling conventions. |
For this specific case (int64), we might not need to worry about However a masked groupby sum would be helpful for the nullable integer dtype. |
If someone can point me in the right direction I am happy to try and fix or help validate a fix. |
OP example now has |
take |
Update: It's been slow going as it took me a while to build an environment where I could perform git bisect for versions 1.x. I have results from the bisect now that say that the bug has been fixed between d3f0856 and 1ce1c3c. I will double check that result, write some tests, and hopefully submit a PR soon. |
@deejlucas happy to take on this issue if you aren't working on it |
take |
[ x ] I have checked that this issue has not already been reported.
[ x ] I have confirmed this bug exists on the latest version of pandas.
(optional) I have confirmed this bug exists on the master branch of pandas.
Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.
Code Sample, a copy-pastable example
Problem description
groupby.sum() results currently provide different results for df.sum() results for large integers.
It is expected that they should provide the same results.
Expected Output
When grouping by a colum with a single value, the groupby().sum() result should always equal the df.sum() result()
Output of
pd.show_versions()
INSTALLED VERSIONS
commit : None
python : 3.7.7.final.0
python-bits : 64
OS : Darwin
OS-release : 19.5.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8
pandas : 1.0.4
numpy : 1.18.2
pytz : 2019.3
dateutil : 2.8.1
pip : 19.0.3
setuptools : 40.8.0
Cython : 0.29.16
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.8.3 (dt dec pq3 ext lo64)
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 : 2.6.2
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : 0.4.2
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
xlsxwriter : None
numba : None
Runing pandas None
Process finished with exit code 0
The text was updated successfully, but these errors were encountered: