Skip to content
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

REGR: ExtensionArray aggregation on non-numeric types fails #38980

Closed
3 tasks done
BryanCutler opened this issue Jan 5, 2021 · 5 comments · Fixed by #38982
Closed
3 tasks done

REGR: ExtensionArray aggregation on non-numeric types fails #38980

BryanCutler opened this issue Jan 5, 2021 · 5 comments · Fixed by #38982
Labels
ExtensionArray Extending pandas with custom dtypes or arrays. Groupby Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@BryanCutler
Copy link
Contributor

BryanCutler commented Jan 5, 2021

  • I have checked that this issue has not already been reported.

  • 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.


Code Sample, a copy-pastable example

df = pd.DataFrame({'key': ['a', 'a', 'b', 'b'], 'val': ['a', 'b', 'c', 'd']}, dtype="string")
df.groupby("key").agg({'val': 'first'})

Expected:

    val
key    
a     a
b     c

Actual fails with error:

lib/python3.8/site-packages/pandas/core/groupby/ops.py in _ea_wrap_cython_operation(self, kind, values, how, axis, min_count, **kwargs)
    538             return result
    539 
--> 540         raise NotImplementedError(values.dtype)
    541

NotImplementedError: string

Problem description

Calling groupby on an ExtensionArray and aggregation on a string column fails with the error: NotImplementedError: string. This is because an optimized cython aggregation is attempted and when fails, the error string is improperly checked and does not fallback to a non-cython aggregation.

This was found with the extension arrays from https://github.com/CODAIT/text-extensions-for-pandas/blob/master/text_extensions_for_pandas/io/bert.py#L217 and full error log can be seen here https://github.com/CODAIT/text-extensions-for-pandas/pull/157/checks?check_run_id=1646927381#step:5:1438

Expected Output

Pandas should handle the NotImplementedError and fallback to a non-cython aggregation implementation.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 3e89b4c
python : 3.8.6.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-58-generic
Version : #64-Ubuntu SMP Wed Dec 9 08:16:25 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.2.0
numpy : 1.19.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.4
setuptools : 49.6.0.post20201009
Cython : None
pytest : 6.1.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.18.1
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.2
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 2.0.0
pyxlsb : None
s3fs : None
scipy : 1.5.2
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@BryanCutler BryanCutler added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 5, 2021
@BryanCutler
Copy link
Contributor Author

I can work on a patch to fix this

@jorisvandenbossche
Copy link
Member

Thanks for the report!
Yes, this is caused by the refactoring in groupby, it was working before correctly, so labeling as regression (and targeting 1.2.1)

@jorisvandenbossche jorisvandenbossche added ExtensionArray Extending pandas with custom dtypes or arrays. Groupby Regression Functionality that used to work in a prior pandas version and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 6, 2021
@jorisvandenbossche jorisvandenbossche added this to the 1.2.1 milestone Jan 6, 2021
@jreback
Copy link
Contributor

jreback commented Jan 6, 2021

pls edit the top include the full example and not just the link

@jorisvandenbossche
Copy link
Member

There is a reproducible example now at #38982 (comment)

simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Jan 6, 2021
@simonjayhawkins
Copy link
Member

first bad commit: [40ca2b9] REF: implement _ea_wrap_cython_operation (#38162) cc @jbrockmendel

@BryanCutler BryanCutler changed the title BUG: ExtensionArray aggregation on non-numeric types fails REGR: ExtensionArray aggregation on non-numeric types fails Jan 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ExtensionArray Extending pandas with custom dtypes or arrays. Groupby Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants