Skip to content

BUG: Series.replace with to_replace dict and value=None, nonsensical error message #46004

Open
@d-rams

Description

@d-rams

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
s = pd.Series([1,2,3,4])
s.replace(to_replace={3:33}, value=None)

Issue Description

import pandas as pd
s = pd.Series([1,2,3,4])
s.replace(to_replace={3:33}, value=7)

yields the error as expected:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/drams/anaconda3/envs/plombier-dev/lib/python3.10/site-packages/pandas/core/series.py", line 4960, in replace
    return super().replace(
  File "/Users/drams/anaconda3/envs/plombier-dev/lib/python3.10/site-packages/pandas/core/generic.py", line 6685, in replace
    raise ValueError(
ValueError: Series.replace cannot use dict-like to_replace and non-None value

However,

import pandas as pd
s = pd.Series([1,2,3,4])
s.replace(to_replace={3:33}, value=None)

yields exactly the same error

Expected Behavior

I suspect this change occurred because we now treat None in the value field explicitly. However, the error message makes no sense anymore. I believe the expected behavior is that we get an error telling us that even None is no longer allowed for value when using a dict in to_replace.

Installed Versions

INSTALLED VERSIONS

commit : 06d2301
python : 3.10.2.final.0
python-bits : 64
OS : Darwin
OS-release : 20.6.0
Version : Darwin Kernel Version 20.6.0: Tue Oct 12 18:33:42 PDT 2021; root:xnu-7195.141.8~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.1
numpy : 1.22.2
pytz : 2021.3
dateutil : 2.8.2
pip : 22.0.3
setuptools : 60.9.1
Cython : None
pytest : 7.0.1
hypothesis : 6.36.2
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
fsspec : 2022.01.0
gcsfs : None
matplotlib : None
numba : None
numexpr : 2.8.0
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : 2022.01.0
scipy : None
sqlalchemy : 1.4.31
tables : 3.7.0
tabulate : 0.8.9
xarray : None
xlrd : None
xlwt : None
zstandard : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugError ReportingIncorrect or improved errors from pandasSeriesSeries data structurereplacereplace method

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions