Skip to content

BUG: inconsistency in dtype of replace() #44864

Closed
@ShafinKhadem

Description

@ShafinKhadem

  • 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

import pandas as pd
s = pd.Series(['0'])
print(s.replace(to_replace='0', value=1, regex=False).dtype)  # shows int64
print(s.replace(to_replace='0', value=1, regex=True).dtype)  # shows object

Issue Description

dtype of replace() is different based on regex. When regex=False, dtype is int64. But when regex=True, dtype is object.

Expected Behavior

dtype in both cases should be the same, int64, as value=1

Installed Versions

INSTALLED VERSIONS

commit : 66e3805
python : 3.9.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.11.0-41-generic
Version : #45-Ubuntu SMP Fri Nov 5 11:37:01 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : en_US.utf8
LANG : en_US.utf8
LOCALE : en_US.UTF-8

pandas : 1.3.5
numpy : 1.21.4
pytz : 2021.3
dateutil : 2.8.1
pip : 20.3.4
setuptools : 52.0.0
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 : 7.30.1
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.4.2
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.7.3
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None
None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDtype ConversionsUnexpected or buggy dtype conversionsRegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions