From bc745d53b238e6abaf88ed635a225265e3c4072e Mon Sep 17 00:00:00 2001 From: Samil Ayoub Date: Sat, 19 Sep 2020 23:48:32 +0100 Subject: [PATCH 01/10] add a test for loc method; check if a warning raise when replacing a subframe --- pandas/tests/indexing/test_loc.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 9b9bca77e17ec..c4f85fc55ef10 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -1130,3 +1130,16 @@ def test_loc_with_period_index_indexer(): tm.assert_frame_equal(df, df.loc[list(idx)]) tm.assert_frame_equal(df.iloc[0:5], df.loc[idx[0:5]]) tm.assert_frame_equal(df, df.loc[list(idx)]) + +def test_loc_replace_subset_with_another(): + # GH#36424 Should raise a warning + df1 = pd.DataFrame( + data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), + columns = ['A', 'B', 'C'], + index = [0, 0, 1] + ) + df2 = df1.copy() + df2[:] = np.nan + # it fail if a warning is not raised + with pytest.warns(Warning): + df2.loc['0']['A'] = df1.loc['0']['A'] From e203af03a29f70d5d4493c58356f1246cd6a9e0e Mon Sep 17 00:00:00 2001 From: Samil Ayoub Date: Sun, 20 Sep 2020 00:04:43 +0100 Subject: [PATCH 02/10] Clean the code --- pandas/tests/indexing/test_loc.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index c4f85fc55ef10..463ca97bf90f5 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -1131,12 +1131,13 @@ def test_loc_with_period_index_indexer(): tm.assert_frame_equal(df.iloc[0:5], df.loc[idx[0:5]]) tm.assert_frame_equal(df, df.loc[list(idx)]) + def test_loc_replace_subset_with_another(): # GH#36424 Should raise a warning df1 = pd.DataFrame( - data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), - columns = ['A', 'B', 'C'], - index = [0, 0, 1] + data=np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), + columns=['A', 'B', 'C'], + index=[0, 0, 1] ) df2 = df1.copy() df2[:] = np.nan From d41ac51562cd506996729b4cc81e05d343de9a41 Mon Sep 17 00:00:00 2001 From: Samil Ayoub Date: Sun, 20 Sep 2020 00:15:55 +0100 Subject: [PATCH 03/10] using assert_produces_warning instead and rename the method --- pandas/tests/indexing/test_loc.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 463ca97bf90f5..b62efe5a0f5f6 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -1132,7 +1132,7 @@ def test_loc_with_period_index_indexer(): tm.assert_frame_equal(df, df.loc[list(idx)]) -def test_loc_replace_subset_with_another(): +def test_loc_replace_subset_with_subset(): # GH#36424 Should raise a warning df1 = pd.DataFrame( data=np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), @@ -1142,5 +1142,5 @@ def test_loc_replace_subset_with_another(): df2 = df1.copy() df2[:] = np.nan # it fail if a warning is not raised - with pytest.warns(Warning): + with tm.assert_produces_warning(): df2.loc['0']['A'] = df1.loc['0']['A'] From 176f926ba48485f3290daa182ca5a7f18dd2d6ee Mon Sep 17 00:00:00 2001 From: Samil Ayoub Date: Sun, 20 Sep 2020 18:33:28 +0100 Subject: [PATCH 04/10] capture rasing instead of warning --- pandas/tests/indexing/p.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 pandas/tests/indexing/p.py diff --git a/pandas/tests/indexing/p.py b/pandas/tests/indexing/p.py new file mode 100644 index 0000000000000..8b63303b84d38 --- /dev/null +++ b/pandas/tests/indexing/p.py @@ -0,0 +1,20 @@ +import pandas as pd +import pytest +import numpy as np +from pandas.core.common import SettingWithCopyError + +def test_loc_replace_subset_with_subset(): + # GH#36424 Should raise a warning + df1 = pd.DataFrame( + data=np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), + columns=['A', 'B', 'C'], + index=[0, 0, 1] + ) + df2 = df1.copy() + df2[:] = np.nan + # it fail if a warning is not raised + with pytest.raises(SettingWithCopyError): + df2.loc[0]['A'] = df1.loc[0]['A'] + +if __name__ == "__main__": + test_loc_replace_subset_with_subset() \ No newline at end of file From b9f23626c2121726be67f17ce757dbd20b577d48 Mon Sep 17 00:00:00 2001 From: Samil Ayoub Date: Sun, 20 Sep 2020 18:36:18 +0100 Subject: [PATCH 05/10] clean the code --- pandas/tests/indexing/p.py | 20 -------------------- pandas/tests/indexing/test_loc.py | 12 ++++++------ 2 files changed, 6 insertions(+), 26 deletions(-) delete mode 100644 pandas/tests/indexing/p.py diff --git a/pandas/tests/indexing/p.py b/pandas/tests/indexing/p.py deleted file mode 100644 index 8b63303b84d38..0000000000000 --- a/pandas/tests/indexing/p.py +++ /dev/null @@ -1,20 +0,0 @@ -import pandas as pd -import pytest -import numpy as np -from pandas.core.common import SettingWithCopyError - -def test_loc_replace_subset_with_subset(): - # GH#36424 Should raise a warning - df1 = pd.DataFrame( - data=np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), - columns=['A', 'B', 'C'], - index=[0, 0, 1] - ) - df2 = df1.copy() - df2[:] = np.nan - # it fail if a warning is not raised - with pytest.raises(SettingWithCopyError): - df2.loc[0]['A'] = df1.loc[0]['A'] - -if __name__ == "__main__": - test_loc_replace_subset_with_subset() \ No newline at end of file diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index b62efe5a0f5f6..24364e2475c5e 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -12,7 +12,7 @@ import pandas._testing as tm from pandas.api.types import is_scalar from pandas.tests.indexing.common import Base - +from pandas.core.common import SettingWithCopyError class TestLoc(Base): def test_loc_getitem_int(self): @@ -1133,14 +1133,14 @@ def test_loc_with_period_index_indexer(): def test_loc_replace_subset_with_subset(): - # GH#36424 Should raise a warning + # GH#36424 Should raise a SettingWithCopyError df1 = pd.DataFrame( data=np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), - columns=['A', 'B', 'C'], + columns=["A", "B", "C"], index=[0, 0, 1] ) df2 = df1.copy() df2[:] = np.nan - # it fail if a warning is not raised - with tm.assert_produces_warning(): - df2.loc['0']['A'] = df1.loc['0']['A'] + # It fail if a SettingWithCopyError is not raised + with pytest.raises(SettingWithCopyError): + df2.loc[0]["A"] = df1.loc[0]["A"] From 4aa3bb8d6e6a1e93313149b14ba7f48fe4e0ce09 Mon Sep 17 00:00:00 2001 From: Samil Ayoub Date: Sun, 20 Sep 2020 20:05:32 +0100 Subject: [PATCH 06/10] remove non standard import and solving linting issues --- pandas/tests/indexing/test_loc.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 24364e2475c5e..b9c451164b901 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -12,7 +12,7 @@ import pandas._testing as tm from pandas.api.types import is_scalar from pandas.tests.indexing.common import Base -from pandas.core.common import SettingWithCopyError + class TestLoc(Base): def test_loc_getitem_int(self): @@ -1137,10 +1137,10 @@ def test_loc_replace_subset_with_subset(): df1 = pd.DataFrame( data=np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), columns=["A", "B", "C"], - index=[0, 0, 1] + index=[0, 0, 1], ) df2 = df1.copy() df2[:] = np.nan # It fail if a SettingWithCopyError is not raised - with pytest.raises(SettingWithCopyError): + with pytest.raises(pandas.core.common.SettingWithCopyError): df2.loc[0]["A"] = df1.loc[0]["A"] From c8733092f7e4be32d88479dd693daa13441f227d Mon Sep 17 00:00:00 2001 From: Samil Ayoub Date: Sun, 20 Sep 2020 23:28:31 +0100 Subject: [PATCH 07/10] using import --- pandas/tests/indexing/test_loc.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index b9c451164b901..3d1a39bbb08b2 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -5,6 +5,8 @@ import numpy as np import pytest +import pandas.core.common as com + from pandas.compat.numpy import is_numpy_dev import pandas as pd @@ -1141,6 +1143,5 @@ def test_loc_replace_subset_with_subset(): ) df2 = df1.copy() df2[:] = np.nan - # It fail if a SettingWithCopyError is not raised - with pytest.raises(pandas.core.common.SettingWithCopyError): + with pytest.raises(com.SettingWithCopyError): df2.loc[0]["A"] = df1.loc[0]["A"] From 45a21ba98ed40d6d94e7987fb22d2847d459c67e Mon Sep 17 00:00:00 2001 From: Samil Ayoub Date: Mon, 21 Sep 2020 11:38:22 +0100 Subject: [PATCH 08/10] Sorting imports & matching error message --- pandas/tests/indexing/test_loc.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 3d1a39bbb08b2..42cd7fbfc7306 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -5,14 +5,13 @@ import numpy as np import pytest -import pandas.core.common as com - from pandas.compat.numpy import is_numpy_dev import pandas as pd from pandas import DataFrame, Series, Timestamp, date_range import pandas._testing as tm from pandas.api.types import is_scalar +import pandas.core.common as com from pandas.tests.indexing.common import Base @@ -1143,5 +1142,6 @@ def test_loc_replace_subset_with_subset(): ) df2 = df1.copy() df2[:] = np.nan - with pytest.raises(com.SettingWithCopyError): + msg = r"A value is trying to be set on a copy of a slice from a DataFrame." + with pytest.raises(com.SettingWithCopyError, match=msg): df2.loc[0]["A"] = df1.loc[0]["A"] From 1708a9e21005b1bbfeb70929d85403049879d43a Mon Sep 17 00:00:00 2001 From: samilAyoub <61546990+samilAyoub@users.noreply.github.com> Date: Mon, 21 Sep 2020 17:24:48 +0100 Subject: [PATCH 09/10] Update pandas/tests/indexing/test_loc.py Co-authored-by: Daniel Saxton <2658661+dsaxton@users.noreply.github.com> --- pandas/tests/indexing/test_loc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 42cd7fbfc7306..92e75bf8d5ad2 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -1142,6 +1142,6 @@ def test_loc_replace_subset_with_subset(): ) df2 = df1.copy() df2[:] = np.nan - msg = r"A value is trying to be set on a copy of a slice from a DataFrame." + msg = "A value is trying to be set on a copy of a slice from a DataFrame." with pytest.raises(com.SettingWithCopyError, match=msg): df2.loc[0]["A"] = df1.loc[0]["A"] From 8f11f2a5e92f40a5a457613439a89bfc6887760b Mon Sep 17 00:00:00 2001 From: Samil Ayoub Date: Tue, 22 Sep 2020 11:56:46 +0100 Subject: [PATCH 10/10] move the test from test_loc to test_chaining_caching.py --- .../tests/indexing/test_chaining_and_caching.py | 8 +++++--- pandas/tests/indexing/test_loc.py | 15 --------------- 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/pandas/tests/indexing/test_chaining_and_caching.py b/pandas/tests/indexing/test_chaining_and_caching.py index 66835c586e6c7..1254f1f217a2e 100644 --- a/pandas/tests/indexing/test_chaining_and_caching.py +++ b/pandas/tests/indexing/test_chaining_and_caching.py @@ -335,12 +335,14 @@ def test_setting_with_copy_bug(self): # this should not raise df2["y"] = ["g", "h", "i"] - def test_detect_chained_assignment_warnings(self): + def test_detect_chained_assignment_warnings_errors(self): + df = DataFrame({"A": ["aaa", "bbb", "ccc"], "B": [1, 2, 3]}) with option_context("chained_assignment", "warn"): - df = DataFrame({"A": ["aaa", "bbb", "ccc"], "B": [1, 2, 3]}) - with tm.assert_produces_warning(com.SettingWithCopyWarning): df.loc[0]["A"] = 111 + with option_context("chained_assignment", "raise"): + with pytest.raises(com.SettingWithCopyError): + df.loc[0]["A"] = 111 def test_detect_chained_assignment_warnings_filter_and_dupe_cols(self): # xref gh-13017. diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 42cd7fbfc7306..9b9bca77e17ec 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -11,7 +11,6 @@ from pandas import DataFrame, Series, Timestamp, date_range import pandas._testing as tm from pandas.api.types import is_scalar -import pandas.core.common as com from pandas.tests.indexing.common import Base @@ -1131,17 +1130,3 @@ def test_loc_with_period_index_indexer(): tm.assert_frame_equal(df, df.loc[list(idx)]) tm.assert_frame_equal(df.iloc[0:5], df.loc[idx[0:5]]) tm.assert_frame_equal(df, df.loc[list(idx)]) - - -def test_loc_replace_subset_with_subset(): - # GH#36424 Should raise a SettingWithCopyError - df1 = pd.DataFrame( - data=np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), - columns=["A", "B", "C"], - index=[0, 0, 1], - ) - df2 = df1.copy() - df2[:] = np.nan - msg = r"A value is trying to be set on a copy of a slice from a DataFrame." - with pytest.raises(com.SettingWithCopyError, match=msg): - df2.loc[0]["A"] = df1.loc[0]["A"]