Skip to content

BUG: undocumented astype("category").astype(str) type inconsistency between pandas 1.1 & 1.2 #41797

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
timlod opened this issue Jun 3, 2021 · 2 comments · Fixed by #42087
Closed
2 of 3 tasks
Labels
Bug Categorical Categorical Data Type Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@timlod
Copy link
Contributor

timlod commented Jun 3, 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

import numpy as np
import pandas as pd

print(pd.__version__)
df = pd.DataFrame(["a", "b", np.nan])
print("astype(str): \n", df.astype(str))
print("astype('category').astype(str): \n", df.astype("category").astype(str))

Output 1.1.3:

1.1.3
astype(str): 
      0
0    a
1    b
2  nan
astype('category').astype(str): 
      0
0    a
1    b
2  nan

Output 1.2.4:

1.2.4
astype(str): 
      0
0    a
1    b
2  nan
astype('category').astype(str): 
      0
0    a
1    b
2  NaN

Problem description

The above snippet run on version 1.1 (tested on 1.1.3) returns 'nan' strings of the converted categories, whereas on 1.2 (tested on 1.2.4) keeps np.nan objects. Tangentially, when using the pandas StringDtype conversion (.astype("string"), pandas 1.1 and 1.2 both keep a NAType after conversion from categorical to string.

It is not clear whether it is expected behaviour that this conversion keeps np.nan, or whether it should return a 'nan' string. The former would mean that this is now correct, the latter would mean there is a regression.

This issue is probably related to #37355 and #25353

Expected Output

Document what behaviour is desired.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 2cb9652
python : 3.8.2.final.0
python-bits : 64
OS : Linux
OS-release : 4.19.0-14-amd64
Version : #1 SMP Debian 4.19.171-2 (2021-01-30)
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.2.4
numpy : 1.20.2
pytz : 2021.1
dateutil : 2.8.1
pip : 21.1.2
setuptools : 54.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.22.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : 0.8.7
fastparquet : None
gcsfs : None
matplotlib : 3.4.1
numexpr : None
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : 4.0.0
pyxlsb : None
s3fs : None
scipy : 1.6.2
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : None
numba : 0.53.1

Versions are identical except for one run at pandas 1.1.3, the other at 1.2.4.

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

This issue is probably related to #37355 and #25353

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

@simonjayhawkins simonjayhawkins added this to the 1.2.5 milestone Jun 3, 2021
@simonjayhawkins simonjayhawkins added Categorical Categorical Data Type 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 Jun 3, 2021
@simonjayhawkins
Copy link
Member

@pandas-dev/pandas-core if any one gets a chance to look. This is now the only open issue on 1.2.5 milestone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Categorical Categorical Data Type Regression Functionality that used to work in a prior pandas version
Projects
None yet
2 participants