Skip to content

BUG: astype from categorical to np.int32 conversion returns np.int64 in pandas 1.2.0 and 1.2.0 #39402

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
wchang778 opened this issue Jan 25, 2021 · 2 comments · Fixed by #39615
Closed
1 task
Labels
Bug Categorical Categorical Data Type Dtype Conversions Unexpected or buggy dtype conversions Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@wchang778
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.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

# Your code here
# numpy=1.18.5, pandas=1.2.x (has issue), pandas = 1.1.5 (no issue)
import pandas as pd
import numpy as pd

df  = pd.DataFrame(data={'column1': [2.0, -1.0, 3.0]})
df.column1 = df.column1.astype('category')
df.column1 = df.column1.astype(np.int32)
assert df.column1.dtype.type == np.int32 # fails in pandas 1.2.x, returns np.int64

Problem description

[this should explain why the current behaviour is a problem and why the expected output is a better solution]
in pandas 1.1.5 astype correctly returning np.int32 as the column1 dtype.
This is expected as we are trying to convert this categorical col into np.int32.
Now it returns np.int64 in pandas 1.2.x

Expected Output

no assertion error

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 3e89b4c
python : 3.8.5.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.17763
machine : AMD64
processor : Intel64 Family 6 Model 79 Stepping 1, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : English_United States.1252

pandas : 1.2.0
numpy : 1.18.5
pytz : 2020.5
dateutil : 2.8.1
pip : 20.1.1
setuptools : 51.3.3.post20210118
Cython : None
pytest : 5.4.3
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 1.3.7
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.19.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 2.0.0
pyxlsb : None
s3fs : None
scipy : 1.6.0
sqlalchemy : 1.3.22
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

[paste the output of pd.show_versions() here leaving a blank line after the details tag]
pd.show_versions()

@wchang778 wchang778 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 25, 2021
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Jan 26, 2021
@simonjayhawkins
Copy link
Member

Thanks @wchang778 for the report

in pandas 1.1.5 astype correctly returning np.int32 as the column1 dtype.
This is expected as we are trying to convert this categorical col into np.int32.
Now it returns np.int64 in pandas 1.2.x

first bad commit: [cc957d1] PERF/ENH: add fast astyping for Categorical (#37355) cc @arw2019

@simonjayhawkins simonjayhawkins added Categorical Categorical Data Type Dtype Conversions Unexpected or buggy dtype conversions Regression Functionality that used to work in a prior pandas version and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 26, 2021
@simonjayhawkins simonjayhawkins added this to the 1.2.2 milestone Jan 26, 2021
@arw2019
Copy link
Member

arw2019 commented Jan 26, 2021

I'll take a look. I remember running into these issues in the Categorical.astype refactor

in the meantime astype('int').astype(np.int32) is a workaround I think though obviously you shouldn't have to do that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Categorical Categorical Data Type Dtype Conversions Unexpected or buggy dtype conversions 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