Skip to content

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

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

Open
2 of 3 tasks
d-rams opened this issue Feb 15, 2022 · 2 comments
Open
2 of 3 tasks
Labels
Bug Error Reporting Incorrect or improved errors from pandas replace replace method Series Series data structure

Comments

@d-rams
Copy link

d-rams commented Feb 15, 2022

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

@d-rams d-rams added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 15, 2022
@d-rams d-rams changed the title BUG: Series.replace with to_replace dict and value=None BUG: Series.replace with to_replace dict and value=None, nonsensical error message Feb 15, 2022
@phofl phofl added Error Reporting Incorrect or improved errors from pandas replace replace method and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 15, 2022
@simonjayhawkins simonjayhawkins added the Series Series data structure label Apr 6, 2022
@Kyrpel
Copy link

Kyrpel commented Apr 7, 2022

take

@alainrafiki
Copy link

The error message may need to be improved for clarity.

df = df1.replace(to_replace=dictionaryvar, value=None, inplace=False) resulted in the same error for me as well:

ValueError: Series.replace cannot use dict-like to_replace and non-None value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Error Reporting Incorrect or improved errors from pandas replace replace method Series Series data structure
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants