-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DEPR: remove Panel-specific parts of core.indexing #25567
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
Changes from all commits
c47d36f
6e485bd
348c595
451a967
63495a6
cc22e5f
bc505bc
f39a75f
c1c200e
fac9442
be19f78
26f2ac2
1de10b5
8d2ab43
496ab7d
29cde25
eaf161b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,11 +11,14 @@ | |
|
||
import pandas as pd | ||
from pandas import DataFrame, Index, NaT, Series | ||
from pandas.core.generic import NDFrame | ||
from pandas.core.indexing import ( | ||
_maybe_numeric_slice, _non_reducing_slice, validate_indices) | ||
from pandas.tests.indexing.common import Base, _mklbl | ||
import pandas.util.testing as tm | ||
|
||
ignore_ix = pytest.mark.filterwarnings("ignore:\\n.ix:FutureWarning") | ||
|
||
# ------------------------------------------------------------------------ | ||
# Indexing test cases | ||
|
||
|
@@ -53,6 +56,93 @@ def test_setitem_ndarray_1d(self): | |
with pytest.raises(ValueError): | ||
df[2:5] = np.arange(1, 4) * 1j | ||
|
||
@pytest.mark.parametrize('index', tm.all_index_generator(5), | ||
ids=lambda x: type(x).__name__) | ||
@pytest.mark.parametrize('obj', [ | ||
lambda i: Series(np.arange(len(i)), index=i), | ||
lambda i: DataFrame( | ||
np.random.randn(len(i), len(i)), index=i, columns=i) | ||
], ids=['Series', 'DataFrame']) | ||
@pytest.mark.parametrize('idxr, idxr_id', [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. might be worth making these a fixture at somepoint (the iteration over indexers) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
yeah. i appreciate we have duplication of parameterization at the moment, and fixtures may be of use. didn't carry on down that route at the moment as i wasn't sure if these tests were what was required. separately for iteration over the indexes: xref #25748 (comment). There is a |
||
(lambda x: x, 'getitem'), | ||
(lambda x: x.loc, 'loc'), | ||
(lambda x: x.iloc, 'iloc'), | ||
pytest.param(lambda x: x.ix, 'ix', marks=ignore_ix) | ||
]) | ||
def test_getitem_ndarray_3d(self, index, obj, idxr, idxr_id): | ||
# GH 25567 | ||
obj = obj(index) | ||
idxr = idxr(obj) | ||
nd3 = np.random.randint(5, size=(2, 2, 2)) | ||
|
||
msg = (r"Buffer has wrong number of dimensions \(expected 1," | ||
r" got 3\)|" | ||
"The truth value of an array with more than one element is" | ||
" ambiguous|" | ||
"Cannot index with multidimensional key|" | ||
r"Wrong number of dimensions. values.ndim != ndim \[3 != 1\]|" | ||
"unhashable type: 'numpy.ndarray'" # TypeError | ||
) | ||
|
||
if (isinstance(obj, Series) and idxr_id == 'getitem' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ugg this is why getitem is so complicated. |
||
and index.inferred_type in [ | ||
'string', 'datetime64', 'period', 'timedelta64', | ||
'boolean', 'categorical']): | ||
idxr[nd3] | ||
else: | ||
if (isinstance(obj, DataFrame) and idxr_id == 'getitem' | ||
and index.inferred_type == 'boolean'): | ||
error = TypeError | ||
else: | ||
error = ValueError | ||
|
||
with pytest.raises(error, match=msg): | ||
idxr[nd3] | ||
|
||
@pytest.mark.parametrize('index', tm.all_index_generator(5), | ||
ids=lambda x: type(x).__name__) | ||
@pytest.mark.parametrize('obj', [ | ||
lambda i: Series(np.arange(len(i)), index=i), | ||
lambda i: DataFrame( | ||
np.random.randn(len(i), len(i)), index=i, columns=i) | ||
], ids=['Series', 'DataFrame']) | ||
@pytest.mark.parametrize('idxr, idxr_id', [ | ||
(lambda x: x, 'setitem'), | ||
(lambda x: x.loc, 'loc'), | ||
(lambda x: x.iloc, 'iloc'), | ||
pytest.param(lambda x: x.ix, 'ix', marks=ignore_ix) | ||
]) | ||
def test_setitem_ndarray_3d(self, index, obj, idxr, idxr_id): | ||
# GH 25567 | ||
obj = obj(index) | ||
idxr = idxr(obj) | ||
nd3 = np.random.randint(5, size=(2, 2, 2)) | ||
|
||
msg = (r"Buffer has wrong number of dimensions \(expected 1," | ||
r" got 3\)|" | ||
"The truth value of an array with more than one element is" | ||
" ambiguous|" | ||
"Only 1-dimensional input arrays are supported|" | ||
"'pandas._libs.interval.IntervalTree' object has no attribute" | ||
" 'set_value'|" # AttributeError | ||
"unhashable type: 'numpy.ndarray'|" # TypeError | ||
r"^\[\[\[" # pandas.core.indexing.IndexingError | ||
) | ||
|
||
if ((idxr_id == 'iloc') | ||
or ((isinstance(obj, Series) and idxr_id == 'setitem' | ||
and index.inferred_type in [ | ||
'floating', 'string', 'datetime64', 'period', 'timedelta64', | ||
'boolean', 'categorical'])) | ||
or (idxr_id == 'ix' and index.inferred_type in [ | ||
'string', 'datetime64', 'period', 'boolean'])): | ||
idxr[nd3] = 0 | ||
else: | ||
with pytest.raises( | ||
(ValueError, AttributeError, TypeError, | ||
pd.core.indexing.IndexingError), match=msg): | ||
idxr[nd3] = 0 | ||
|
||
def test_inf_upcast(self): | ||
# GH 16957 | ||
# We should be able to use np.inf as a key | ||
|
@@ -1015,3 +1105,26 @@ def test_extension_array_cross_section_converts(): | |
|
||
result = df.iloc[0] | ||
tm.assert_series_equal(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize('idxr, error, error_message', [ | ||
(lambda x: x, | ||
AttributeError, | ||
"'numpy.ndarray' object has no attribute 'get'"), | ||
(lambda x: x.loc, | ||
AttributeError, | ||
"type object 'NDFrame' has no attribute '_AXIS_ALIASES'"), | ||
(lambda x: x.iloc, | ||
AttributeError, | ||
"type object 'NDFrame' has no attribute '_AXIS_ALIASES'"), | ||
pytest.param( | ||
lambda x: x.ix, | ||
ValueError, | ||
"NDFrameIndexer does not support NDFrame objects with ndim > 2", | ||
marks=ignore_ix) | ||
]) | ||
def test_ndframe_indexing_raises(idxr, error, error_message): | ||
# GH 25567 | ||
frame = NDFrame(np.random.randint(5, size=(2, 2, 2))) | ||
with pytest.raises(error, match=error_message): | ||
idxr(frame)[0] |
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.
I think we are going to need a new error if self.obj.ndim.ndim > 2 (e.g. a numpy array); need to check both for setting & getting (it *might be already there but not sure about tests)