Skip to content

TST: tighten assert_index_equal calls #38054

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
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 5 additions & 1 deletion pandas/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,11 @@ def assert_categorical_equal(
_check_isinstance(left, right, Categorical)

if check_category_order:
assert_index_equal(left.categories, right.categories, obj=f"{obj}.categories")
# TODO: might be able to get exact=True if Categorical(RangeIndex)
# preserved RangeIndex categories
assert_index_equal(
left.categories, right.categories, obj=f"{obj}.categories", exact="equiv"
)
assert_numpy_array_equal(
left.codes, right.codes, check_dtype=check_dtype, obj=f"{obj}.codes"
)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arithmetic/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,7 @@ def check_binop(self, ops, scalars, idxs):
for scalar in scalars:
result = op(idx, scalar)
expected = op(Int64Index(idx), scalar)
tm.assert_index_equal(result, expected)
tm.assert_index_equal(result, expected, exact="equiv")

def test_binops(self):
ops = [
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/methods/test_reset_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def test_reset_index(self, float_frame):
float_frame.index.name = "index"
deleveled = float_frame.reset_index()
tm.assert_series_equal(deleveled["index"], Series(float_frame.index))
tm.assert_index_equal(deleveled.index, Index(np.arange(len(deleveled))))
tm.assert_index_equal(deleveled.index, Index(range(len(deleveled))), exact=True)

# preserve column names
float_frame.columns.name = "columns"
Expand Down
10 changes: 5 additions & 5 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ def test_constructor_dict(self):

# with dict of empty list and Series
frame = DataFrame({"A": [], "B": []}, columns=["A", "B"])
tm.assert_index_equal(frame.index, Index([], dtype=np.int64))
tm.assert_index_equal(frame.index, RangeIndex(0), exact=True)

# GH 14381
# Dict with None value
Expand Down Expand Up @@ -802,14 +802,14 @@ def _check_basic_constructor(self, empty):

# automatic labeling
frame = DataFrame(mat)
tm.assert_index_equal(frame.index, pd.Int64Index(range(2)))
tm.assert_index_equal(frame.columns, pd.Int64Index(range(3)))
tm.assert_index_equal(frame.index, Index(range(2)), exact=True)
tm.assert_index_equal(frame.columns, Index(range(3)), exact=True)

frame = DataFrame(mat, index=[1, 2])
tm.assert_index_equal(frame.columns, pd.Int64Index(range(3)))
tm.assert_index_equal(frame.columns, Index(range(3)), exact=True)

frame = DataFrame(mat, columns=["A", "B", "C"])
tm.assert_index_equal(frame.index, pd.Int64Index(range(2)))
tm.assert_index_equal(frame.index, Index(range(2)), exact=True)

# 0-length axis
frame = DataFrame(empty((0, 3)))
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/groupby/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ def test_apply_frame_not_as_index_column_name(df):
result = grouped.apply(len)
expected = grouped.count().rename(columns={"C": np.nan}).drop(columns="D")
# TODO: Use assert_frame_equal when column name is not np.nan (GH 36306)
tm.assert_index_equal(result.index, expected.index)
tm.assert_index_equal(result.index, expected.index, exact="equiv")
tm.assert_numpy_array_equal(result.values, expected.values)


Expand Down
7 changes: 5 additions & 2 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ def test_equals_op(self):
# assuming the 2nd to last item is unique in the data
item = index_a[-2]
tm.assert_numpy_array_equal(index_a == item, expected3)
# For RangeIndex we can convert to Int64Index
tm.assert_series_equal(series_a == item, Series(expected3))

def test_format(self):
Expand Down Expand Up @@ -656,7 +657,8 @@ def test_map(self):
expected = index

result = index.map(lambda x: x)
tm.assert_index_equal(result, expected)
# For RangeIndex we convert to Int64Index
tm.assert_index_equal(result, expected, exact="equiv")

@pytest.mark.parametrize(
"mapper",
Expand All @@ -680,7 +682,8 @@ def test_map_dictlike(self, mapper):
expected = index

result = index.map(identity)
tm.assert_index_equal(result, expected)
# For RangeIndex we convert to Int64Index
tm.assert_index_equal(result, expected, exact="equiv")

# empty mappable
expected = Index([np.nan] * len(index))
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/ranges/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_constructor(self, args, kwargs, start, stop, step, name):
assert isinstance(result, RangeIndex)
assert result.name is name
assert result._range == range(start, stop, step)
tm.assert_index_equal(result, expected)
tm.assert_index_equal(result, expected, exact="equiv")

def test_constructor_invalid_args(self):
msg = "RangeIndex\\(\\.\\.\\.\\) must be called with integers"
Expand Down Expand Up @@ -146,7 +146,7 @@ def test_constructor_corner(self):
arr = np.array([1, 2, 3, 4], dtype=object)
index = RangeIndex(1, 5)
assert index.values.dtype == np.int64
tm.assert_index_equal(index, Index(arr))
tm.assert_index_equal(index, Index(arr), exact="equiv")

# non-int raise Exception
with pytest.raises(TypeError, match=r"Wrong type \<class 'str'\>"):
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/indexes/ranges/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_join_outer(self):

assert isinstance(res, Int64Index)
assert not isinstance(res, RangeIndex)
tm.assert_index_equal(res, eres)
tm.assert_index_equal(res, eres, exact="equiv")
tm.assert_numpy_array_equal(lidx, elidx)
tm.assert_numpy_array_equal(ridx, eridx)

Expand All @@ -41,7 +41,7 @@ def test_join_outer(self):

assert isinstance(res, Int64Index)
assert not isinstance(res, RangeIndex)
tm.assert_index_equal(res, eres)
tm.assert_index_equal(res, eres, exact="equiv")
tm.assert_numpy_array_equal(lidx, elidx)
tm.assert_numpy_array_equal(ridx, eridx)

Expand All @@ -63,7 +63,7 @@ def test_join_inner(self):
eridx = np.array([9, 7], dtype=np.intp)

assert isinstance(res, Int64Index)
tm.assert_index_equal(res, eres)
tm.assert_index_equal(res, eres, exact="equiv")
tm.assert_numpy_array_equal(lidx, elidx)
tm.assert_numpy_array_equal(ridx, eridx)

Expand All @@ -73,7 +73,7 @@ def test_join_inner(self):
res, lidx, ridx = index.join(other, how="inner", return_indexers=True)

assert isinstance(res, RangeIndex)
tm.assert_index_equal(res, eres)
tm.assert_index_equal(res, eres, exact="equiv")
tm.assert_numpy_array_equal(lidx, elidx)
tm.assert_numpy_array_equal(ridx, eridx)

Expand Down
16 changes: 8 additions & 8 deletions pandas/tests/indexes/ranges/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,38 +419,38 @@ def test_slice_specialised(self):
# positive slice values
index_slice = index[7:10:2]
expected = Index(np.array([14, 18]), name="foo")
tm.assert_index_equal(index_slice, expected)
tm.assert_index_equal(index_slice, expected, exact="equiv")

# negative slice values
index_slice = index[-1:-5:-2]
expected = Index(np.array([18, 14]), name="foo")
tm.assert_index_equal(index_slice, expected)
tm.assert_index_equal(index_slice, expected, exact="equiv")

# stop overshoot
index_slice = index[2:100:4]
expected = Index(np.array([4, 12]), name="foo")
tm.assert_index_equal(index_slice, expected)
tm.assert_index_equal(index_slice, expected, exact="equiv")

# reverse
index_slice = index[::-1]
expected = Index(index.values[::-1], name="foo")
tm.assert_index_equal(index_slice, expected)
tm.assert_index_equal(index_slice, expected, exact="equiv")

index_slice = index[-8::-1]
expected = Index(np.array([4, 2, 0]), name="foo")
tm.assert_index_equal(index_slice, expected)
tm.assert_index_equal(index_slice, expected, exact="equiv")

index_slice = index[-40::-1]
expected = Index(np.array([], dtype=np.int64), name="foo")
tm.assert_index_equal(index_slice, expected)
tm.assert_index_equal(index_slice, expected, exact="equiv")

index_slice = index[40::-1]
expected = Index(index.values[40::-1], name="foo")
tm.assert_index_equal(index_slice, expected)
tm.assert_index_equal(index_slice, expected, exact="equiv")

index_slice = index[10::-1]
expected = Index(index.values[::-1], name="foo")
tm.assert_index_equal(index_slice, expected)
tm.assert_index_equal(index_slice, expected, exact="equiv")

@pytest.mark.parametrize("step", set(range(-5, 6)) - {0})
def test_len_specialised(self, step):
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/indexes/ranges/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,28 @@ def test_intersection(self, sort):
other = RangeIndex(1, 6)
result = index.intersection(other, sort=sort)
expected = Index(np.sort(np.intersect1d(index.values, other.values)))
tm.assert_index_equal(result, expected)
tm.assert_index_equal(result, expected, exact="equiv")

# intersect with decreasing RangeIndex
other = RangeIndex(5, 0, -1)
result = index.intersection(other, sort=sort)
expected = Index(np.sort(np.intersect1d(index.values, other.values)))
tm.assert_index_equal(result, expected)
tm.assert_index_equal(result, expected, exact="equiv")

# reversed (GH 17296)
result = other.intersection(index, sort=sort)
tm.assert_index_equal(result, expected)
tm.assert_index_equal(result, expected, exact="equiv")

# GH 17296: intersect two decreasing RangeIndexes
first = RangeIndex(10, -2, -2)
other = RangeIndex(5, -4, -1)
expected = first.astype(int).intersection(other.astype(int), sort=sort)
result = first.intersection(other, sort=sort).astype(int)
tm.assert_index_equal(result, expected)
tm.assert_index_equal(result, expected, exact="equiv")

# reversed
result = other.intersection(first, sort=sort).astype(int)
tm.assert_index_equal(result, expected)
tm.assert_index_equal(result, expected, exact="equiv")

index = RangeIndex(5)

Expand Down Expand Up @@ -238,7 +238,7 @@ def test_union_sorted(self, unions):
res2 = idx2.union(idx1, sort=None)
res3 = idx1._int64index.union(idx2, sort=None)
tm.assert_index_equal(res2, expected_sorted, exact=True)
tm.assert_index_equal(res3, expected_sorted)
tm.assert_index_equal(res3, expected_sorted, exact="equiv")

def test_difference(self):
# GH#12034 Cases where we operate against another RangeIndex and may
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,8 @@ def test_union_dt_as_obj(self, sort):

def test_map_identity_mapping(self, index):
# GH 12766
tm.assert_index_equal(index, index.map(lambda x: x))
# exact="equiv" bc RangeIndex converts to Int64Index
tm.assert_index_equal(index, index.map(lambda x: x), exact="equiv")

def test_map_with_tuples(self):
# GH 12766
Expand Down Expand Up @@ -772,7 +773,7 @@ def test_map_tseries_indices_return_index(self, attr):
def test_map_tseries_indices_accsr_return_index(self):
date_index = tm.makeDateIndex(24, freq="h", name="hourly")
expected = Index(range(24), name="hourly")
tm.assert_index_equal(expected, date_index.map(lambda x: x.hour))
tm.assert_index_equal(expected, date_index.map(lambda x: x.hour), exact="equiv")

@pytest.mark.parametrize(
"mapper",
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ def test_unique(self, index):
expected = index.drop_duplicates()
for level in 0, index.name, None:
result = index.unique(level=level)
tm.assert_index_equal(result, expected)
# TODO: make exact=True by making RangeIndex.unique return RangeIndex
tm.assert_index_equal(result, expected, exact="equiv")

msg = "Too many levels: Index has only 1 level, not 4"
with pytest.raises(IndexError, match=msg):
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/indexes/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,8 @@ def test_difference_preserves_type_empty(self, index, sort):
if not index.is_unique:
return
result = index.difference(index, sort=sort)
expected = index.drop(index)
tm.assert_index_equal(result, expected)
expected = index[:0]
tm.assert_index_equal(result, expected, exact=True)

def test_intersection_difference_match_empty(self, index, sort):
# GH#20040
Expand All @@ -395,6 +395,6 @@ def test_intersection_difference_match_empty(self, index, sort):
# of an index with itself. Test for all types
if not index.is_unique:
return
inter = index.intersection(index.drop(index))
inter = index.intersection(index[:0])
diff = index.difference(index, sort=sort)
tm.assert_index_equal(inter, diff)
tm.assert_index_equal(inter, diff, exact=True)
4 changes: 2 additions & 2 deletions pandas/tests/reshape/concat/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ def test_concat_series_axis1_same_names_ignore_index(self):
s2 = Series(np.random.randn(len(dates)), index=dates, name="value")

result = concat([s1, s2], axis=1, ignore_index=True)
expected = Index([0, 1])
expected = Index(range(2))

tm.assert_index_equal(result.columns, expected)
tm.assert_index_equal(result.columns, expected, exact=True)

@pytest.mark.parametrize(
"s1name,s2name", [(np.int64(190), (43, 0)), (190, (43, 0))]
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/reshape/merge/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ def test_join_sort(self):

# smoke test
joined = left.join(right, on="key", sort=False)
tm.assert_index_equal(joined.index, Index(list(range(4))))
tm.assert_index_equal(joined.index, Index(range(4)), exact=True)

def test_join_mixed_non_unique_index(self):
# GH 12814, unorderable types in py3 with a non-unique index
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ def test_series_ctor_plus_datetimeindex(self):

def test_constructor_default_index(self):
s = Series([0, 1, 2])
tm.assert_index_equal(s.index, Index(np.arange(3)))
tm.assert_index_equal(s.index, Index(range(3)), exact=True)

@pytest.mark.parametrize(
"input",
Expand Down
10 changes: 8 additions & 2 deletions pandas/tests/window/test_pairwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ def test_pairwise_with_self(self, pairwise_frames, pairwise_target_frame, f):
# in a non-monotonic way, so compare accordingly
result = f(pairwise_frames)
tm.assert_index_equal(
result.index.levels[0], pairwise_frames.index, check_names=False
result.index.levels[0],
pairwise_frames.index,
check_names=False,
exact="equiv",
)
tm.assert_numpy_array_equal(
safe_sort(result.index.levels[1]),
Expand Down Expand Up @@ -103,7 +106,10 @@ def test_pairwise_with_other(
# DataFrame with another DataFrame, pairwise=True
result = f(pairwise_frames, pairwise_other_frame)
tm.assert_index_equal(
result.index.levels[0], pairwise_frames.index, check_names=False
result.index.levels[0],
pairwise_frames.index,
check_names=False,
exact="equiv",
)
tm.assert_numpy_array_equal(
safe_sort(result.index.levels[1]),
Expand Down