Skip to content

CLN: Refactor tests and delete duplicates #40181

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
Mar 4, 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
8 changes: 8 additions & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,14 @@ def indexer_sl(request):
return request.param


@pytest.fixture(params=[tm.at, tm.loc])
def indexer_al(request):
"""
Parametrize over at.__setitem__, loc.__setitem__
"""
return request.param


@pytest.fixture
def using_array_manager(request):
"""
Expand Down
10 changes: 0 additions & 10 deletions pandas/tests/indexing/multiindex/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,12 @@ def test_loc_multiindex_incomplete(self):

result = s.loc[0:4, "a":"c"]
tm.assert_series_equal(result, expected)
tm.assert_series_equal(result, expected)

result = s.loc[:4, "a":"c"]
tm.assert_series_equal(result, expected)
tm.assert_series_equal(result, expected)

result = s.loc[0:, "a":"c"]
tm.assert_series_equal(result, expected)
tm.assert_series_equal(result, expected)

# GH 7400
# multiindexer gettitem with list of indexers skips wrong element
Expand Down Expand Up @@ -412,13 +409,6 @@ def test_loc_getitem_duplicates_multiindex_missing_indexers(indexer, pos):
tm.assert_series_equal(result, expected)


def test_series_loc_getitem_fancy(multiindex_year_month_day_dataframe_random_data):
s = multiindex_year_month_day_dataframe_random_data["A"]
expected = s.reindex(s.index[49:51])
result = s.loc[[(2000, 3, 10), (2000, 3, 13)]]
tm.assert_series_equal(result, expected)


@pytest.mark.parametrize("columns_indexer", [([], slice(None)), (["foo"], [])])
def test_loc_getitem_duplicates_multiindex_empty_indexer(columns_indexer):
# GH 8737
Expand Down
4 changes: 0 additions & 4 deletions pandas/tests/indexing/multiindex/test_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,6 @@ def test_per_axis_per_level_getitem(self):
# This used to treat [1] as positional GH#16396
df.loc[slice(None), [1]]

result = df.loc[(slice(None), [1]), :]
expected = df.iloc[[0, 3]]
tm.assert_frame_equal(result, expected)

# not lexsorted
assert df.index._lexsort_depth == 2
df = df.sort_index(level=1, axis=0)
Expand Down
46 changes: 13 additions & 33 deletions pandas/tests/indexing/test_at.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,71 +88,51 @@ def test_at_with_duplicate_axes_requires_scalar_lookup(self):

class TestAtErrors:
# TODO: De-duplicate/parametrize
# test_at_series_raises_key_error, test_at_frame_raises_key_error,
# test_at_series_raises_key_error2, test_at_frame_raises_key_error2

def test_at_series_raises_key_error(self):
def test_at_series_raises_key_error(self, indexer_al):
# GH#31724 .at should match .loc

ser = Series([1, 2, 3], index=[3, 2, 1])
result = ser.at[1]
assert result == 3
result = ser.loc[1]
result = indexer_al(ser)[1]
assert result == 3

with pytest.raises(KeyError, match="a"):
ser.at["a"]
with pytest.raises(KeyError, match="a"):
# .at should match .loc
ser.loc["a"]
indexer_al(ser)["a"]

def test_at_frame_raises_key_error(self):
def test_at_frame_raises_key_error(self, indexer_al):
# GH#31724 .at should match .loc

df = DataFrame({0: [1, 2, 3]}, index=[3, 2, 1])

result = df.at[1, 0]
assert result == 3
result = df.loc[1, 0]
result = indexer_al(df)[1, 0]
assert result == 3

with pytest.raises(KeyError, match="a"):
df.at["a", 0]
with pytest.raises(KeyError, match="a"):
df.loc["a", 0]
indexer_al(df)["a", 0]

with pytest.raises(KeyError, match="a"):
df.at[1, "a"]
with pytest.raises(KeyError, match="a"):
df.loc[1, "a"]
indexer_al(df)[1, "a"]

def test_at_series_raises_key_error2(self):
def test_at_series_raises_key_error2(self, indexer_al):
# at should not fallback
# GH#7814
# GH#31724 .at should match .loc
ser = Series([1, 2, 3], index=list("abc"))
result = ser.at["a"]
assert result == 1
result = ser.loc["a"]
result = indexer_al(ser)["a"]
assert result == 1

with pytest.raises(KeyError, match="^0$"):
ser.at[0]
with pytest.raises(KeyError, match="^0$"):
ser.loc[0]
indexer_al(ser)[0]

def test_at_frame_raises_key_error2(self):
def test_at_frame_raises_key_error2(self, indexer_al):
# GH#31724 .at should match .loc
df = DataFrame({"A": [1, 2, 3]}, index=list("abc"))
result = df.at["a", "A"]
assert result == 1
result = df.loc["a", "A"]
result = indexer_al(df)["a", "A"]
assert result == 1

with pytest.raises(KeyError, match="^0$"):
df.at["a", 0]
with pytest.raises(KeyError, match="^0$"):
df.loc["a", 0]
indexer_al(df)["a", 0]

def test_at_getitem_mixed_index_no_fallback(self):
# GH#19860
Expand Down
32 changes: 5 additions & 27 deletions pandas/tests/indexing/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_consistency_with_tz_aware_scalar(self):
result = df[0].at[0]
assert result == expected

def test_indexing_with_datetimeindex_tz(self):
def test_indexing_with_datetimeindex_tz(self, indexer_sl):

# GH 12050
# indexing on a series with a datetimeindex with tz
Expand All @@ -93,48 +93,26 @@ def test_indexing_with_datetimeindex_tz(self):

for sel in (index, list(index)):
# getitem
result = ser[sel]
result = indexer_sl(ser)[sel]
expected = ser.copy()
if sel is not index:
expected.index = expected.index._with_freq(None)
tm.assert_series_equal(result, expected)

# setitem
result = ser.copy()
result[sel] = 1
expected = Series(1, index=index)
tm.assert_series_equal(result, expected)

# .loc getitem
result = ser.loc[sel]
expected = ser.copy()
if sel is not index:
expected.index = expected.index._with_freq(None)
tm.assert_series_equal(result, expected)

# .loc setitem
result = ser.copy()
result.loc[sel] = 1
indexer_sl(result)[sel] = 1
expected = Series(1, index=index)
tm.assert_series_equal(result, expected)

# single element indexing

# getitem
assert ser[index[1]] == 1
assert indexer_sl(ser)[index[1]] == 1

# setitem
result = ser.copy()
result[index[1]] = 5
expected = Series([0, 5], index=index)
tm.assert_series_equal(result, expected)

# .loc getitem
assert ser.loc[index[1]] == 1

# .loc setitem
result = ser.copy()
result.loc[index[1]] = 5
indexer_sl(result)[index[1]] = 5
expected = Series([0, 5], index=index)
tm.assert_series_equal(result, expected)

Expand Down
27 changes: 5 additions & 22 deletions pandas/tests/indexing/test_na_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,30 +63,13 @@ def test_series_mask_boolean(values, dtype, mask, indexer_class, frame):
tm.assert_equal(result, expected)


@pytest.mark.parametrize("frame", [True, False])
def test_na_treated_as_false(frame):
def test_na_treated_as_false(frame_or_series, indexer_sli):
# https://github.com/pandas-dev/pandas/issues/31503
s = pd.Series([1, 2, 3], name="name")

if frame:
s = s.to_frame()
obj = frame_or_series([1, 2, 3])

mask = pd.array([True, False, None], dtype="boolean")

result = s[mask]
expected = s[mask.fillna(False)]

result_loc = s.loc[mask]
expected_loc = s.loc[mask.fillna(False)]
result = indexer_sli(obj)[mask]
expected = indexer_sli(obj)[mask.fillna(False)]

result_iloc = s.iloc[mask]
expected_iloc = s.iloc[mask.fillna(False)]

if frame:
tm.assert_frame_equal(result, expected)
tm.assert_frame_equal(result_loc, expected_loc)
tm.assert_frame_equal(result_iloc, expected_iloc)
else:
tm.assert_series_equal(result, expected)
tm.assert_series_equal(result_loc, expected_loc)
tm.assert_series_equal(result_iloc, expected_iloc)
tm.assert_equal(result, expected)