From 97489681ec3235e7b1125eb5b3e0d5d685dfe3bd Mon Sep 17 00:00:00 2001 From: pv8493013j Date: Sat, 2 Nov 2019 14:46:37 +0000 Subject: [PATCH 1/4] add test for GH9113 --- pandas/tests/series/test_replace.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pandas/tests/series/test_replace.py b/pandas/tests/series/test_replace.py index e9d5a4b105a35..d4eb7ce027a33 100644 --- a/pandas/tests/series/test_replace.py +++ b/pandas/tests/series/test_replace.py @@ -305,3 +305,18 @@ def test_replace_with_no_overflowerror(self): result = s.replace(["100000000000000000000"], [1]) expected = pd.Series([0, 1, "100000000000000000001"]) tm.assert_series_equal(result, expected) + + + def test_replace_no_cast(self): + # GH 9113 + # BUG: replace int64 dtype with bool coerces to int64 + + s1 = pd.Series([1, 2, 3]) + result1 = s1.replace(2, True) + expected1 = pd.Series([1, True, 3]) + tm.assert_series_equal(result1, expected1) + + s2 = pd.Series(["x", 2, 3]) + result2 = s2.replace(2, True) + expected2 = pd.Series(["x", True, 3]) + tm.assert_series_equal(result2, expected2) From 6a61f242a5333b0f79307c60f5dce0bec36c0032 Mon Sep 17 00:00:00 2001 From: pv8493013j Date: Sat, 2 Nov 2019 14:47:55 +0000 Subject: [PATCH 2/4] add test for GH9113 --- pandas/tests/series/test_replace.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/series/test_replace.py b/pandas/tests/series/test_replace.py index d4eb7ce027a33..8fdca675d5693 100644 --- a/pandas/tests/series/test_replace.py +++ b/pandas/tests/series/test_replace.py @@ -306,7 +306,6 @@ def test_replace_with_no_overflowerror(self): expected = pd.Series([0, 1, "100000000000000000001"]) tm.assert_series_equal(result, expected) - def test_replace_no_cast(self): # GH 9113 # BUG: replace int64 dtype with bool coerces to int64 From 472f19351dad851491cf09954451a594852d93c0 Mon Sep 17 00:00:00 2001 From: pv8493013j Date: Sat, 2 Nov 2019 18:25:00 +0000 Subject: [PATCH 3/4] tidied test --- pandas/tests/series/test_replace.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/pandas/tests/series/test_replace.py b/pandas/tests/series/test_replace.py index 8fdca675d5693..848de4d9d0de1 100644 --- a/pandas/tests/series/test_replace.py +++ b/pandas/tests/series/test_replace.py @@ -306,16 +306,15 @@ def test_replace_with_no_overflowerror(self): expected = pd.Series([0, 1, "100000000000000000001"]) tm.assert_series_equal(result, expected) - def test_replace_no_cast(self): + @pytest.mark.parametrize( + "ser, to_replace, value, exp", + [([1, 2, 3], 2, True, [1, True, 3]), (["x", 2, 3], 2, True, ["x", True, 3])], + ) + def test_replace_no_cast(self, ser, to_replace, value, exp): # GH 9113 # BUG: replace int64 dtype with bool coerces to int64 - s1 = pd.Series([1, 2, 3]) - result1 = s1.replace(2, True) - expected1 = pd.Series([1, True, 3]) - tm.assert_series_equal(result1, expected1) - - s2 = pd.Series(["x", 2, 3]) - result2 = s2.replace(2, True) - expected2 = pd.Series(["x", True, 3]) - tm.assert_series_equal(result2, expected2) + series = pd.Series(ser) + result = series.replace(to_replace, value) + expected = pd.Series(exp) + tm.assert_series_equal(result, expected) From 0b44cf00eb6fd1f88ef817357a1edfab50c608d9 Mon Sep 17 00:00:00 2001 From: pv8493013j Date: Sat, 2 Nov 2019 20:14:27 +0000 Subject: [PATCH 4/4] update for comments --- pandas/tests/series/test_replace.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pandas/tests/series/test_replace.py b/pandas/tests/series/test_replace.py index 848de4d9d0de1..86a54922fcf86 100644 --- a/pandas/tests/series/test_replace.py +++ b/pandas/tests/series/test_replace.py @@ -307,14 +307,13 @@ def test_replace_with_no_overflowerror(self): tm.assert_series_equal(result, expected) @pytest.mark.parametrize( - "ser, to_replace, value, exp", - [([1, 2, 3], 2, True, [1, True, 3]), (["x", 2, 3], 2, True, ["x", True, 3])], + "ser, exp", [([1, 2, 3], [1, True, 3]), (["x", 2, 3], ["x", True, 3])] ) - def test_replace_no_cast(self, ser, to_replace, value, exp): + def test_replace_no_cast(self, ser, exp): # GH 9113 # BUG: replace int64 dtype with bool coerces to int64 series = pd.Series(ser) - result = series.replace(to_replace, value) + result = series.replace(2, True) expected = pd.Series(exp) tm.assert_series_equal(result, expected)