Skip to content

Commit a059c61

Browse files
committed
TST: test MultiIndexPyIntEngine
1 parent ca967bc commit a059c61

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

pandas/tests/test_multilevel.py

+32
Original file line numberDiff line numberDiff line change
@@ -1590,6 +1590,38 @@ def test_unstack_group_index_overflow(self):
15901590
result = s.unstack(4)
15911591
assert result.shape == (500, 2)
15921592

1593+
def test_pyint_engine(self):
1594+
# GH 18519 : when combinations of codes cannot be represented in 64
1595+
# bits, the index underlying the MultiIndex engine works with Python
1596+
# integers, rather than uint64.
1597+
N = 5
1598+
keys = [tuple(l) for l in [[0] * 10 * N,
1599+
[1] * 10 * N,
1600+
[2] * 10 * N,
1601+
[np.nan] * N + [2] * 9 * N,
1602+
[0] * N + [2] * 9 * N,
1603+
[np.nan] * N + [2] * 8 * N + [0] * N]]
1604+
# Each level contains 4 elements (including NaN), so it is represented
1605+
# in 2 bits, for a total of 2*N*10 = 100 > 64 bits. If we were using a
1606+
# 64 bit engine and truncating the first levels, the fourth and fifth
1607+
# keys would collide; if truncating the last levels, the fifth and
1608+
# sixth; if rotating bits rather than shifting, the third and fifth.
1609+
1610+
for idx in range(len(keys)):
1611+
index = MultiIndex.from_tuples(keys)
1612+
assert index.get_loc(keys[idx]) == idx
1613+
1614+
expected = np.arange(idx + 1, dtype='int64')
1615+
result = index.get_indexer([keys[i] for i in expected])
1616+
tm.assert_numpy_array_equal(result, expected)
1617+
1618+
# With missing key:
1619+
idces = range(len(keys))
1620+
expected = np.array([-1] + list(idces), dtype='int64')
1621+
missing = tuple([0, 1] * 5 * N)
1622+
result = index.get_indexer([missing] + [keys[i] for i in idces])
1623+
tm.assert_numpy_array_equal(result, expected)
1624+
15931625
def test_getitem_lowerdim_corner(self):
15941626
pytest.raises(KeyError, self.frame.loc.__getitem__,
15951627
(('bar', 'three'), 'B'))

0 commit comments

Comments
 (0)