8000 CLN: Address MulitIndex Test Follow Ups in Issue #21918 by elmq0022 · Pull Request #21928 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

CLN: Address MulitIndex Test Follow Ups in Issue #21918 #21928

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

Merged
merged 3 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
additional refactoring
  • Loading branch information
elmq0022 committed Jul 22, 2018
commit e993d4da6d8afcb5150ee6024ebefaebe75e62f8
116 changes: 10 additions & 106 deletions pandas/tests/indexes/multi/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
import pandas as pd
import pandas.util.testing as tm
import pytest
from pandas import (DatetimeIndex, Index, MultiIndex,
PeriodIndex, TimedeltaIndex, date_range,
period_range)
from pandas.compat import lrange, range
from pandas.core.dtypes.dtypes import CategoricalDtype
from pandas import Index, MultiIndex, date_range, period_range
from pandas.compat import lrange


def test_shift(idx):
Expand All @@ -17,91 +14,11 @@ def test_shift(idx):
pytest.raises(NotImplementedError, idx.shift, 1)
pytest.raises(NotImplementedError, idx.shift, 1, 2)

# TODO: reshape
def test_insert(idx):
# key contained in all levels
new_index = idx.insert(0, ('bar', 'two'))
assert new_index.equal_levels(idx)
assert new_index[0] == ('bar', 'two')

# key not contained in all levels
new_index = idx.insert(0, ('abc', 'three'))

exp0 = Index(list(idx.levels[0]) + ['abc'], name='first')
tm.assert_index_equal(new_index.levels[0], exp0)

exp1 = Index(list(idx.levels[1]) + ['three'], name='second')
tm.assert_index_equal(new_index.levels[1], exp1)
assert new_index[0] == ('abc', 'three')

# key wrong length
msg = "Item must have length equal to number of levels"
with tm.assert_raises_regex(ValueError, msg):
idx.insert(0, ('foo2',))

left = pd.DataFrame([['a', 'b', 0], ['b', 'd', 1]],
columns=['1st', '2nd', '3rd'])
left.set_index(['1st', '2nd'], inplace=True)
ts = left['3rd'].copy(deep=True)

left.loc[('b', 'x'), '3rd'] = 2
left.loc[('b', 'a'), '3rd'] = -1
left.loc[('b', 'b'), '3rd'] = 3
left.loc[('a', 'x'), '3rd'] = 4
left.loc[('a', 'w'), '3rd'] = 5
left.loc[('a', 'a'), '3rd'] = 6

ts.loc[('b', 'x')] = 2
ts.loc['b', 'a'] = -1
ts.loc[('b', 'b')] = 3
ts.loc['a', 'x'] = 4
ts.loc[('a', 'w')] = 5
ts.loc['a', 'a'] = 6

right = pd.DataFrame([['a', 'b', 0], ['b', 'd', 1], ['b', 'x', 2],
['b', 'a', -1], ['b', 'b', 3], ['a', 'x', 4],
['a', 'w', 5], ['a', 'a', 6]],
columns=['1st', '2nd', '3rd'])
right.set_index(['1st', '2nd'], inplace=True)
# FIXME data types changes to float because
# of intermediate nan insertion;
tm.assert_frame_equal(left, right, check_dtype=False)
tm.assert_series_equal(ts, right['3rd'])

# GH9250
idx = [('test1', i) for i in range(5)] + \
[('test2', i) for i in range(6)] + \
[('test', 17), ('test', 18)]

left = pd.Series(np.linspace(0, 10, 11),
pd.MultiIndex.from_tuples(idx[:-2]))

left.loc[('test', 17)] = 11
left.loc[('test', 18)] = 12

right = pd.Series(np.linspace(0, 12, 13),
pd.MultiIndex.from_tuples(idx))

tm.assert_series_equal(left, right)


def test_bounds(idx):
idx._bounds

# TODO: reshape
def test_append(idx):
result = idx[:3].append(idx[3:])
assert result.equals(idx)

foos = [idx[:1], idx[1:3], idx[3:]]
result = foos[0].append(foos[1:])
assert result.equals(idx)

# empty
result = idx.append([])
assert result.equals(idx)

# TODO: reshape
def test_groupby(idx):
groups = idx.groupby(np.array([1, 1, 1, 2, 2, 2]))
labels = idx.get_values().tolist()
Expand Down Expand Up @@ -159,28 +76,14 @@ def f():
pytest.raises(NotImplementedError, f)

# TODO: reshape


def test_reorder_levels(idx):
# this blows up
tm.assert_raises_regex(IndexError, '^Too many levels',
idx.reorder_levels, [2, 1, 0])


def test_repeat():
reps = 2
numbers = [1, 2, 3]
names = np.array(['foo', 'bar'])

m = MultiIndex.from_product([
numbers, names], names=names)
expected = MultiIndex.from_product([
numbers, names.repeat(reps)], names=names)
tm.assert_index_equal(m.repeat(reps), expected)

with tm.assert_produces_warning(FutureWarning):
result = m.repeat(n=reps)
tm.assert_index_equal(result, expected)


def test_numpy_repeat():
reps = 2
numbers = [1, 2, 3]
Expand Down Expand Up @@ -238,11 +141,12 @@ def test_take(idx):
expected = idx[indexer]
assert result.equals(expected)

if not isinstance(idx,
(DatetimeIndex, PeriodIndex, TimedeltaIndex)):
# GH 10791
with pytest.raises(AttributeError):
idx.freq
# TODO: Remove Commented Code
# if not isinstance(idx,
# (DatetimeIndex, PeriodIndex, TimedeltaIndex)):
# GH 10791
with pytest.raises(AttributeError):
idx.freq


def test_take_invalid_kwargs(idx):
Expand Down
2 changes: 2 additions & 0 deletions pandas/tests/indexes/multi/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import numpy as np
import pandas.util.testing as tm
import pytest
from pandas.util.testing import assert_copy
from pandas.core.dtypes.dtypes import CategoricalDtype


def test_astype(idx):
Expand Down
26 changes: 18 additions & 8 deletions pandas/tests/indexes/multi/test_constructor.py
628C
Original file line number Diff line number Diff line change
Expand Up @@ -343,19 +343,29 @@ def test_from_product_empty_three_levels(N):
tm.assert_index_equal(result, expected)


def test_from_product_invalid_input():
invalid_inputs = [1, [1], [1, 2], [[1], 2],
'a', ['a'], ['a', 'b'], [['a'], 'b']]
for i in invalid_inputs:
pytest.raises(TypeError, MultiIndex.from_product, iterables=i)
@pytest.mark.parametrize('invalid_input', [
1,
[1],
[1, 2],
[[1], 2],
'a',
['a'],
['a', 'b'],
[['a'], 'b'],
])
def test_from_product_invalid_input(invalid_input):
pytest.raises(TypeError, MultiIndex.from_product, iterables=invalid_input)


def test_from_product_datetimeindex():
dt_index = date_range('2000-01-01', periods=2)
mi = pd.MultiIndex.from_product([[1, 2], dt_index])
etalon = construct_1d_object_array_from_listlike([(1, pd.Timestamp(
'2000-01-01')), (1, pd.Timestamp('2000-01-02')), (2, pd.Timestamp(
'2000-01-01')), (2, pd.Timestamp('2000-01-02'))])
etalon = construct_1d_object_array_from_listlike([
(1, pd.Timestamp('2000-01-01')),
(1, pd.Timestamp('2000-01-02')),
(2, pd.Timestamp('2000-01-01')),
(2, pd.Timestamp('2000-01-02')),
])
tm.assert_numpy_array_equal(mi.values, etalon)


Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/indexes/multi/test_contains.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ def test_isin_nan_pypy():
def test_isin():
values = [('foo', 2), ('bar', 3), ('quux', 4)]

idx = MultiIndex.from_arrays([['qux', 'baz', 'foo', 'bar'], np.arange(
4)])
idx = MultiIndex.from_arrays([
['qux', 'baz', 'foo', 'bar'],
np.arange(4)
])
result = idx.isin(values)
expected = np.array([False, False, True, True])
tm.assert_numpy_array_equal(result, expected)
Expand Down
34 changes: 15 additions & 19 deletions pandas/tests/indexes/multi/test_equivalence.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,25 @@
import numpy as np
import pandas as pd
import pandas.util.testing as tm
from pandas import Index, MultiIndex, RangeIndex, Series, compat
from pandas import Index, MultiIndex, Series
from pandas.compat import lrange, lzip, range


def test_equals(idx):
# TODO: Remove or Refactor. MultiIndex not tested.
for name, idx in compat.iteritems({'idx': idx}):
assert idx.equals(idx)
assert idx.equals(idx.copy())
assert idx.equals(idx.astype(object))

assert not idx.equals(list(idx))
assert not idx.equals(np.array(idx))

# Cannot pass in non-int64 dtype to RangeIndex
if not isinstance(idx, RangeIndex):
same_values = Index(idx, dtype=object)
assert idx.equals(same_values)
assert same_values.equals(idx)

if idx.nlevels == 1:
# do not test MultiIndex
assert not idx.equals(pd.Series(idx))
assert idx.equals(idx)
assert idx.equals(idx.copy())
assert idx.equals(idx.astype(object))

assert not idx.equals(list(idx))
assert not idx.equals(np.array(idx))

same_values = Index(idx, dtype=object)
assert idx.equals(same_values)
assert same_values.equals(idx)

if idx.nlevels == 1:
# do not test MultiIndex
assert not idx.equals(pd.Series(idx))


def test_equals_op(idx):
Expand Down
26 changes: 0 additions & 26 deletions pandas/tests/indexes/multi/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,32 +108,6 @@ def test_slice_locs_not_contained():
result = index.slice_locs(-1, 10)
assert result == (0, len(index))

# TODO: reshape
def test_insert_base(idx):

result = idx[1:4]

# test 0th element
assert idx[0:4].equals(result.insert(0, idx[0]))


# TODO: reshape
def test_delete_base(idx):

expected = idx[1:]
result = idx.delete(0)
assert result.equals(expected)
assert result.name == expected.name

expected = idx[:-1]
result = idx.delete(-1)
assert result.equals(expected)
assert result.name == expected.name

with pytest.raises((IndexError, ValueError)):
# either depending on numpy version
result = idx.delete(len(idx))


def test_putmask_with_wrong_mask(idx):
# GH18368
Expand Down
9 changes: 5 additions & 4 deletions pandas/tests/indexes/multi/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
from pandas import Index, MultiIndex


@pytest.mark.parametrize('other',
[Index(['three', 'one', 'two']),
Index(['one']),
Index(['one', 'three'])])
@pytest.mark.parametrize('other', [
Index(['three', 'one', 'two']),
Index(['one']),
Index(['one', 'three']),
])
def test_join_level(idx, other, join_type):
join_index, lidx, ridx = other.join(idx, how=join_type,
level='second',
Expand Down
26 changes: 4 additions & 22 deletions pandas/tests/indexes/multi/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pandas as pd
import pandas.util.testing as tm
import pytest
from pandas import Int64Index, MultiIndex, PeriodIndex, UInt64Index, isna
from pandas import Int64Index, MultiIndex, PeriodIndex, UInt64Index
from pandas._libs.tslib import iNaT
from pandas.core.indexes.datetimelike import DatetimeIndexOpsMixin

Expand Down Expand Up @@ -78,27 +78,9 @@ def test_nulls(idx):
# this is really a smoke test for the methods
# as these are adequately tested for function elsewhere

# TODO: Remove or Refactor. MultiIndex not Implemeted.
for name, index in [('idx', idx), ]:
if len(index) == 0:
tm.assert_numpy_array_equal(
index.isna(), np.array([], dtype=bool))
elif isinstance(index, MultiIndex):
idx = index.copy()
msg = "isna is not defined for MultiIndex"
with tm.assert_raises_regex(NotImplementedError, msg):
idx.isna()
else:

if not index.hasnans:
tm.assert_numpy_array_equal(
index.isna(), np.zeros(len(index), dtype=bool))
tm.assert_numpy_array_equal(
index.notna(), np.ones(len(index), dtype=bool))
else:
result = isna(index)
tm.assert_numpy_array_equal(index.isna(), result)
tm.assert_numpy_array_equal(index.notna(), ~result)
msg = "isna is not defined for MultiIndex"
with tm.assert_raises_regex(NotImplementedError, msg):
idx.isna()


@pytest.mark.xfail
Expand Down
Loading
0