Skip to content

REF: parametrize indexing tests #31592

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 5 commits into from
Feb 5, 2020
Merged
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
46 changes: 24 additions & 22 deletions pandas/tests/indexing/test_floats.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,9 @@ def check(self, result, original, indexer, getitem):

tm.assert_almost_equal(result, expected)

def test_scalar_error(self):

# GH 4892
# float_indexers should raise exceptions
# on appropriate Index types & accessors
# this duplicates the code below
# but is specifically testing for the error
# message

for index in [
@pytest.mark.parametrize(
"index_func",
[
tm.makeStringIndex,
tm.makeUnicodeIndex,
tm.makeCategoricalIndex,
Expand All @@ -40,22 +33,31 @@ def test_scalar_error(self):
tm.makePeriodIndex,
tm.makeIntIndex,
tm.makeRangeIndex,
]:
],
)
def test_scalar_error(self, index_func):

i = index(5)
# GH 4892
# float_indexers should raise exceptions
# on appropriate Index types & accessors
# this duplicates the code below
# but is specifically testing for the error
# message

s = Series(np.arange(len(i)), index=i)
i = index_func(5)

msg = "Cannot index by location index"
with pytest.raises(TypeError, match=msg):
s.iloc[3.0]
s = Series(np.arange(len(i)), index=i)

msg = (
"cannot do positional indexing on {klass} with these "
r"indexers \[3\.0\] of {kind}".format(klass=type(i), kind=str(float))
)
with pytest.raises(TypeError, match=msg):
s.iloc[3.0] = 0
msg = "Cannot index by location index"
with pytest.raises(TypeError, match=msg):
s.iloc[3.0]

msg = (
"cannot do positional indexing on {klass} with these "
r"indexers \[3\.0\] of {kind}".format(klass=type(i), kind=str(float))
)
with pytest.raises(TypeError, match=msg):
s.iloc[3.0] = 0

def test_scalar_non_numeric(self):

Expand Down
77 changes: 41 additions & 36 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,44 @@


class TestiLoc(Base):
def test_iloc_getitem_int(self):
# integer
self.check_result(
"iloc",
2,
"iloc",
2,
typs=["labels", "mixed", "ts", "floats", "empty"],
fails=IndexError,
)

def test_iloc_getitem_neg_int(self):
# neg integer
self.check_result(
"iloc",
-1,
"iloc",
-1,
typs=["labels", "mixed", "ts", "floats", "empty"],
fails=IndexError,
)

def test_iloc_getitem_list_int(self):
self.check_result(
"iloc",
[0, 1, 2],
"iloc",
[0, 1, 2],
typs=["labels", "mixed", "ts", "floats", "empty"],
fails=IndexError,
)

# array of ints (GH5006), make sure that a single indexer is returning
# the correct type


class TestiLoc2:
# TODO: better name, just separating out things that dont rely on base class
def test_iloc_exceeds_bounds(self):

# GH6296
Expand Down Expand Up @@ -135,28 +173,6 @@ def test_iloc_non_integer_raises(self, index, columns, index_vals, column_vals):
with pytest.raises(IndexError, match=msg):
df.iloc[index_vals, column_vals]

def test_iloc_getitem_int(self):
# integer
self.check_result(
"iloc",
2,
"iloc",
2,
typs=["labels", "mixed", "ts", "floats", "empty"],
fails=IndexError,
)

def test_iloc_getitem_neg_int(self):
# neg integer
self.check_result(
"iloc",
-1,
"iloc",
-1,
typs=["labels", "mixed", "ts", "floats", "empty"],
fails=IndexError,
)

@pytest.mark.parametrize("dims", [1, 2])
def test_iloc_getitem_invalid_scalar(self, dims):
# GH 21982
Expand All @@ -183,19 +199,6 @@ def test_iloc_array_not_mutating_negative_indices(self):
df.iloc[:, array_with_neg_numbers]
tm.assert_numpy_array_equal(array_with_neg_numbers, array_copy)

def test_iloc_getitem_list_int(self):
self.check_result(
"iloc",
[0, 1, 2],
"iloc",
[0, 1, 2],
typs=["labels", "mixed", "ts", "floats", "empty"],
fails=IndexError,
)

# array of ints (GH5006), make sure that a single indexer is returning
# the correct type

def test_iloc_getitem_neg_int_can_reach_first_index(self):
# GH10547 and GH10779
# negative integers should be able to reach index 0
Expand Down Expand Up @@ -286,7 +289,9 @@ def test_iloc_getitem_slice_dups(self):
tm.assert_frame_equal(df.iloc[10:, 2:], df1)

def test_iloc_setitem(self):
df = self.frame_ints
df = DataFrame(
np.random.randn(4, 4), index=np.arange(0, 8, 2), columns=np.arange(0, 12, 3)
)

df.iloc[1, 1] = 1
result = df.iloc[1, 1]
Expand Down
131 changes: 69 additions & 62 deletions pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
from pandas.core.generic import NDFrame
from pandas.core.indexers import validate_indices
from pandas.core.indexing import _maybe_numeric_slice, _non_reducing_slice
from pandas.tests.indexing.common import Base, _mklbl
from pandas.tests.indexing.common import _mklbl

# ------------------------------------------------------------------------
# Indexing test cases


class TestFancy(Base):
class TestFancy:
""" pure get/set item & fancy indexing """

def test_setitem_ndarray_1d(self):
Expand Down Expand Up @@ -750,7 +750,7 @@ def test_index_type_coercion(self):
assert s2.index.is_object()


class TestMisc(Base):
class TestMisc:
def test_float_index_to_mixed(self):
df = DataFrame({0.0: np.random.rand(10), 1.0: np.random.rand(10)})
df["a"] = 10
Expand Down Expand Up @@ -875,21 +875,21 @@ def test_indexing_dtypes_on_empty(self):
assert df2.loc[:, "a"].dtype == np.int64
tm.assert_series_equal(df2.loc[:, "a"], df2.iloc[:, 0])

def test_range_in_series_indexing(self):
@pytest.mark.parametrize("size", [5, 999999, 1000000])
def test_range_in_series_indexing(self, size):
# range can cause an indexing error
# GH 11652
for x in [5, 999999, 1000000]:
s = Series(index=range(x), dtype=np.float64)
s.loc[range(1)] = 42
tm.assert_series_equal(s.loc[range(1)], Series(42.0, index=[0]))
s = Series(index=range(size), dtype=np.float64)
s.loc[range(1)] = 42
tm.assert_series_equal(s.loc[range(1)], Series(42.0, index=[0]))

s.loc[range(2)] = 43
tm.assert_series_equal(s.loc[range(2)], Series(43.0, index=[0, 1]))
s.loc[range(2)] = 43
tm.assert_series_equal(s.loc[range(2)], Series(43.0, index=[0, 1]))

def test_non_reducing_slice(self):
df = DataFrame([[0, 1], [2, 3]])

slices = [
@pytest.mark.parametrize(
"slc",
[
# FIXME: dont leave commented-out
# pd.IndexSlice[:, :],
pd.IndexSlice[:, 1],
pd.IndexSlice[1, :],
Expand All @@ -902,10 +902,13 @@ def test_non_reducing_slice(self):
[0, 1],
np.array([0, 1]),
Series([0, 1]),
]
for slice_ in slices:
tslice_ = _non_reducing_slice(slice_)
assert isinstance(df.loc[tslice_], DataFrame)
],
)
def test_non_reducing_slice(self, slc):
df = DataFrame([[0, 1], [2, 3]])

tslice_ = _non_reducing_slice(slc)
assert isinstance(df.loc[tslice_], DataFrame)

def test_list_slice(self):
# like dataframe getitem
Expand Down Expand Up @@ -965,37 +968,37 @@ class TestSeriesNoneCoercion:
(["foo", "bar", "baz"], [None, "bar", "baz"]),
]

def test_coercion_with_setitem(self):
for start_data, expected_result in self.EXPECTED_RESULTS:
start_series = Series(start_data)
start_series[0] = None
@pytest.mark.parametrize("start_data,expected_result", EXPECTED_RESULTS)
def test_coercion_with_setitem(self, start_data, expected_result):
start_series = Series(start_data)
start_series[0] = None

expected_series = Series(expected_result)
tm.assert_series_equal(start_series, expected_series)
expected_series = Series(expected_result)
tm.assert_series_equal(start_series, expected_series)

def test_coercion_with_loc_setitem(self):
for start_data, expected_result in self.EXPECTED_RESULTS:
start_series = Series(start_data)
start_series.loc[0] = None
@pytest.mark.parametrize("start_data,expected_result", EXPECTED_RESULTS)
def test_coercion_with_loc_setitem(self, start_data, expected_result):
start_series = Series(start_data)
start_series.loc[0] = None

expected_series = Series(expected_result)
tm.assert_series_equal(start_series, expected_series)
expected_series = Series(expected_result)
tm.assert_series_equal(start_series, expected_series)

def test_coercion_with_setitem_and_series(self):
for start_data, expected_result in self.EXPECTED_RESULTS:
start_series = Series(start_data)
start_series[start_series == start_series[0]] = None
@pytest.mark.parametrize("start_data,expected_result", EXPECTED_RESULTS)
def test_coercion_with_setitem_and_series(self, start_data, expected_result):
start_series = Series(start_data)
start_series[start_series == start_series[0]] = None

expected_series = Series(expected_result)
tm.assert_series_equal(start_series, expected_series)
expected_series = Series(expected_result)
tm.assert_series_equal(start_series, expected_series)

def test_coercion_with_loc_and_series(self):
for start_data, expected_result in self.EXPECTED_RESULTS:
start_series = Series(start_data)
start_series.loc[start_series == start_series[0]] = None
@pytest.mark.parametrize("start_data,expected_result", EXPECTED_RESULTS)
def test_coercion_with_loc_and_series(self, start_data, expected_result):
start_series = Series(start_data)
start_series.loc[start_series == start_series[0]] = None

expected_series = Series(expected_result)
tm.assert_series_equal(start_series, expected_series)
expected_series = Series(expected_result)
tm.assert_series_equal(start_series, expected_series)


class TestDataframeNoneCoercion:
Expand All @@ -1012,31 +1015,35 @@ class TestDataframeNoneCoercion:
(["foo", "bar", "baz"], [None, "bar", "baz"]),
]

def test_coercion_with_loc(self):
for start_data, expected_result in self.EXPECTED_SINGLE_ROW_RESULTS:
start_dataframe = DataFrame({"foo": start_data})
start_dataframe.loc[0, ["foo"]] = None
@pytest.mark.parametrize("expected", EXPECTED_SINGLE_ROW_RESULTS)
def test_coercion_with_loc(self, expected):
start_data, expected_result = expected

start_dataframe = DataFrame({"foo": start_data})
start_dataframe.loc[0, ["foo"]] = None

expected_dataframe = DataFrame({"foo": expected_result})
tm.assert_frame_equal(start_dataframe, expected_dataframe)

@pytest.mark.parametrize("expected", EXPECTED_SINGLE_ROW_RESULTS)
def test_coercion_with_setitem_and_dataframe(self, expected):
start_data, expected_result = expected

expected_dataframe = DataFrame({"foo": expected_result})
tm.assert_frame_equal(start_dataframe, expected_dataframe)
start_dataframe = DataFrame({"foo": start_data})
start_dataframe[start_dataframe["foo"] == start_dataframe["foo"][0]] = None

def test_coercion_with_setitem_and_dataframe(self):
for start_data, expected_result in self.EXPECTED_SINGLE_ROW_RESULTS:
start_dataframe = DataFrame({"foo": start_data})
start_dataframe[start_dataframe["foo"] == start_dataframe["foo"][0]] = None
expected_dataframe = DataFrame({"foo": expected_result})
tm.assert_frame_equal(start_dataframe, expected_dataframe)

expected_dataframe = DataFrame({"foo": expected_result})
tm.assert_frame_equal(start_dataframe, expected_dataframe)
@pytest.mark.parametrize("expected", EXPECTED_SINGLE_ROW_RESULTS)
def test_none_coercion_loc_and_dataframe(self, expected):
start_data, expected_result = expected

def test_none_coercion_loc_and_dataframe(self):
for start_data, expected_result in self.EXPECTED_SINGLE_ROW_RESULTS:
start_dataframe = DataFrame({"foo": start_data})
start_dataframe.loc[
start_dataframe["foo"] == start_dataframe["foo"][0]
] = None
start_dataframe = DataFrame({"foo": start_data})
start_dataframe.loc[start_dataframe["foo"] == start_dataframe["foo"][0]] = None

expected_dataframe = DataFrame({"foo": expected_result})
tm.assert_frame_equal(start_dataframe, expected_dataframe)
expected_dataframe = DataFrame({"foo": expected_result})
tm.assert_frame_equal(start_dataframe, expected_dataframe)

def test_none_coercion_mixed_dtypes(self):
start_dataframe = DataFrame(
Expand Down
Loading