Skip to content

BUG: groupby with std aggregation of pandas integer dtype throws exception: 'IntegerArray' object has no attribute 'reshape' #37415

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
1 task
hannesdm opened this issue Oct 26, 2020 · 5 comments · Fixed by #37433
Labels
Groupby NA - MaskedArrays Related to pd.NA and nullable extension arrays Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@hannesdm
Copy link

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


Code Sample, a copy-pastable example

import pandas as pd
df = pd.DataFrame({'a': [1,1,1,2,2],'b': [1,2,3,4,5]})
df['b'] = df['b'].astype(pd.Int32Dtype())
df.groupby('a').agg('std') # Error 'IntegerArray' object has no attribute 'reshape'. The same error occurs with all pandas integer types

Problem description

For all integer pandas dtypes the code expects an IntegerArray to have the reshape function as is the case with numpy arrays.
It is possible that the error will also occur with other operations performed after a groupby, this has only been checked for 'std'.
The error occurs at
pandas\core\groupby\groupby.py", line 2491, in _get_cythonized_result
vals = vals.reshape((-1, 1))

Expected Output

The same output as with a numpy data type, for example above this would be:

a b
1 1.000000
2 0.707107

Output of pd.show_versions()

INSTALLED VERSIONS

commit : db08276
python : 3.6.9.final.0
python-bits : 64
OS : Linux
OS-release : 4.19.112+
Version : #1 SMP Thu Jul 23 08:00:38 PDT 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.3
numpy : 1.18.5
pytz : 2018.9
dateutil : 2.8.1
pip : 19.3.1
setuptools : 50.3.0
Cython : 0.29.21
pytest : 3.6.4
hypothesis : None
sphinx : 1.8.5
blosc : None
feather : 0.4.1
xlsxwriter : None
lxml.etree : 4.2.6
html5lib : 1.0.1
pymysql : None
psycopg2 : 2.7.6.1 (dt dec pq3 ext lo64)
jinja2 : 2.11.2
IPython : 5.5.0
pandas_datareader: 0.9.0
bs4 : 4.6.3
bottleneck : 1.3.2
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.2.2
numexpr : 2.7.1
odfpy : None
openpyxl : 2.5.9
pandas_gbq : 0.13.2
pyarrow : 0.14.1
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.19
tables : 3.4.4
tabulate : 0.8.7
xarray : 0.15.1
xlrd : 1.1.0
xlwt : 1.3.0
numba : 0.48.0

@hannesdm hannesdm added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 26, 2020
@jreback
Copy link
Contributor

jreback commented Oct 26, 2020

pls try on master

@phofl
Copy link
Member

phofl commented Oct 26, 2020

Can confirm, does not work on master. Same error.

Traceback (most recent call last):
  File "/home/developer/.config/JetBrains/PyCharm2020.2/scratches/scratch_4.py", line 7, in <module>
    df.groupby('a').agg('std')
  File "/home/developer/PycharmProjects/pandas/pandas/core/groupby/generic.py", line 951, in aggregate
    result, how = aggregate(self, func, *args, **kwargs)
  File "/home/developer/PycharmProjects/pandas/pandas/core/aggregation.py", line 557, in aggregate
    return obj._try_aggregate_string_function(arg, *args, **kwargs), None
  File "/home/developer/PycharmProjects/pandas/pandas/core/base.py", line 278, in _try_aggregate_string_function
    return f(*args, **kwargs)
  File "/home/developer/PycharmProjects/pandas/pandas/core/groupby/groupby.py", line 1517, in std
    return self._get_cythonized_result(
  File "/home/developer/PycharmProjects/pandas/pandas/core/groupby/groupby.py", line 2577, in _get_cythonized_result
    vals = vals.reshape((-1, 1))
AttributeError: 'IntegerArray' object has no attribute 'reshape'

@phofl phofl added Groupby and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 26, 2020
@jorisvandenbossche jorisvandenbossche added NA - MaskedArrays Related to pd.NA and nullable extension arrays Regression Functionality that used to work in a prior pandas version and removed Bug labels Oct 26, 2020
@jorisvandenbossche jorisvandenbossche added this to the 1.1.4 milestone Oct 26, 2020
@jorisvandenbossche
Copy link
Member

Turns out this is actually a regression compared to 1.0. It's not that groupby is already well supported for the nullable dtypes (meaning, a proper implementation preserving the dtypes), but it was working before, so would be good to keep this working (though inefficiently) until we properly support it.

@jorisvandenbossche
Copy link
Member

jorisvandenbossche commented Oct 26, 2020

Small fix at #37433

simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Oct 27, 2020
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Oct 27, 2020
@simonjayhawkins
Copy link
Member

Turns out this is actually a regression compared to 1.0

first bad commit: [c9144ca] CLN: Unify signatures in _libs.groupby (#34372)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Groupby NA - MaskedArrays Related to pd.NA and nullable extension arrays Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants