Skip to content

Commit 60a3b81

Browse files
committed
changes
1 parent d9912dc commit 60a3b81

File tree

3 files changed

+21
-25
lines changed

3 files changed

+21
-25
lines changed

pandas/_testing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@ def assert_series_equal(
11061106
check_categorical : bool, default True
11071107
Whether to compare internal Categorical exactly.
11081108
check_category_order : bool, default True
1109-
Whether to compare category order internal Categoricals
1109+
Whether to compare category order of internal Categoricals
11101110
obj : str, default 'Series'
11111111
Specify object name being compared, internally used to show appropriate
11121112
assertion message.

pandas/core/arrays/categorical.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2412,9 +2412,6 @@ def isin(self, values):
24122412
code_values = code_values[null_mask | (code_values >= 0)]
24132413
return algorithms.isin(self.codes, code_values)
24142414

2415-
def replace_list(self, to_replace, value, inplace: bool = False):
2416-
return self.replace(to_replace, value, inplace)
2417-
24182415
def replace(self, to_replace, value, inplace: bool = False):
24192416
"""
24202417
Replaces all instances of one value with another
@@ -2445,12 +2442,7 @@ def replace(self, to_replace, value, inplace: bool = False):
24452442
inplace = validate_bool_kwarg(inplace, "inplace")
24462443
cat = self if inplace else self.copy()
24472444
if is_list_like(to_replace):
2448-
if is_list_like(value):
2449-
if len(to_replace) != len(value):
2450-
raise ValueError("to_replace and value must be same length!")
2451-
replace_dict = dict(zip(to_replace, value))
2452-
else:
2453-
replace_dict = {replace_value: value for replace_value in to_replace}
2445+
replace_dict = {replace_value: value for replace_value in to_replace}
24542446
else:
24552447
replace_dict = {to_replace: value}
24562448
for replace_value, new_value in replace_dict.items():

pandas/tests/arrays/categorical/test_replace.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,29 @@
55

66

77
@pytest.mark.parametrize(
8-
"to_replace,value,expected,check_types",
8+
"to_replace,value,expected,check_types,check_categorical",
99
[
1010
# one-to-one
11-
(1, 2, [2, 2, 3], True),
12-
(1, 4, [4, 2, 3], True),
13-
(4, 1, [1, 2, 3], True),
14-
(5, 6, [1, 2, 3], True),
11+
(1, 2, [2, 2, 3], True, True),
12+
(1, 4, [4, 2, 3], True, True),
13+
(4, 1, [1, 2, 3], True, True),
14+
(5, 6, [1, 2, 3], True, True),
1515
# many-to-one
16-
([1], 2, [2, 2, 3], True),
17-
([1, 2], 3, [3, 3, 3], True),
18-
([1, 2], 4, [4, 4, 3], True),
19-
((1, 2, 4), 5, [5, 5, 3], True),
20-
((5, 6), 2, [1, 2, 3], True),
16+
([1], 2, [2, 2, 3], True, True),
17+
([1, 2], 3, [3, 3, 3], True, True),
18+
([1, 2], 4, [4, 4, 3], True, True),
19+
((1, 2, 4), 5, [5, 5, 3], True, True),
20+
((5, 6), 2, [1, 2, 3], True, True),
2121
# many-to-many, handled outside of Categorical and results in separate dtype
22-
([1], [2], [2, 2, 3], False),
23-
([1, 4], [5, 2], [5, 2, 3], False),
22+
([1], [2], [2, 2, 3], False, False),
23+
([1, 4], [5, 2], [5, 2, 3], False, False),
24+
# checking categorical causes categories to be sorted, which can crash on mixed dtypes
25+
(3, "4", [1, 2, "4"], True, False),
26+
([1, 2, "3"], "5", ["5", "5", 3], True, False)
2427
],
2528
)
26-
def test_replace(to_replace, value, expected, check_types):
29+
# GH 31720
30+
def test_replace(to_replace, value, expected, check_types, check_categorical):
2731
s = pd.Series([1, 2, 3], dtype="category")
2832
result = s.replace(to_replace, value)
2933
expected = pd.Series(expected, dtype="category")
@@ -32,13 +36,13 @@ def test_replace(to_replace, value, expected, check_types):
3236
expected,
3337
result,
3438
check_dtype=check_types,
35-
check_categorical=check_types,
39+
check_categorical=check_categorical,
3640
check_category_order=False,
3741
)
3842
tm.assert_series_equal(
3943
expected,
4044
s,
4145
check_dtype=check_types,
42-
check_categorical=check_types,
46+
check_categorical=check_categorical,
4347
check_category_order=False,
4448
)

0 commit comments

Comments
 (0)