Skip to content

Commit 320d613

Browse files
fbourgeymroeschke
andauthored
Series replace error (#59552)
* changed AttributeError to ValueError * added test for replace method from dict_like to dict_like for a pd.Series * added entry in whatsnew.rst * Update doc/source/whatsnew/v3.0.0.rst removed rst entry Co-authored-by: Matthew Roeschke <[email protected]> * Update pandas/core/generic.py Co-authored-by: Matthew Roeschke <[email protected]> * Update pandas/core/generic.py Co-authored-by: Matthew Roeschke <[email protected]> * updated msg test function * breaking line for 88 instead of 89 characters --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent e1a79b2 commit 320d613

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

pandas/core/generic.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -7487,9 +7487,13 @@ def replace(
74877487
if inplace:
74887488
return None
74897489
return self.copy(deep=False)
7490-
74917490
if is_dict_like(to_replace):
74927491
if is_dict_like(value): # {'A' : NA} -> {'A' : 0}
7492+
if isinstance(self, ABCSeries):
7493+
raise ValueError(
7494+
"to_replace and value cannot be dict-like for "
7495+
"Series.replace"
7496+
)
74937497
# Note: Checking below for `in foo.keys()` instead of
74947498
# `in foo` is needed for when we have a Series and not dict
74957499
mapping = {

pandas/tests/series/methods/test_replace.py

+9
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,15 @@ def test_replace_only_one_dictlike_arg(self, fixed_now_ts):
496496
with pytest.raises(ValueError, match=msg):
497497
ser.replace(to_replace, value)
498498

499+
def test_replace_dict_like_with_dict_like(self):
500+
# GH 59452
501+
s = pd.Series([1, 2, 3, 4, 5])
502+
to_replace = pd.Series([1])
503+
value = pd.Series([75])
504+
msg = "to_replace and value cannot be dict-like for Series.replace"
505+
with pytest.raises(ValueError, match=msg):
506+
s.replace(to_replace, value)
507+
499508
def test_replace_extension_other(self, frame_or_series):
500509
# https://github.com/pandas-dev/pandas/issues/34530
501510
obj = frame_or_series(pd.array([1, 2, 3], dtype="Int64"))

0 commit comments

Comments
 (0)