From 18885367c85a330e1ea5569d619b613a848d0520 Mon Sep 17 00:00:00 2001 From: gfyoung Date: Sun, 4 Nov 2018 01:53:53 -0700 Subject: [PATCH] TST: Add test of assignment chaining and dupe cols xref gh-13017. --- .../indexing/test_chaining_and_caching.py | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/pandas/tests/indexing/test_chaining_and_caching.py b/pandas/tests/indexing/test_chaining_and_caching.py index 71fec75f9a7d3..8bc8cb3fb1535 100644 --- a/pandas/tests/indexing/test_chaining_and_caching.py +++ b/pandas/tests/indexing/test_chaining_and_caching.py @@ -337,13 +337,24 @@ def f(): df2['y'] = ['g', 'h', 'i'] def test_detect_chained_assignment_warnings(self): + with option_context("chained_assignment", "warn"): + df = DataFrame({"A": ["aaa", "bbb", "ccc"], "B": [1, 2, 3]}) - # warnings - with option_context('chained_assignment', 'warn'): - df = DataFrame({'A': ['aaa', 'bbb', 'ccc'], 'B': [1, 2, 3]}) - with tm.assert_produces_warning( - expected_warning=com.SettingWithCopyWarning): - df.loc[0]['A'] = 111 + with tm.assert_produces_warning(com.SettingWithCopyWarning): + df.loc[0]["A"] = 111 + + def test_detect_chained_assignment_warnings_filter_and_dupe_cols(self): + # xref gh-13017. + with option_context("chained_assignment", "warn"): + df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, -9]], + columns=["a", "a", "c"]) + + with tm.assert_produces_warning(com.SettingWithCopyWarning): + df.c.loc[df.c > 0] = None + + expected = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, -9]], + columns=["a", "a", "c"]) + tm.assert_frame_equal(df, expected) def test_chained_getitem_with_lists(self):