Skip to content

BUG: cython groupby cummin/cummax assumes groups ordered #15635

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
adbull opened this issue Mar 9, 2017 · 4 comments
Closed

BUG: cython groupby cummin/cummax assumes groups ordered #15635

adbull opened this issue Mar 9, 2017 · 4 comments
Milestone

Comments

@adbull
Copy link
Contributor

adbull commented Mar 9, 2017

Code Sample, a copy-pastable example if possible

>>> import pandas as pd
>>> pd.DataFrame(dict(a=[1,2,1], b=[2,1,1])).groupby('a').b.cummax()
0    2
1    1
2    1
Name: b, dtype: int64

Problem description

The current github version of pandas has cython implementations of groupby.cummin and groupby.cummax which give the wrong answer when the groups are unordered. (See #15048, 0fe491d.)

Expected Output

0    2
1    1
2    2
Name: b, dtype: int64

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.5.2.final.0
python-bits: 64
OS: Linux
OS-release: 4.9.8-100.fc24.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: C
LANG: C
LOCALE: None.None

pandas: 0.19.0+575.g5667a3a
pytest: 3.0.5
pip: 9.0.1
setuptools: 27.2.0
Cython: 0.25.2
numpy: 1.11.3
scipy: 0.18.1
xarray: 0.9.1
IPython: 4.2.0
sphinx: 1.5.1
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2016.10
blosc: None
bottleneck: 1.2.0
tables: 3.3.0
numexpr: 2.6.2
feather: None
matplotlib: 2.0.0
openpyxl: 2.4.1
xlrd: 1.0.0
xlwt: 1.2.0
xlsxwriter: 0.9.6
lxml: 3.7.2
bs4: 4.5.3
html5lib: 0.999
sqlalchemy: 1.1.5
pymysql: None
psycopg2: None
jinja2: 2.9.4
s3fs: None
pandas_gbq: None
pandas_datareader: None

@jreback
Copy link
Contributor

jreback commented Mar 9, 2017

looks like a bug in how the maxval is updated

it's not about the group ordering or not but a straight up bug

pr to fix is welcome

@jreback
Copy link
Contributor

jreback commented Mar 9, 2017

cc @mroeschke

@adbull
Copy link
Contributor Author

adbull commented Mar 9, 2017

Ah, agreed. I think it happens to get the right answer on ordered groups, which is why the tests passed.

Suspect it just requires the following, plus tests of course?

diff --git a/pandas/_libs/algos_groupby_helper.pxi.in b/pandas/_libs/algos_groupby_helper.pxi.in
index 9552b42..c86a4b4 100644
--- a/pandas/_libs/algos_groupby_helper.pxi.in
+++ b/pandas/_libs/algos_groupby_helper.pxi.in
@@ -603,7 +603,7 @@ def group_cummin_{{name}}(ndarray[{{dest_type2}}, ndim=2] out,
     """
     cdef:
         Py_ssize_t i, j, N, K, size
-        {{dest_type2}} val, min_val = 0
+        {{dest_type2}} val
         ndarray[{{dest_type2}}, ndim=2] accum
         int64_t lab
 
@@ -629,8 +629,7 @@ def group_cummin_{{name}}(ndarray[{{dest_type2}}, ndim=2] out,
                 if val == val:
                 {{endif}}
                     if val < accum[lab, j]:
-                        min_val = val
-                    accum[lab, j] = min_val
+                        accum[lab, j] = val
                     out[i, j] = accum[lab, j]
 
 
@@ -645,7 +644,7 @@ def group_cummax_{{name}}(ndarray[{{dest_type2}}, ndim=2] out,
     """
     cdef:
         Py_ssize_t i, j, N, K, size
-        {{dest_type2}} val, max_val = 0
+        {{dest_type2}} val
         ndarray[{{dest_type2}}, ndim=2] accum
         int64_t lab
 
@@ -670,8 +669,7 @@ def group_cummax_{{name}}(ndarray[{{dest_type2}}, ndim=2] out,
                 if val == val:
                 {{endif}}
                     if val > accum[lab, j]:
-                        max_val = val
-                    accum[lab, j] = max_val
+                        accum[lab, j] = val
                     out[i, j] = accum[lab, j]
 
 {{endfor}}

@jreback
Copy link
Contributor

jreback commented Mar 9, 2017

yep that might do it.

AnkurDedania pushed a commit to AnkurDedania/pandas that referenced this issue Mar 21, 2017
closes pandas-dev#15635

Author: Matt Roeschke <[email protected]>

Closes pandas-dev#15642 from mroeschke/fix_15635 and squashes the following commits:

b92b81a [Matt Roeschke] BUG: Incorrect value updating for groupby.cummin/max (pandas-dev#15635)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants