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: pd.replace changes dtype when using a dict to replace values even when there are no matches #53539

Open
3 tasks done
sergiykhan opened this issue Jun 6, 2023 · 4 comments
Assignees
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions replace replace method

Comments

@sergiykhan
Copy link

sergiykhan commented Jun 6, 2023

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

@sergiykhan sergiykhan added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 6, 2023
@lithomas1 lithomas1 added Dtype Conversions Unexpected or buggy dtype conversions replace replace method and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 6, 2023
@ABizzinotto
Copy link
Contributor

take

@phofl
Copy link
Member

phofl commented Jul 2, 2023

This is intended behavior unfortunately. We can't change this without deprecating.

You should not use object to store numbers in general

@sergiykhan
Copy link
Author

Just to clarify, are you saying that the inconsistent behavior between {5: 0} and to_replace=5, value=0 is intended behaviour?

@phofl
Copy link
Member

phofl commented Jul 2, 2023

Nope the inconsistency is not intended. But the dict case is fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions replace replace method
Projects
None yet
4 participants