Skip to content

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

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

Closed
wants to merge 13 commits into from
29 changes: 28 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6594,6 +6594,7 @@ def replace(
regex=False,
method=lib.no_default,
):

if not (
is_scalar(to_replace)
or is_re_compilable(to_replace)
Expand Down Expand Up @@ -6701,6 +6702,33 @@ def replace(

# {'A': NA} -> 0
elif not is_list_like(value):

items = list(to_replace.items())
keys, values = zip(*items) if items else ([], [])
if items and (value is None or value is not lib.no_default):
# e.g ({1:5}, value = None),
# ({1:5}, value = 10), ({1:5}, value="")

col = keys[0]
if col in self and isinstance(self, ABCDataFrame):
# e.g Nested dict in df {"a": {1: 5}}, value=10),
# or columnwise with None value {"a": 1}, value=None
print("col in self")
mapping = {
col: (to_replace.values(), value)
for col in to_replace.keys()
if col in self
}
return self._replace_columnwise(mapping, inplace, regex)

return self.replace(
to_replace=keys,
value=values,
inplace=inplace,
limit=limit,
regex=regex,
)

# Operate column-wise
if self.ndim == 1:
raise ValueError(
Expand Down Expand Up @@ -6748,7 +6776,6 @@ def replace(
regex, value, inplace=inplace, limit=limit, regex=True
)
else:

# dest iterable dict-like
if is_dict_like(value): # NA -> {'A' : 0, 'B' : -1}
# Operate column-wise
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/series/methods/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,14 @@ def test_pandas_replace_na(self):
exp = pd.Series(["CC", "CC", "CC-REPL", "DD", "CC", "", pd.NA], dtype="string")
tm.assert_series_equal(result, exp)

def test_replace_dictlike_value_None(self):
# GH46004
ser = pd.Series([1, 2, 3, 4])
result = ser.replace(to_replace={3: 33}, value=None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is supposed to work. We are using lib.no_default as a sentinel to check if value was provided.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like when we have explicitly to_replace=dict_like and value=None it interpreted as a special case where None is different from lib.no_default. Maybe we need another conditional statement somewhere.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should raise if value=None, otherwise lib.no_default does not make sense


exp = pd.Series([1, 2, 33, 4])
tm.assert_series_equal(result, exp)

@pytest.mark.parametrize(
"dtype, input_data, to_replace, expected_data",
[
Expand Down