Skip to content

BUG: Groupby: Int-Datatype (of group-by-columns) always casted to int64 #34598

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
2 of 3 tasks
Mueller2-Patrick opened this issue Jun 5, 2020 · 4 comments
Closed
2 of 3 tasks
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions Groupby

Comments

@Mueller2-Patrick
Copy link

Mueller2-Patrick commented Jun 5, 2020

  • 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

import pandas as pd
import numpy as np

df = pd.DataFrame({'col1':[1,2,3],'col2':[10,11,12]})
df['col1'] = df['col1'].astype(np.int8)

print(df.dtypes)

print(df.groupby('col1', as_index=False)['col2'].count().dtypes)

Problem description

Before the groupby: The dtype of col1 is np.int8:
Output of

print(df.dtypes):

col1     int8
col2    int64
dtype: object

After the groupby the dtype of col1 is np.int64
Output of

print(df.groupby('col1', as_index=False)['col2'].count().dtypes):

col1    int64
col2    int64
dtype: object

Expected Output

print(df.groupby('col1', as_index=False)['col2'].count().dtypes) gives output:

col1    int8
col2    int64
dtype: object

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.1.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 9, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None
pandas : 1.0.4
numpy : 1.17.4
pytz : 2019.3
dateutil : 2.8.1
pip : 10.0.1
setuptools : 45.2.0
Cython : None
pytest : 5.4.1
hypothesis : None
sphinx : 2.3.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.10.3
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.0.3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 0.12.0
pytables : None
pytest : 5.4.1
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.12
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@Mueller2-Patrick Mueller2-Patrick added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 5, 2020
@Mueller2-Patrick Mueller2-Patrick changed the title BUG: Groupby: Int-Datatype always casted to int64 BUG: Groupby: Int-Datatype (of group-by-columns) always casted to int64 Jun 8, 2020
@rhshadrach
Copy link
Member

rhshadrach commented Jun 8, 2020

Thanks for reporting this - it seems to me this is expected, datatypes may need to change to hold the correct result. For example, taking the mean of a Boolean/integer should result in float. Taking the count of a column with say 10k rows with dtype int8, changing the datatype to int64 so that the correct result can be stored seems preferable.

@rhshadrach rhshadrach added Groupby Dtype Conversions Unexpected or buggy dtype conversions labels Jun 8, 2020
@rhshadrach
Copy link
Member

Ahh, I missed that it was the grouping column that is changing. That changes things.

@rhshadrach
Copy link
Member

rhshadrach commented Jun 11, 2020

Even when using as_index=False, internally the unique values for the groupby are stored as an Index. I think that makes this a duplicate of #16404. Perhaps a conversion could be done after the operation is computed in this case. But then, with 16404 open, we would have two different results when using as_index=True/False.

@TomAugspurger
Copy link
Contributor

Closing as a duplicate of #16404. To support this we'll need hashtables / algos that operate on all these integer dtypes.

@TomAugspurger TomAugspurger removed the Needs Triage Issue that has not been reviewed by a pandas team member label Sep 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions Groupby
Projects
None yet
Development

No branches or pull requests

3 participants