Skip to content

Commit 2728408

Browse files
committed
robustify tests and add additional comments
1 parent a96a5f1 commit 2728408

File tree

2 files changed

+75
-12
lines changed

2 files changed

+75
-12
lines changed

pandas/tests/frame/indexing/test_indexing.py

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,22 +1403,59 @@ def test_set_value_resize(self, float_frame):
14031403
def test_reindex_with_multi_index(self):
14041404
# https://github.com/pandas-dev/pandas/issues/29896
14051405
# tests for reindexing a multi-indexed DataFrame with a new MultiIndex
1406-
df = pd.DataFrame(
1407-
{"a": [0] * 7, "b": list(range(7)), "c": list(range(7))}
1408-
).set_index(["a", "b"])
1406+
#
1407+
# confirms that we can reindex a multi-indexed DataFrame with a new
1408+
# MultiIndex object correctly when using no filling, backfilling, and
1409+
# padding
1410+
#
1411+
# The DataFrame, `df`, used in this test is:
1412+
# c
1413+
# a b
1414+
# -1 0 A
1415+
# 1 B
1416+
# 2 C
1417+
# 3 D
1418+
# 4 E
1419+
# 5 F
1420+
# 6 G
1421+
# 0 0 A
1422+
# 1 B
1423+
# 2 C
1424+
# 3 D
1425+
# 4 E
1426+
# 5 F
1427+
# 6 G
1428+
# 1 0 A
1429+
# 1 B
1430+
# 2 C
1431+
# 3 D
1432+
# 4 E
1433+
# 5 F
1434+
# 6 G
1435+
#
1436+
# and the other MultiIndex, `new_multi_index`, is:
1437+
# 0: 0 0.5
1438+
# 1: 2.0
1439+
# 2: 5.0
1440+
# 3: 5.8
1441+
df = pd.DataFrame({
1442+
"a": [-1] * 7 + [0] * 7 + [1] * 7 ,
1443+
"b": list(range(7)) * 3,
1444+
"c": ["A", "B", "C", "D", "E", "F", "G"] * 3,
1445+
}).set_index(["a", "b"])
14091446
new_index = [0.5, 2.0, 5.0, 5.8]
14101447
new_multi_index = MultiIndex.from_product([[0], new_index], names=["a", "b"])
14111448

14121449
# reindexing w/o a `method` value
14131450
reindexed = df.reindex(new_multi_index)
14141451
expected = pd.DataFrame(
1415-
{"a": [0] * 4, "b": new_index, "c": [np.nan, 2.0, 5.0, np.nan]}
1452+
{"a": [0] * 4, "b": new_index, "c": [np.nan, "C", "F", np.nan]}
14161453
).set_index(["a", "b"])
14171454
tm.assert_frame_equal(expected, reindexed)
14181455

14191456
# reindexing with backfilling
14201457
expected = pd.DataFrame(
1421-
{"a": [0] * 4, "b": new_index, "c": [1, 2, 5, 6]}
1458+
{"a": [0] * 4, "b": new_index, "c": ["B", "C", "F", "G"]}
14221459
).set_index(["a", "b"])
14231460
reindexed_with_backfilling = df.reindex(new_multi_index, method="bfill")
14241461
tm.assert_frame_equal(expected, reindexed_with_backfilling)
@@ -1428,7 +1465,7 @@ def test_reindex_with_multi_index(self):
14281465

14291466
# reindexing with padding
14301467
expected = pd.DataFrame(
1431-
{"a": [0] * 4, "b": new_index, "c": [0, 2, 5, 5]}
1468+
{"a": [0] * 4, "b": new_index, "c": ["A", "C", "F", "F"]}
14321469
).set_index(["a", "b"])
14331470
reindexed_with_padding = df.reindex(new_multi_index, method="pad")
14341471
tm.assert_frame_equal(expected, reindexed_with_padding)

pandas/tests/indexes/multi/test_indexing.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,27 +186,53 @@ def test_get_indexer():
186186
def test_get_indexer_methods():
187187
# https://github.com/pandas-dev/pandas/issues/29896
188188
# test getting an indexer for another index with different methods
189-
mult_idx_1 = MultiIndex.from_product([[0], [0, 2, 3, 4]])
189+
# confirms that getting an indexer without a filling method, getting an
190+
# indexer and backfilling, and getting an indexer and padding all behave
191+
# correctly in the case where all of the target values fall in between
192+
# several levels in the MultiIndex into which they are getting an indexer
193+
#
194+
# visually, the MultiIndexes used in this test are:
195+
# mult_idx_1:
196+
# 0: -1 0
197+
# 1: 2
198+
#  2: 3
199+
# 3: 4
200+
# 4: 0 0
201+
# 5: 2
202+
#  6: 3
203+
# 7: 4
204+
# 8: 1 0
205+
# 9: 2
206+
# 10: 3
207+
# 11: 4
208+
#
209+
# mult_idx_2:
210+
# 0: 0 1
211+
# 1: 3
212+
# 2: 4
213+
mult_idx_1 = MultiIndex.from_product([[-1, 0, 1], [0, 2, 3, 4]])
190214
mult_idx_2 = MultiIndex.from_product([[0], [1, 3, 4]])
191215

192216
indexer = mult_idx_1.get_indexer(mult_idx_2)
193-
expected = np.array([-1, 2, 3], dtype=indexer.dtype)
217+
expected = np.array([-1, 6, 7], dtype=indexer.dtype)
194218
tm.assert_almost_equal(expected, indexer)
195219

196220
backfill_indexer = mult_idx_1.get_indexer(mult_idx_2, method="backfill")
197-
expected = np.array([1, 2, 3], dtype=backfill_indexer.dtype)
221+
expected = np.array([5, 6, 7], dtype=backfill_indexer.dtype)
198222
tm.assert_almost_equal(expected, backfill_indexer)
199223

224+
# ensure the legacy "bfill" option functions identically to "backfill"
200225
backfill_indexer = mult_idx_1.get_indexer(mult_idx_2, method="bfill")
201-
expected = np.array([1, 2, 3], dtype=backfill_indexer.dtype)
226+
expected = np.array([5, 6, 7], dtype=backfill_indexer.dtype)
202227
tm.assert_almost_equal(expected, backfill_indexer)
203228

204229
pad_indexer = mult_idx_1.get_indexer(mult_idx_2, method="pad")
205-
expected = np.array([0, 2, 3], dtype=pad_indexer.dtype)
230+
expected = np.array([4, 6, 7], dtype=pad_indexer.dtype)
206231
tm.assert_almost_equal(expected, pad_indexer)
207232

233+
# ensure the legacy "ffill" option functions identically to "pad"
208234
pad_indexer = mult_idx_1.get_indexer(mult_idx_2, method="ffill")
209-
expected = np.array([0, 2, 3], dtype=pad_indexer.dtype)
235+
expected = np.array([4, 6, 7], dtype=pad_indexer.dtype)
210236
tm.assert_almost_equal(expected, pad_indexer)
211237

212238
def test_get_indexer_three_or_more_levels():

0 commit comments

Comments
 (0)