Skip to content
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

BUG: v1.3.0 Introduces Unexpected Retyping of Categorical Column with NaN inside #43996

Open
3 tasks done
Tracked by #7
DamianBarabonkovQC opened this issue Oct 12, 2021 · 1 comment
Open
3 tasks done
Tracked by #7
Labels
Bug Categorical Categorical Data Type Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Needs Tests Unit test(s) needed to prevent regressions

Comments

@DamianBarabonkovQC
Copy link
Contributor

DamianBarabonkovQC commented Oct 12, 2021

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the master branch of pandas.

Reproducible Example

# Bug in v1.3.0 and later
import pandas as pd
import numpy as np

print(pd.__version__)

X = pd.DataFrame({
    "a": pd.Categorical([np.nan], [1]),
    "b": [1]  # comment this out and it will return a categorical in 1.3.0
})

mask = X["a"].isna()
X.loc[mask, "a"] = np.array([1])
print(X["a"].dtype)
# int, but should remain a category

Issue Description

In the above reproducible example, using pandas v1.3.0 will return a int64 type. However, the column was initially a categorical type.

The behaviour is very esoteric. In v1.2.5, this code works properly and keeps the categorical type. If the column "b" is removed, then it works correctly in v1.3.0. If the np.nan is replaced with a value such as 1 in the "a" column, this code works correctly.

After performing a git bisect, the offending commit is 8022743. Its parent (4ecba51) returns a category. Something in 802274... introduces this bug.

Expected Behavior

The last print returns a category type, not an int64 which is the correct and expected behaviour.

# Works correctly in v1.2.5
import pandas as pd
import numpy as np

print(pd.__version__)

X = pd.DataFrame({
    "a": pd.Categorical([np.nan], [1]),
    "b": [1]
})

mask = X["a"].isna()
X.loc[mask, "a"] = np.array([1])
print(X["a"].dtype)
# category

Installed Versions

INSTALLED VERSIONS

commit : 8022743
python : 3.9.7.final.0
python-bits : 64
OS : Darwin
OS-release : 20.6.0
Version : Darwin Kernel Version 20.6.0: Wed Jun 23 00:26:27 PDT 2021; root:xnu-7195.141.2~5/RELEASE_ARM64_T8101
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 1.3.0.dev0+435.g80227439db
numpy : 1.21.2
pytz : 2021.3
dateutil : 2.8.2
pip : 21.3
setuptools : 58.2.0
Cython : 0.29.24
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
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 : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@DamianBarabonkovQC DamianBarabonkovQC added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 12, 2021
DamianBarabonkovQC pushed a commit to DamianBarabonkovQC/pandas that referenced this issue Oct 12, 2021
DamianBarabonkovQC added a commit to DamianBarabonkovQC/pandas that referenced this issue Oct 12, 2021
@rhshadrach rhshadrach added the Categorical Categorical Data Type label Oct 16, 2021
@mroeschke mroeschke added 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 Oct 16, 2021
@jreback jreback added this to the 1.4 milestone Oct 21, 2021
@jreback jreback added the Needs Tests Unit test(s) needed to prevent regressions label Oct 21, 2021
@simonjayhawkins
Copy link
Member

removing milestone

@simonjayhawkins simonjayhawkins removed this from the 1.4 milestone Jan 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Categorical Categorical Data Type Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

No branches or pull requests

5 participants