Skip to content

TST: tests for GH5873 #9169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import pandas as pd
from pandas.lib import Timestamp
from itertools import product


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

# GH5873
for a in [101, 102]:
mi = MultiIndex.from_arrays([[101, a], [3.5, np.nan]])
self.assertFalse(mi.has_duplicates)
self.assertEqual(mi.get_duplicates(), [])
self.assert_array_equal(mi.duplicated(), np.zeros(2, dtype='bool'))

for n in range(1, 6): # 1st level shape
for m in range(1, 5): # 2nd level shape
# all possible unique combinations, including nan
lab = product(range(-1, n), range(-1, m))
mi = MultiIndex(levels=[list('abcde')[:n], list('WXYZ')[:m]],
labels=np.random.permutation(list(lab)).T)
self.assertEqual(len(mi), (n + 1) * (m + 1))
self.assertFalse(mi.has_duplicates)
self.assertEqual(mi.get_duplicates(), [])
self.assert_array_equal(mi.duplicated(),
np.zeros(len(mi), dtype='bool'))

def test_tolist(self):
result = self.index.tolist()
exp = list(self.index.values)
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -5884,6 +5884,24 @@ def test_unstack(self):
unstacked = s.unstack(0)
assert_frame_equal(unstacked, expected)

# GH5873
idx = pd.MultiIndex.from_arrays([[101, 102], [3.5, np.nan]])
ts = pd.Series([1,2], index=idx)
left = ts.unstack()
left.columns = left.columns.astype('float64')
right = DataFrame([[nan, 1], [2, nan]], index=[101, 102],
columns=[nan, 3.5])
assert_frame_equal(left, right)

idx = pd.MultiIndex.from_arrays([['cat', 'cat', 'cat', 'dog', 'dog'],
['a', 'a', 'b', 'a', 'b'], [1, 2, 1, 1, np.nan]])
ts = pd.Series([1.0, 1.1, 1.2, 1.3, 1.4], index=idx)
right = DataFrame([[1.0, 1.3], [1.1, nan], [nan, 1.4], [1.2, nan]],
columns=['cat', 'dog'])
tpls = [('a', 1), ('a', 2), ('b', nan), ('b', 1)]
right.index = pd.MultiIndex.from_tuples(tpls)
assert_frame_equal(ts.unstack(level=0), right)

def test_sortlevel(self):
mi = MultiIndex.from_tuples([[1, 1, 3], [1, 1, 1]], names=list('ABC'))
s = Series([1, 2], mi)
Expand Down