Skip to content

Commit 28fc02a

Browse files
behzadnourijreback
authored andcommitted
tests for GH5873
1 parent 4aa0e0a commit 28fc02a

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

doc/source/whatsnew/v0.16.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Bug Fixes
9292

9393

9494

95-
- Bug in ``MultiIndex.has_duplicates`` when having many levels causes an indexer overflow (:issue:`9075`)
95+
- Bug in ``MultiIndex.has_duplicates`` when having many levels causes an indexer overflow (:issue:`9075`, :issue:`5873`)
9696
- Bug in ``pivot`` and `unstack`` where ``nan`` values would break index alignment (:issue:`7466`)
9797

9898

pandas/tests/test_index.py

+20
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import pandas as pd
3535
from pandas.lib import Timestamp
36+
from itertools import product
3637

3738

3839
class Base(object):
@@ -3525,6 +3526,25 @@ def check(nlevels, with_nulls):
35253526
right = pd.lib.duplicated(mi.values, take_last=take_last)
35263527
tm.assert_array_equal(left, right)
35273528

3529+
# GH5873
3530+
for a in [101, 102]:
3531+
mi = MultiIndex.from_arrays([[101, a], [3.5, np.nan]])
3532+
self.assertFalse(mi.has_duplicates)
3533+
self.assertEqual(mi.get_duplicates(), [])
3534+
self.assert_array_equal(mi.duplicated(), np.zeros(2, dtype='bool'))
3535+
3536+
for n in range(1, 6): # 1st level shape
3537+
for m in range(1, 5): # 2nd level shape
3538+
# all possible unique combinations, including nan
3539+
lab = product(range(-1, n), range(-1, m))
3540+
mi = MultiIndex(levels=[list('abcde')[:n], list('WXYZ')[:m]],
3541+
labels=np.random.permutation(list(lab)).T)
3542+
self.assertEqual(len(mi), (n + 1) * (m + 1))
3543+
self.assertFalse(mi.has_duplicates)
3544+
self.assertEqual(mi.get_duplicates(), [])
3545+
self.assert_array_equal(mi.duplicated(),
3546+
np.zeros(len(mi), dtype='bool'))
3547+
35283548
def test_tolist(self):
35293549
result = self.index.tolist()
35303550
exp = list(self.index.values)

pandas/tests/test_series.py

+18
Original file line numberDiff line numberDiff line change
@@ -5884,6 +5884,24 @@ def test_unstack(self):
58845884
unstacked = s.unstack(0)
58855885
assert_frame_equal(unstacked, expected)
58865886

5887+
# GH5873
5888+
idx = pd.MultiIndex.from_arrays([[101, 102], [3.5, np.nan]])
5889+
ts = pd.Series([1,2], index=idx)
5890+
left = ts.unstack()
5891+
left.columns = left.columns.astype('float64')
5892+
right = DataFrame([[nan, 1], [2, nan]], index=[101, 102],
5893+
columns=[nan, 3.5])
5894+
assert_frame_equal(left, right)
5895+
5896+
idx = pd.MultiIndex.from_arrays([['cat', 'cat', 'cat', 'dog', 'dog'],
5897+
['a', 'a', 'b', 'a', 'b'], [1, 2, 1, 1, np.nan]])
5898+
ts = pd.Series([1.0, 1.1, 1.2, 1.3, 1.4], index=idx)
5899+
right = DataFrame([[1.0, 1.3], [1.1, nan], [nan, 1.4], [1.2, nan]],
5900+
columns=['cat', 'dog'])
5901+
tpls = [('a', 1), ('a', 2), ('b', nan), ('b', 1)]
5902+
right.index = pd.MultiIndex.from_tuples(tpls)
5903+
assert_frame_equal(ts.unstack(level=0), right)
5904+
58875905
def test_sortlevel(self):
58885906
mi = MultiIndex.from_tuples([[1, 1, 3], [1, 1, 1]], names=list('ABC'))
58895907
s = Series([1, 2], mi)

0 commit comments

Comments
 (0)