Skip to content

BUG: Replace changes the dtype of other columns #46634

Closed
@bchen1116

Description

@bchen1116

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    RegressionFunctionality that used to work in a prior pandas versionreplacereplace method

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions