From c439ff7eb905a4ab2381926f88762958e96e1f14 Mon Sep 17 00:00:00 2001 From: matteosantama Date: Thu, 21 May 2020 21:13:25 +0000 Subject: [PATCH 1/2] TST: 24798 df.replace() with duplicate columns --- pandas/tests/frame/methods/test_replace.py | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/pandas/tests/frame/methods/test_replace.py b/pandas/tests/frame/methods/test_replace.py index 444aa45155dbf..67ecc7716248d 100644 --- a/pandas/tests/frame/methods/test_replace.py +++ b/pandas/tests/frame/methods/test_replace.py @@ -1388,3 +1388,29 @@ def test_replace_no_replacement_dtypes(self, dtype, value): df = pd.DataFrame(np.eye(2), dtype=dtype) result = df.replace(to_replace=[None, -np.inf, np.inf], value=value) tm.assert_frame_equal(result, df) + + @pytest.mark.parametrize("replacement", [np.nan, pd.NA, 5]) + @pytest.mark.parametrize("inplace", [True, False]) + def test_replace_with_duplicate_columns(self, replacement, inplace): + # https://github.com/pandas-dev/pandas/issues/24798 + # should be able to assign column of df with replace() + # even if the df has duplicate columns + + # can't init df with duplicate columns, so we assign after + result = pd.DataFrame({"A": [1, 2, 3], "A1": [4, 5, 6], "B": [7, 8, 9]}) + result.columns = list("AAB") + + expected = pd.DataFrame( + {"A": [1, 2, 3], "A1": [4, 5, 6], "B": [replacement, 8, 9]} + ) + expected.columns = list("AAB") + + if inplace: + # fails for now, will be fixed eventually + with pytest.raises(pd.core.common.SettingWithCopyError): + result["B"].replace(7, replacement, inplace=True) + tm.assert_frame_equal(result, expected) + else: + result["B"] = result["B"].replace(7, replacement, inplace=False) + + tm.assert_frame_equal(result, expected) From 087f0e2cb2e8ab8e43b09fb761d87280f5d74664 Mon Sep 17 00:00:00 2001 From: matteosantama Date: Mon, 25 May 2020 22:29:13 +0000 Subject: [PATCH 2/2] Narrow scope of tests --- pandas/tests/frame/methods/test_replace.py | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/pandas/tests/frame/methods/test_replace.py b/pandas/tests/frame/methods/test_replace.py index 67ecc7716248d..3bcc26e85e347 100644 --- a/pandas/tests/frame/methods/test_replace.py +++ b/pandas/tests/frame/methods/test_replace.py @@ -1389,14 +1389,9 @@ def test_replace_no_replacement_dtypes(self, dtype, value): result = df.replace(to_replace=[None, -np.inf, np.inf], value=value) tm.assert_frame_equal(result, df) - @pytest.mark.parametrize("replacement", [np.nan, pd.NA, 5]) - @pytest.mark.parametrize("inplace", [True, False]) - def test_replace_with_duplicate_columns(self, replacement, inplace): - # https://github.com/pandas-dev/pandas/issues/24798 - # should be able to assign column of df with replace() - # even if the df has duplicate columns - - # can't init df with duplicate columns, so we assign after + @pytest.mark.parametrize("replacement", [np.nan, 5]) + def test_replace_with_duplicate_columns(self, replacement): + # GH 24798 result = pd.DataFrame({"A": [1, 2, 3], "A1": [4, 5, 6], "B": [7, 8, 9]}) result.columns = list("AAB") @@ -1405,12 +1400,6 @@ def test_replace_with_duplicate_columns(self, replacement, inplace): ) expected.columns = list("AAB") - if inplace: - # fails for now, will be fixed eventually - with pytest.raises(pd.core.common.SettingWithCopyError): - result["B"].replace(7, replacement, inplace=True) - tm.assert_frame_equal(result, expected) - else: - result["B"] = result["B"].replace(7, replacement, inplace=False) + result["B"] = result["B"].replace(7, replacement) - tm.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected)