Skip to content

BUG: Replace changes the dtype of other columns #46634

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
bchen1116 opened this issue Apr 4, 2022 · 2 comments · Fixed by #46636
Closed
3 tasks done

BUG: Replace changes the dtype of other columns #46634

bchen1116 opened this issue Apr 4, 2022 · 2 comments · Fixed by #46636
Labels
Regression Functionality that used to work in a prior pandas version replace replace method
Milestone

Comments

@bchen1116
Copy link

Pandas version checks

  • 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 main branch of pandas.

Reproducible Example

import pandas as pd

df = pd.DataFrame(
    {
        "id": pd.Series([5, 4, 3, 2], dtype="float64"),
        "col": pd.Series(["b", "b", "b", "d"], dtype="category"),
    }
)
df = df.replace({3: None})

Issue Description

In version 1.4.1, df.replace would change the id column to the object type and leave col with the category dtype. However, in pandas 1.4.2, this now replaces both the dtypes with object.

Expected Behavior

I would expect all columns that are unimpacted/unchanged by replace to continue to exhibit the same dtype as previously assigned.

Installed Versions

INSTALLED VERSIONS

commit : 4bfe3d0
python : 3.8.0.final.0
python-bits : 64
OS : Darwin
OS-release : 21.1.0
Version : Darwin Kernel Version 21.1.0: Wed Oct 13 17:33:23 PDT 2021; root:xnu-8019.41.5~1/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.4.2
numpy : 1.21.2
pytz : 2021.1
dateutil : 2.8.2
pip : 22.0.4
setuptools : 41.2.0
Cython : 0.29.28
pytest : 7.0.1
hypothesis : None
sphinx : 4.2.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.31.1
pandas_datareader: None
bs4 : 4.10.0
bottleneck : None
brotli : None
fastparquet : None
fsspec : 2021.08.1
gcsfs : None
markupsafe : 2.0.1
matplotlib : 3.5.1
numba : 0.55.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 5.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.7.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

@bchen1116 bchen1116 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 4, 2022
@MarcoGorelli MarcoGorelli added Regression Functionality that used to work in a prior pandas version and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 4, 2022
@simonjayhawkins
Copy link
Member

Thanks @bchen1116 for the report.

i'm assuming that the regression was caused by #46404 and have opened #46636 on that assumption.

@simonjayhawkins simonjayhawkins added the replace replace method label Apr 4, 2022
@simonjayhawkins simonjayhawkins added this to the 1.4.3 milestone Apr 4, 2022
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Apr 5, 2022
@simonjayhawkins
Copy link
Member

i'm assuming that the regression was caused by #46404 and have opened #46636 on that assumption.

can confirm

first bad commit: [18529a0] Backport PR #46404: REGR: DataFrame.replace when the replacement value was explicitly None (#46441)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Regression Functionality that used to work in a prior pandas version replace replace method
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants