Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

BUG: pd.replace changes dtype when using a dict to replace values even when there are no matches #53539

Copy link
Copy link
Open
@sergiykhan

Description

@sergiykhan
Issue body actions

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(
    {'a': [1, 2], 'b': [3, 4]},
    dtype='object',
)

df1 = df.replace(
    {5: 0},
)

df2 = df.replace(
    to_replace=5,
    value=0,
)

print(df1.dtypes)
print(df2.dtypes)

Issue Description

pd.replace changes the dtype of the columns whether there is a match or not.

The output of the example above:

a    int64
b    int64
dtype: object
a    object
b    object
dtype: object

Expected Behavior

Expected output:

a    object
b    object
dtype: object
a    object
b    object
dtype: object

Installed Versions

INSTALLED VERSIONS

commit : 965ceca
python : 3.9.16.final.0
python-bits : 64
OS : Linux
OS-release : 5.10.102.1-microsoft-standard-WSL2
Version : #1 SMP Wed Mar 2 00:30:59 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.0.2
numpy : 1.24.2
pytz : 2023.3
dateutil : 2.8.2
setuptools : 58.1.0
pip : 23.1.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.12.0
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.