-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
TST: move mi tests from tests/indexing/ to tests/indexing/multiindex/ #24417
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0632a4a
move tests from test_chaining_and_caching.py
simonjayhawkins 70d0236
move test from test_iloc.py
simonjayhawkins 0b767ad
move mi tests from test_indexing_slow.py
simonjayhawkins 34d5e40
move mi tests from test_indexing.py
simonjayhawkins c1fbe98
move mi test from test_ix.py
simonjayhawkins 01c9401
move mi tests from test_loc.py
simonjayhawkins 01fd0a5
suppress FutureWarning for Panel
simonjayhawkins a73e87c
Merge remote-tracking branch 'upstream/master' into move-mi
simonjayhawkins 2b6a8d4
Merge remote-tracking branch 'upstream/master' into move-mi
simonjayhawkins f7c2528
remove class from test_indexing_slow.py
simonjayhawkins 5d1c4f8
revert suppress Panel FutureWarning
simonjayhawkins a6d542b
remove commented out code
simonjayhawkins 336d061
refactor import
simonjayhawkins fe9ee29
refactor import
simonjayhawkins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
pandas/tests/indexing/multiindex/test_chaining_and_caching.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from pandas.compat import lrange, lzip, range | ||
|
||
from pandas import DataFrame, MultiIndex, Series | ||
from pandas.core import common as com | ||
import pandas.util.testing as tm | ||
|
||
|
||
def test_detect_chained_assignment(): | ||
# Inplace ops, originally from: | ||
# http://stackoverflow.com/questions/20508968/series-fillna-in-a-multiindex-dataframe-does-not-fill-is-this-a-bug | ||
a = [12, 23] | ||
b = [123, None] | ||
c = [1234, 2345] | ||
d = [12345, 23456] | ||
tuples = [('eyes', 'left'), ('eyes', 'right'), ('ears', 'left'), | ||
('ears', 'right')] | ||
events = {('eyes', 'left'): a, | ||
('eyes', 'right'): b, | ||
('ears', 'left'): c, | ||
('ears', 'right'): d} | ||
multiind = MultiIndex.from_tuples(tuples, names=['part', 'side']) | ||
zed = DataFrame(events, index=['a', 'b'], columns=multiind) | ||
|
||
with pytest.raises(com.SettingWithCopyError): | ||
zed['eyes']['right'].fillna(value=555, inplace=True) | ||
|
||
|
||
def test_cache_updating(): | ||
# 5216 | ||
# make sure that we don't try to set a dead cache | ||
a = np.random.rand(10, 3) | ||
df = DataFrame(a, columns=['x', 'y', 'z']) | ||
tuples = [(i, j) for i in range(5) for j in range(2)] | ||
index = MultiIndex.from_tuples(tuples) | ||
df.index = index | ||
|
||
# setting via chained assignment | ||
# but actually works, since everything is a view | ||
df.loc[0]['z'].iloc[0] = 1. | ||
result = df.loc[(0, 0), 'z'] | ||
assert result == 1 | ||
|
||
# correct setting | ||
df.loc[(0, 0), 'z'] = 2 | ||
result = df.loc[(0, 0), 'z'] | ||
assert result == 2 | ||
|
||
|
||
def test_indexer_caching(): | ||
# GH5727 | ||
# make sure that indexers are in the _internal_names_set | ||
n = 1000001 | ||
arrays = [lrange(n), lrange(n)] | ||
index = MultiIndex.from_tuples(lzip(*arrays)) | ||
s = Series(np.zeros(n), index=index) | ||
str(s) | ||
|
||
# setitem | ||
expected = Series(np.ones(n), index=index) | ||
s = Series(np.zeros(n), index=index) | ||
s[s == 0] = 1 | ||
tm.assert_series_equal(s, expected) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import warnings | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
import pandas as pd | ||
from pandas import DataFrame, MultiIndex, Series | ||
import pandas.util.testing as tm | ||
|
||
|
||
@pytest.mark.slow | ||
@pytest.mark.filterwarnings("ignore::pandas.errors.PerformanceWarning") | ||
def test_multiindex_get_loc(): # GH7724, GH2646 | ||
|
||
with warnings.catch_warnings(record=True): | ||
|
||
# test indexing into a multi-index before & past the lexsort depth | ||
from numpy.random import randint, choice, randn | ||
cols = ['jim', 'joe', 'jolie', 'joline', 'jolia'] | ||
|
||
def validate(mi, df, key): | ||
mask = np.ones(len(df)).astype('bool') | ||
|
||
# test for all partials of this key | ||
for i, k in enumerate(key): | ||
mask &= df.iloc[:, i] == k | ||
|
||
if not mask.any(): | ||
assert key[:i + 1] not in mi.index | ||
continue | ||
|
||
assert key[:i + 1] in mi.index | ||
right = df[mask].copy() | ||
|
||
if i + 1 != len(key): # partial key | ||
right.drop(cols[:i + 1], axis=1, inplace=True) | ||
right.set_index(cols[i + 1:-1], inplace=True) | ||
tm.assert_frame_equal(mi.loc[key[:i + 1]], right) | ||
|
||
else: # full key | ||
right.set_index(cols[:-1], inplace=True) | ||
if len(right) == 1: # single hit | ||
right = Series(right['jolia'].values, | ||
name=right.index[0], | ||
index=['jolia']) | ||
tm.assert_series_equal(mi.loc[key[:i + 1]], right) | ||
else: # multi hit | ||
tm.assert_frame_equal(mi.loc[key[:i + 1]], right) | ||
|
||
def loop(mi, df, keys): | ||
for key in keys: | ||
validate(mi, df, key) | ||
|
||
n, m = 1000, 50 | ||
|
||
vals = [randint(0, 10, n), choice( | ||
list('abcdefghij'), n), choice( | ||
pd.date_range('20141009', periods=10).tolist(), n), choice( | ||
list('ZYXWVUTSRQ'), n), randn(n)] | ||
vals = list(map(tuple, zip(*vals))) | ||
|
||
# bunch of keys for testing | ||
keys = [randint(0, 11, m), choice( | ||
list('abcdefghijk'), m), choice( | ||
pd.date_range('20141009', periods=11).tolist(), m), choice( | ||
list('ZYXWVUTSRQP'), m)] | ||
keys = list(map(tuple, zip(*keys))) | ||
keys += list(map(lambda t: t[:-1], vals[::n // m])) | ||
|
||
# covers both unique index and non-unique index | ||
df = DataFrame(vals, columns=cols) | ||
a, b = pd.concat([df, df]), df.drop_duplicates(subset=cols[:-1]) | ||
|
||
for frame in a, b: | ||
for i in range(5): # lexsort depth | ||
df = frame.copy() if i == 0 else frame.sort_values( | ||
by=cols[:i]) | ||
mi = df.set_index(cols[:-1]) | ||
assert not mi.index.lexsort_depth < i | ||
loop(mi, df, keys) | ||
|
||
|
||
@pytest.mark.slow | ||
def test_large_mi_dataframe_indexing(): | ||
# GH10645 | ||
result = MultiIndex.from_arrays([range(10 ** 6), range(10 ** 6)]) | ||
assert (not (10 ** 6, 0) in result) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ideally would like to split this massive test up, but as a followup