From 0f7a54807634df2c9bc2794c7ff4b1bcf326110a Mon Sep 17 00:00:00 2001 From: dshettyepi Date: Fri, 29 Nov 2024 12:44:00 +0200 Subject: [PATCH 1/6] Added test for .loc to test setitem on matching indices --- pandas/tests/indexing/test_loc.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index e0e9d4cfc5ccb..c252139d87d62 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -3297,3 +3297,22 @@ def test_loc_reindexing_of_empty_index(self): df.loc[Series([False] * 4, index=df.index, name=0), 0] = df[0] expected = DataFrame(index=[1, 1, 2, 2], data=["1", "1", "2", "2"]) tm.assert_frame_equal(df, expected) + + def test_loc_setitem_matching_index(self,series_with_simple_index): + # GH 25548 + ser1=series_with_simple_index.copy() + ser1=ser1[~ser1.index.duplicated(keep='first')] + ser2=ser1.copy() + # Testing on upto 2 indices + for nsize in range(1,min(len(ser1),3)): + random_sample=ser1.sample(n=nsize) + matching_mask=ser1.index.isin(random_sample.index) + # Remove values at indices to test assignment + ser1.loc[matching_mask]=np.NaN + ser1.loc[matching_mask]=random_sample + tm.assert_series_equal(ser1,ser2) + #exclude row and index and test assignment of unmatched indices + exclude_mask=~matching_mask + ser2.loc[matching_mask]=ser1.loc[exclude_mask] + ser1.loc[matching_mask]=np.NaN + tm.assert_series_equal(ser2,ser1) From 5175288f5ecad815b17b4a52acc8bb8d77afc6e7 Mon Sep 17 00:00:00 2001 From: dshettyepi Date: Fri, 29 Nov 2024 13:16:49 +0200 Subject: [PATCH 2/6] precommit workflow --- pandas/tests/indexing/test_loc.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index c252139d87d62..da37b1f9a63bc 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -3298,21 +3298,21 @@ def test_loc_reindexing_of_empty_index(self): expected = DataFrame(index=[1, 1, 2, 2], data=["1", "1", "2", "2"]) tm.assert_frame_equal(df, expected) - def test_loc_setitem_matching_index(self,series_with_simple_index): + def test_loc_setitem_matching_index(self, series_with_simple_index): # GH 25548 - ser1=series_with_simple_index.copy() - ser1=ser1[~ser1.index.duplicated(keep='first')] - ser2=ser1.copy() + ser1 = series_with_simple_index.copy() + ser1 = ser1[~ser1.index.duplicated(keep="first")] + ser2 = ser1.copy() # Testing on upto 2 indices - for nsize in range(1,min(len(ser1),3)): - random_sample=ser1.sample(n=nsize) - matching_mask=ser1.index.isin(random_sample.index) + for nsize in range(1, min(len(ser1), 3)): + random_sample = ser1.sample(n=nsize) + matching_mask = ser1.index.isin(random_sample.index) # Remove values at indices to test assignment - ser1.loc[matching_mask]=np.NaN - ser1.loc[matching_mask]=random_sample - tm.assert_series_equal(ser1,ser2) - #exclude row and index and test assignment of unmatched indices - exclude_mask=~matching_mask - ser2.loc[matching_mask]=ser1.loc[exclude_mask] - ser1.loc[matching_mask]=np.NaN - tm.assert_series_equal(ser2,ser1) + ser1.loc[matching_mask] = np.NaN + ser1.loc[matching_mask] = random_sample + tm.assert_series_equal(ser1, ser2) + # exclude row and index and test assignment of unmatched indices + exclude_mask = ~matching_mask + ser2.loc[matching_mask] = ser1.loc[exclude_mask] + ser1.loc[matching_mask] = np.NaN + tm.assert_series_equal(ser2, ser1) From 021bdba6ac6830c668daefd2cf3f5c05304d7d6c Mon Sep 17 00:00:00 2001 From: DhruvBShetty Date: Fri, 29 Nov 2024 14:13:32 +0200 Subject: [PATCH 3/6] modified from np.NaN to np.nan --- 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 da37b1f9a63bc..d488ba3e39628 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -3308,11 +3308,11 @@ def test_loc_setitem_matching_index(self, series_with_simple_index): random_sample = ser1.sample(n=nsize) matching_mask = ser1.index.isin(random_sample.index) # Remove values at indices to test assignment - ser1.loc[matching_mask] = np.NaN + ser1.loc[matching_mask] = np.nan ser1.loc[matching_mask] = random_sample tm.assert_series_equal(ser1, ser2) # exclude row and index and test assignment of unmatched indices exclude_mask = ~matching_mask ser2.loc[matching_mask] = ser1.loc[exclude_mask] - ser1.loc[matching_mask] = np.NaN + ser1.loc[matching_mask] = np.nan tm.assert_series_equal(ser2, ser1) From 968c6f71a3566799faac2cf6ac9e93fc77316a49 Mon Sep 17 00:00:00 2001 From: DhruvBShetty Date: Sun, 8 Dec 2024 10:27:07 +0200 Subject: [PATCH 4/6] formatting fixes --- pandas/tests/indexing/test_loc.py | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index d488ba3e39628..d07c7f5e0103d 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -3298,21 +3298,18 @@ def test_loc_reindexing_of_empty_index(self): expected = DataFrame(index=[1, 1, 2, 2], data=["1", "1", "2", "2"]) tm.assert_frame_equal(df, expected) - def test_loc_setitem_matching_index(self, series_with_simple_index): + def test_loc_setitem_matching_index(self): # GH 25548 - ser1 = series_with_simple_index.copy() - ser1 = ser1[~ser1.index.duplicated(keep="first")] - ser2 = ser1.copy() - # Testing on upto 2 indices - for nsize in range(1, min(len(ser1), 3)): - random_sample = ser1.sample(n=nsize) - matching_mask = ser1.index.isin(random_sample.index) - # Remove values at indices to test assignment - ser1.loc[matching_mask] = np.nan - ser1.loc[matching_mask] = random_sample - tm.assert_series_equal(ser1, ser2) - # exclude row and index and test assignment of unmatched indices - exclude_mask = ~matching_mask - ser2.loc[matching_mask] = ser1.loc[exclude_mask] - ser1.loc[matching_mask] = np.nan - tm.assert_series_equal(ser2, ser1) + s = Series(0.0, index=list("abcd")) + s1 = Series(1.0, index=list("ab")) + s2 = Series(2.0, index=list("xy")) + + # Test matching indices + s.loc[["a", "b"]] = s1 + tm.assert_series_equal(s[["a", "b"]], s1) + + # Test unmatched indices + s.loc[["a", "b"]] = s2 + tm.assert_series_equal( + s[["a", "b"]], Series([np.nan, np.nan], index=["a", "b"]) + ) From a1c5fec920612dd278798988b691e72b200d0791 Mon Sep 17 00:00:00 2001 From: DhruvBShetty Date: Sun, 22 Dec 2024 10:25:37 +0200 Subject: [PATCH 5/6] Added result and expected variables --- 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 d07c7f5e0103d..efd5f59867a5f 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -3310,6 +3310,7 @@ def test_loc_setitem_matching_index(self): # Test unmatched indices s.loc[["a", "b"]] = s2 - tm.assert_series_equal( - s[["a", "b"]], Series([np.nan, np.nan], index=["a", "b"]) - ) + + result = s[["a", "b"]] + expected = Series([np.nan, np.nan], index=["a", "b"]) + tm.assert_series_equal(result, expected) From d6604eed74d830e9e42c7b8174ac4216b07acd60 Mon Sep 17 00:00:00 2001 From: DhruvBShetty Date: Sun, 22 Dec 2024 10:56:05 +0200 Subject: [PATCH 6/6] Added result and expected variables for both tests --- pandas/tests/indexing/test_loc.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index efd5f59867a5f..7aeded5a6cb7f 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -3306,7 +3306,10 @@ def test_loc_setitem_matching_index(self): # Test matching indices s.loc[["a", "b"]] = s1 - tm.assert_series_equal(s[["a", "b"]], s1) + + result = s[["a", "b"]] + expected = s1 + tm.assert_series_equal(result, expected) # Test unmatched indices s.loc[["a", "b"]] = s2