Skip to content
19 changes: 19 additions & 0 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading