Skip to content

BUG?: 1.2.5 astype('bool') converts NaNs in 'category' columns to True #42189

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
3 tasks done
tamargrey opened this issue Jun 22, 2021 · 1 comment
Closed
3 tasks done
Labels
Bug Categorical Categorical Data Type Dtype Conversions Unexpected or buggy dtype conversions Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate

Comments

@tamargrey
Copy link

  • 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

pd.Series(['a','b',None], dtype='category').astype('bool')

In 1.2.5 produces:

0    True
1    True
2    True
dtype: bool

In 1.2.4 produces:

0    True
1    True
2     NaN
dtype: object

Problem description

At first, I thought this was a bug, but the astype call producing object dtype only started showing up in 1.2.0, so I went back and checked 1.1.5, and 1.2.5 now matches what was on 1.1.5.

This makes me think that the behavior in 1.1.5 and 1.2.5 is the expected behavior, but I don't see a release note in 1.1.5 referencing this change back to the expected, so I just wanted to point this out incase it was an unknown regression/fix or a bug of some sort.

I do see that #41797 seems like a similar problem that also first showed up in 1.2.0 and was fixed in 1.2.5, so I'm wondering if they're related.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 7c48ff4
python : 3.8.2.final.0
python-bits : 64
OS : Darwin
OS-release : 19.6.0
Version : Darwin Kernel Version 19.6.0: Sun Jul 5 00:43:10 PDT 2020; root:xnu-6153.141.1~9/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.2.5
numpy : 1.20.3
pytz : 2021.1
dateutil : 2.8.1
pip : 21.1.2
setuptools : 41.2.0
Cython : None
pytest : 6.0.1
hypothesis : None
sphinx : 3.2.1
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.18.1
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : 2021.06.0
fastparquet : 0.5.0
gcsfs : None
matplotlib : 3.2.2
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 4.0.1
pyxlsb : None
s3fs : None
scipy : 1.6.3
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : 0.53.1

@tamargrey tamargrey added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 22, 2021
@tamargrey tamargrey changed the title BUG?: 1.2.5 astype('bool') converts NaNs in categorical columns to True BUG?: 1.2.5 astype('bool') converts NaNs in 'category' columns to True Jun 22, 2021
@simonjayhawkins
Copy link
Member

Thanks @tamargrey for the report.

At first, I thought this was a bug, but the astype call producing object dtype only started showing up in 1.2.0, so I went back and checked 1.1.5, and 1.2.5 now matches what was on 1.1.5.

I do see that #41797 seems like a similar problem that also first showed up in 1.2.0 and was fixed in 1.2.5, so I'm wondering if they're related.

yes. they are related. The regression reported in #41797 was not restricted to dtype=str.

This makes me think that the behavior in 1.1.5 and 1.2.5 is the expected behavior, but I don't see a release note in 1.1.5 referencing this change back to the expected, so I just wanted to point this out incase it was an unknown regression/fix or a bug of some sort.

The release note added in #42087 could perhaps have been more generic. Thanks for taking the time to highlight this. The behavior in 1.2.5 now matches 1.1.5 so we have persevered the behavior between versions so closing this.

@simonjayhawkins simonjayhawkins added Categorical Categorical Data Type Dtype Conversions Unexpected or buggy dtype conversions Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 22, 2021
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 Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Projects
None yet
Development

No branches or pull requests

2 participants