Skip to content

CLN: Remove some duplicated tests and parametrize #39960

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 4 commits into from
Feb 22, 2021
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
103 changes: 26 additions & 77 deletions pandas/tests/indexing/test_floats.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,122 +498,71 @@ def test_floating_index_doc_example(self):
assert s.loc[3] == 2
assert s.iloc[3] == 3

def test_floating_misc(self):
def test_floating_misc(self, indexer_sl):

# related 236
# scalar/slicing of a float index
s = Series(np.arange(5), index=np.arange(5) * 2.5, dtype=np.int64)

# label based slicing
result1 = s[1.0:3.0]
result2 = s.loc[1.0:3.0]
result3 = s.loc[1.0:3.0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not necessarily for this PR, but i think this test can be parametrized over indexer_sl

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good point. Was too focused on the duplicates :)

tm.assert_series_equal(result1, result2)
tm.assert_series_equal(result1, result3)
result = indexer_sl(s)[1.0:3.0]
expected = Series(1, index=[2.5])
tm.assert_series_equal(result, expected)

# exact indexing when found
result1 = s[5.0]
result2 = s.loc[5.0]
result3 = s.loc[5.0]
assert result1 == result2
assert result1 == result3

result1 = s[5]
result2 = s.loc[5]
result3 = s.loc[5]
assert result1 == result2
assert result1 == result3
result = indexer_sl(s)[5.0]
assert result == 2

assert s[5.0] == s[5]
result = indexer_sl(s)[5]
assert result == 2

# value not found (and no fallbacking at all)

# scalar integers
with pytest.raises(KeyError, match=r"^4$"):
s.loc[4]
with pytest.raises(KeyError, match=r"^4$"):
s.loc[4]
with pytest.raises(KeyError, match=r"^4$"):
s[4]
indexer_sl(s)[4]

# fancy floats/integers create the correct entry (as nan)
# fancy tests
expected = Series([2, 0], index=Float64Index([5.0, 0.0]))
for fancy_idx in [[5.0, 0.0], np.array([5.0, 0.0])]: # float
tm.assert_series_equal(s[fancy_idx], expected)
tm.assert_series_equal(s.loc[fancy_idx], expected)
tm.assert_series_equal(s.loc[fancy_idx], expected)
tm.assert_series_equal(indexer_sl(s)[fancy_idx], expected)

expected = Series([2, 0], index=Index([5, 0], dtype="int64"))
for fancy_idx in [[5, 0], np.array([5, 0])]: # int
tm.assert_series_equal(s[fancy_idx], expected)
tm.assert_series_equal(s.loc[fancy_idx], expected)
tm.assert_series_equal(s.loc[fancy_idx], expected)
tm.assert_series_equal(indexer_sl(s)[fancy_idx], expected)

# all should return the same as we are slicing 'the same'
result1 = s.loc[2:5]
result2 = s.loc[2.0:5.0]
result3 = s.loc[2.0:5]
result4 = s.loc[2.1:5]
tm.assert_series_equal(result1, result2)
tm.assert_series_equal(result1, result3)
tm.assert_series_equal(result1, result4)

# previously this did fallback indexing
result1 = s[2:5]
result2 = s[2.0:5.0]
result3 = s[2.0:5]
result4 = s[2.1:5]
result1 = indexer_sl(s)[2:5]
result2 = indexer_sl(s)[2.0:5.0]
result3 = indexer_sl(s)[2.0:5]
result4 = indexer_sl(s)[2.1:5]
tm.assert_series_equal(result1, result2)
tm.assert_series_equal(result1, result3)
tm.assert_series_equal(result1, result4)

result1 = s.loc[2:5]
result2 = s.loc[2.0:5.0]
result3 = s.loc[2.0:5]
result4 = s.loc[2.1:5]
tm.assert_series_equal(result1, result2)
tm.assert_series_equal(result1, result3)
tm.assert_series_equal(result1, result4)
expected = Series([1, 2], index=[2.5, 5.0])
result = indexer_sl(s)[2:5]

# combined test
result1 = s.loc[2:5]
result2 = s.loc[2:5]
result3 = s[2:5]

tm.assert_series_equal(result1, result2)
tm.assert_series_equal(result1, result3)
tm.assert_series_equal(result, expected)

# list selection
result1 = s[[0.0, 5, 10]]
result2 = s.loc[[0.0, 5, 10]]
result3 = s.loc[[0.0, 5, 10]]
result4 = s.iloc[[0, 2, 4]]
result1 = indexer_sl(s)[[0.0, 5, 10]]
result2 = s.iloc[[0, 2, 4]]
tm.assert_series_equal(result1, result2)
tm.assert_series_equal(result1, result3)
tm.assert_series_equal(result1, result4)

with pytest.raises(KeyError, match="with any missing labels"):
s[[1.6, 5, 10]]
with pytest.raises(KeyError, match="with any missing labels"):
s.loc[[1.6, 5, 10]]
indexer_sl(s)[[1.6, 5, 10]]

with pytest.raises(KeyError, match="with any missing labels"):
s[[0, 1, 2]]
with pytest.raises(KeyError, match="with any missing labels"):
s.loc[[0, 1, 2]]
indexer_sl(s)[[0, 1, 2]]

result1 = s.loc[[2.5, 5]]
result2 = s.loc[[2.5, 5]]
tm.assert_series_equal(result1, result2)
tm.assert_series_equal(result1, Series([1, 2], index=[2.5, 5.0]))
result = indexer_sl(s)[[2.5, 5]]
tm.assert_series_equal(result, Series([1, 2], index=[2.5, 5.0]))

result1 = s[[2.5]]
result2 = s.loc[[2.5]]
result3 = s.loc[[2.5]]
tm.assert_series_equal(result1, result2)
tm.assert_series_equal(result1, result3)
tm.assert_series_equal(result1, Series([1], index=[2.5]))
result = indexer_sl(s)[[2.5]]
tm.assert_series_equal(result, Series([1], index=[2.5]))

def test_floating_tuples(self):
# see gh-13509
Expand Down
25 changes: 4 additions & 21 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,11 @@


class TestiLoc(Base):
def test_iloc_getitem_int(self):
# integer
@pytest.mark.parametrize("key", [2, -1, [0, 1, 2]])
def test_iloc_getitem_int_and_list_int(self, key):
self.check_result(
"iloc",
2,
typs=["labels", "mixed", "ts", "floats", "empty"],
fails=IndexError,
)

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

def test_iloc_getitem_list_int(self):
self.check_result(
"iloc",
[0, 1, 2],
key,
typs=["labels", "mixed", "ts", "floats", "empty"],
fails=IndexError,
)
Expand Down Expand Up @@ -371,7 +354,7 @@ def test_iloc_getitem_bool_diff_len(self, index):
s = Series([1, 2, 3])
msg = f"Boolean index has wrong length: {len(index)} instead of {len(s)}"
with pytest.raises(IndexError, match=msg):
_ = s.iloc[index]
s.iloc[index]

def test_iloc_getitem_slice(self):
df = DataFrame(
Expand Down
14 changes: 3 additions & 11 deletions pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,11 @@ def test_dups_fancy_indexing_only_missing_label(self):

# ToDo: check_index_type can be True after GH 11497

def test_dups_fancy_indexing_missing_label(self):
@pytest.mark.parametrize("vals", [[0, 1, 2], list("abc")])
def test_dups_fancy_indexing_missing_label(self, vals):

# GH 4619; duplicate indexer with missing label
df = DataFrame({"A": [0, 1, 2]})
with pytest.raises(KeyError, match="with any missing labels"):
df.loc[[0, 8, 0]]

df = DataFrame({"A": list("abc")})
df = DataFrame({"A": vals})
with pytest.raises(KeyError, match="with any missing labels"):
df.loc[[0, 8, 0]]

Expand Down Expand Up @@ -455,9 +452,6 @@ def test_multi_assign(self):
df2.loc[mask, cols] = dft.loc[mask, cols]
tm.assert_frame_equal(df2, expected)

df2.loc[mask, cols] = dft.loc[mask, cols]
tm.assert_frame_equal(df2, expected)

# with an ndarray on rhs
# coerces to float64 because values has float64 dtype
# GH 14001
Expand All @@ -472,8 +466,6 @@ def test_multi_assign(self):
df2 = df.copy()
df2.loc[mask, cols] = dft.loc[mask, cols].values
tm.assert_frame_equal(df2, expected)
df2.loc[mask, cols] = dft.loc[mask, cols].values
tm.assert_frame_equal(df2, expected)

def test_multi_assign_broadcasting_rhs(self):
# broadcasting on the rhs is required
Expand Down
Loading