Skip to content

DEPR: remove .ix tests from tests/indexing/test_floats.py #27533

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 2 commits into from
Jul 23, 2019
Merged
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
150 changes: 39 additions & 111 deletions pandas/tests/indexing/test_floats.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
from warnings import catch_warnings

import numpy as np
import pytest

from pandas import DataFrame, Float64Index, Index, Int64Index, RangeIndex, Series
import pandas.util.testing as tm
from pandas.util.testing import assert_almost_equal, assert_series_equal

ignore_ix = pytest.mark.filterwarnings("ignore:\\n.ix:FutureWarning")


class TestFloatIndexers:
def check(self, result, original, indexer, getitem):
Expand Down Expand Up @@ -62,7 +58,6 @@ def test_scalar_error(self):
with pytest.raises(TypeError, match=msg):
s.iloc[3.0] = 0

@ignore_ix
def test_scalar_non_numeric(self):

# GH 4892
Expand All @@ -86,11 +81,7 @@ def test_scalar_non_numeric(self):
]:

# getting
for idxr, getitem in [
(lambda x: x.ix, False),
(lambda x: x.iloc, False),
(lambda x: x, True),
]:
for idxr, getitem in [(lambda x: x.iloc, False), (lambda x: x, True)]:

# gettitem on a DataFrame is a KeyError as it is indexing
# via labels on the columns
Expand All @@ -106,9 +97,8 @@ def test_scalar_non_numeric(self):
"Cannot index by location index with a"
" non-integer key".format(klass=type(i), kind=str(float))
)
with catch_warnings(record=True):
with pytest.raises(error, match=msg):
idxr(s)[3.0]
with pytest.raises(error, match=msg):
idxr(s)[3.0]

# label based can be a TypeError or KeyError
if s.index.inferred_type in ["string", "unicode", "mixed"]:
Expand Down Expand Up @@ -158,10 +148,9 @@ def test_scalar_non_numeric(self):
s2.loc[3.0] = 10
assert s2.index.is_object()

for idxr in [lambda x: x.ix, lambda x: x]:
for idxr in [lambda x: x]:
s2 = s.copy()
with catch_warnings(record=True):
idxr(s2)[3.0] = 0
idxr(s2)[3.0] = 0
assert s2.index.is_object()

# fallsback to position selection, series only
Expand All @@ -175,15 +164,14 @@ def test_scalar_non_numeric(self):
with pytest.raises(TypeError, match=msg):
s[3.0]

@ignore_ix
def test_scalar_with_mixed(self):

s2 = Series([1, 2, 3], index=["a", "b", "c"])
s3 = Series([1, 2, 3], index=["a", "b", 1.5])

# lookup in a pure stringstr
# with an invalid indexer
for idxr in [lambda x: x.ix, lambda x: x, lambda x: x.iloc]:
for idxr in [lambda x: x, lambda x: x.iloc]:

msg = (
r"cannot do label indexing"
Expand All @@ -193,9 +181,8 @@ def test_scalar_with_mixed(self):
klass=str(Index), kind=str(float)
)
)
with catch_warnings(record=True):
with pytest.raises(TypeError, match=msg):
idxr(s2)[1.0]
with pytest.raises(TypeError, match=msg):
idxr(s2)[1.0]

with pytest.raises(KeyError, match=r"^1$"):
s2.loc[1.0]
Expand All @@ -220,23 +207,6 @@ def test_scalar_with_mixed(self):
expected = 2
assert result == expected

# mixed index so we have label
# indexing
for idxr in [lambda x: x.ix]:
with catch_warnings(record=True):

msg = (
r"cannot do label indexing"
r" on {klass} with these indexers \[1\.0\] of"
r" {kind}".format(klass=str(Index), kind=str(float))
)
with pytest.raises(TypeError, match=msg):
idxr(s3)[1.0]

result = idxr(s3)[1]
expected = 2
assert result == expected

msg = "Cannot index by location index with a non-integer key"
with pytest.raises(TypeError, match=msg):
s3.iloc[1.0]
Expand All @@ -247,7 +217,6 @@ def test_scalar_with_mixed(self):
expected = 3
assert result == expected

@ignore_ix
def test_scalar_integer(self):

# test how scalar float indexers work on int indexes
Expand All @@ -261,22 +230,13 @@ def test_scalar_integer(self):
]:

# coerce to equal int
for idxr, getitem in [
(lambda x: x.ix, False),
(lambda x: x.loc, False),
(lambda x: x, True),
]:
for idxr, getitem in [(lambda x: x.loc, False), (lambda x: x, True)]:

with catch_warnings(record=True):
result = idxr(s)[3.0]
result = idxr(s)[3.0]
self.check(result, s, 3, getitem)

# coerce to equal int
for idxr, getitem in [
(lambda x: x.ix, False),
(lambda x: x.loc, False),
(lambda x: x, True),
]:
for idxr, getitem in [(lambda x: x.loc, False), (lambda x: x, True)]:

if isinstance(s, Series):

Expand All @@ -292,20 +252,18 @@ def compare(x, y):
expected = Series(100.0, index=range(len(s)), name=3)

s2 = s.copy()
with catch_warnings(record=True):
idxr(s2)[3.0] = 100
idxr(s2)[3.0] = 100

result = idxr(s2)[3.0]
compare(result, expected)
result = idxr(s2)[3.0]
compare(result, expected)

result = idxr(s2)[3]
compare(result, expected)
result = idxr(s2)[3]
compare(result, expected)

# contains
# coerce to equal int
assert 3.0 in s

@ignore_ix
def test_scalar_float(self):

# scalar float indexers work on a float index
Expand All @@ -319,11 +277,7 @@ def test_scalar_float(self):

# assert all operations except for iloc are ok
indexer = index[3]
for idxr, getitem in [
(lambda x: x.ix, False),
(lambda x: x.loc, False),
(lambda x: x, True),
]:
for idxr, getitem in [(lambda x: x.loc, False), (lambda x: x, True)]:

# getting
result = idxr(s)[indexer]
Expand All @@ -332,14 +286,12 @@ def test_scalar_float(self):
# setting
s2 = s.copy()

with catch_warnings(record=True):
result = idxr(s2)[indexer]
result = idxr(s2)[indexer]
self.check(result, s, 3, getitem)

# random integer is a KeyError
with catch_warnings(record=True):
with pytest.raises(KeyError, match=r"^3\.5$"):
idxr(s)[3.5]
with pytest.raises(KeyError, match=r"^3\.5$"):
idxr(s)[3.5]

# contains
assert 3.0 in s
Expand All @@ -365,7 +317,6 @@ def test_scalar_float(self):
with pytest.raises(TypeError, match=msg):
s2.iloc[3.0] = 0

@ignore_ix
def test_slice_non_numeric(self):

# GH 4892
Expand Down Expand Up @@ -397,12 +348,7 @@ def test_slice_non_numeric(self):
with pytest.raises(TypeError, match=msg):
s.iloc[l]

for idxr in [
lambda x: x.ix,
lambda x: x.loc,
lambda x: x.iloc,
lambda x: x,
]:
for idxr in [lambda x: x.loc, lambda x: x.iloc, lambda x: x]:

msg = (
"cannot do slice indexing"
Expand All @@ -414,9 +360,8 @@ def test_slice_non_numeric(self):
kind_int=str(int),
)
)
with catch_warnings(record=True):
with pytest.raises(TypeError, match=msg):
idxr(s)[l]
with pytest.raises(TypeError, match=msg):
idxr(s)[l]

# setitem
for l in [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]:
Expand All @@ -429,12 +374,7 @@ def test_slice_non_numeric(self):
with pytest.raises(TypeError, match=msg):
s.iloc[l] = 0

for idxr in [
lambda x: x.ix,
lambda x: x.loc,
lambda x: x.iloc,
lambda x: x,
]:
for idxr in [lambda x: x.loc, lambda x: x.iloc, lambda x: x]:
msg = (
"cannot do slice indexing"
r" on {klass} with these indexers"
Expand All @@ -445,11 +385,9 @@ def test_slice_non_numeric(self):
kind_int=str(int),
)
)
with catch_warnings(record=True):
with pytest.raises(TypeError, match=msg):
idxr(s)[l] = 0
with pytest.raises(TypeError, match=msg):
idxr(s)[l] = 0

@ignore_ix
def test_slice_integer(self):

# same as above, but for Integer based indexes
Expand All @@ -468,10 +406,9 @@ def test_slice_integer(self):
# getitem
for l in [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]:

for idxr in [lambda x: x.loc, lambda x: x.ix]:
for idxr in [lambda x: x.loc]:

with catch_warnings(record=True):
result = idxr(s)[l]
result = idxr(s)[l]

# these are all label indexing
# except getitem which is positional
Expand All @@ -494,9 +431,8 @@ def test_slice_integer(self):
# getitem out-of-bounds
for l in [slice(-6, 6), slice(-6.0, 6.0)]:

for idxr in [lambda x: x.loc, lambda x: x.ix]:
with catch_warnings(record=True):
result = idxr(s)[l]
for idxr in [lambda x: x.loc]:
result = idxr(s)[l]

# these are all label indexing
# except getitem which is positional
Expand All @@ -523,10 +459,9 @@ def test_slice_integer(self):
(slice(2.5, 3.5), slice(3, 4)),
]:

for idxr in [lambda x: x.loc, lambda x: x.ix]:
for idxr in [lambda x: x.loc]:

with catch_warnings(record=True):
result = idxr(s)[l]
result = idxr(s)[l]
if oob:
res = slice(0, 0)
else:
Expand All @@ -546,11 +481,10 @@ def test_slice_integer(self):
# setitem
for l in [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]:

for idxr in [lambda x: x.loc, lambda x: x.ix]:
for idxr in [lambda x: x.loc]:
sc = s.copy()
with catch_warnings(record=True):
idxr(sc)[l] = 0
result = idxr(sc)[l].values.ravel()
idxr(sc)[l] = 0
result = idxr(sc)[l].values.ravel()
assert (result == 0).all()

# positional indexing
Expand Down Expand Up @@ -585,7 +519,6 @@ def test_integer_positional_indexing(self):
with pytest.raises(TypeError, match=msg):
idxr(s)[l]

@ignore_ix
def test_slice_integer_frame_getitem(self):

# similar to above, but on the getitem dim (of a DataFrame)
Expand Down Expand Up @@ -663,10 +596,7 @@ def f(idxr):
s[l] = 0

f(lambda x: x.loc)
with catch_warnings(record=True):
f(lambda x: x.ix)

@ignore_ix
def test_slice_float(self):

# same as above, but for floats
Expand All @@ -679,20 +609,18 @@ def test_slice_float(self):
for l in [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]:

expected = s.iloc[3:4]
for idxr in [lambda x: x.ix, lambda x: x.loc, lambda x: x]:
for idxr in [lambda x: x.loc, lambda x: x]:

# getitem
with catch_warnings(record=True):
result = idxr(s)[l]
result = idxr(s)[l]
if isinstance(s, Series):
tm.assert_series_equal(result, expected)
else:
tm.assert_frame_equal(result, expected)
# setitem
s2 = s.copy()
with catch_warnings(record=True):
idxr(s2)[l] = 0
result = idxr(s2)[l].values.ravel()
idxr(s2)[l] = 0
result = idxr(s2)[l].values.ravel()
assert (result == 0).all()

def test_floating_index_doc_example(self):
Expand Down