Skip to content

Commit f732749

Browse files
authored
TST: Clean tests that constuct Index equivalent to RangeIndexes (#57441)
* API: Check index and column classess exactly by default * Add a todo * Change test for expected behavior * add ignore index check * ignore column checking for some test * Ignore index checking for test_concat_all_na_block * Ignore adjust some tests * Fix another test * Adjust more tests * Fix more tests * Adjust more tests * adjust another test * Adjust more tests * Adjust test * Adjust test * Adjust test * Fix more tests * Fix more tests * Fix more tests * Fix tests * Adjust more tests * Adjust more tests * Fix some tests * Adjust tests * Fix test * Fix more test * Adjust more tests * Undo some strictness checking * update tests * Adjust more tests * Another test * Adjust more tests * fix another test * Fix test * Fix another test * fix more test * More indexes * Undo assert_ functions for strict checking * Fix tests
1 parent 1afc7a3 commit f732749

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+235
-189
lines changed

pandas/tests/apply/test_frame_apply.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -368,18 +368,18 @@ def test_apply_mixed_dtype_corner():
368368
result = df[:0].apply(np.mean, axis=1)
369369
# the result here is actually kind of ambiguous, should it be a Series
370370
# or a DataFrame?
371-
expected = Series(np.nan, index=pd.Index([], dtype="int64"))
371+
expected = Series(dtype=np.float64)
372372
tm.assert_series_equal(result, expected)
373373

374374

375375
def test_apply_mixed_dtype_corner_indexing():
376376
df = DataFrame({"A": ["foo"], "B": [1.0]})
377377
result = df.apply(lambda x: x["A"], axis=1)
378-
expected = Series(["foo"], index=[0])
378+
expected = Series(["foo"], index=range(1))
379379
tm.assert_series_equal(result, expected)
380380

381381
result = df.apply(lambda x: x["B"], axis=1)
382-
expected = Series([1.0], index=[0])
382+
expected = Series([1.0], index=range(1))
383383
tm.assert_series_equal(result, expected)
384384

385385

@@ -1037,7 +1037,7 @@ def test_result_type(int_frame_const_col):
10371037

10381038
result = df.apply(lambda x: [1, 2, 3], axis=1, result_type="expand")
10391039
expected = df.copy()
1040-
expected.columns = [0, 1, 2]
1040+
expected.columns = range(3)
10411041
tm.assert_frame_equal(result, expected)
10421042

10431043

@@ -1047,7 +1047,7 @@ def test_result_type_shorter_list(int_frame_const_col):
10471047
df = int_frame_const_col
10481048
result = df.apply(lambda x: [1, 2], axis=1, result_type="expand")
10491049
expected = df[["A", "B"]].copy()
1050-
expected.columns = [0, 1]
1050+
expected.columns = range(2)
10511051
tm.assert_frame_equal(result, expected)
10521052

10531053

pandas/tests/arithmetic/test_numeric.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1451,7 +1451,7 @@ def test_fill_value_inf_masking():
14511451
expected = pd.DataFrame(
14521452
{"A": [np.inf, 1.0, 0.0, 1.0], "B": [0.0, np.nan, 0.0, np.nan]}
14531453
)
1454-
tm.assert_frame_equal(result, expected)
1454+
tm.assert_frame_equal(result, expected, check_index_type=False)
14551455

14561456

14571457
def test_dataframe_div_silenced():

pandas/tests/computation/test_eval.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1800,7 +1800,7 @@ def test_numexpr_option_incompatible_op():
18001800
{"A": [True, False, True, False, None, None], "B": [1, 2, 3, 4, 5, 6]}
18011801
)
18021802
result = df.query("A.isnull()")
1803-
expected = DataFrame({"A": [None, None], "B": [5, 6]}, index=[4, 5])
1803+
expected = DataFrame({"A": [None, None], "B": [5, 6]}, index=range(4, 6))
18041804
tm.assert_frame_equal(result, expected)
18051805

18061806

pandas/tests/extension/base/getitem.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ def test_take_series(self, data):
408408
result = s.take([0, -1])
409409
expected = pd.Series(
410410
data._from_sequence([data[0], data[len(data) - 1]], dtype=s.dtype),
411-
index=[0, len(data) - 1],
411+
index=range(0, 198, 99),
412412
)
413413
tm.assert_series_equal(result, expected)
414414

@@ -428,7 +428,8 @@ def test_reindex(self, data, na_value):
428428

429429
result = s.reindex([n, n + 1])
430430
expected = pd.Series(
431-
data._from_sequence([na_value, na_value], dtype=s.dtype), index=[n, n + 1]
431+
data._from_sequence([na_value, na_value], dtype=s.dtype),
432+
index=range(n, n + 2, 1),
432433
)
433434
tm.assert_series_equal(result, expected)
434435

pandas/tests/extension/base/reshaping.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ def test_concat(self, data, in_frame):
3333

3434
@pytest.mark.parametrize("in_frame", [True, False])
3535
def test_concat_all_na_block(self, data_missing, in_frame):
36-
valid_block = pd.Series(data_missing.take([1, 1]), index=[0, 1])
37-
na_block = pd.Series(data_missing.take([0, 0]), index=[2, 3])
36+
valid_block = pd.Series(data_missing.take([1, 1]), index=range(2))
37+
na_block = pd.Series(data_missing.take([0, 0]), index=range(2, 4))
3838
if in_frame:
3939
valid_block = pd.DataFrame({"a": valid_block})
4040
na_block = pd.DataFrame({"a": na_block})

pandas/tests/extension/base/setitem.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ def test_setitem_preserves_views(self, data):
374374

375375
def test_setitem_with_expansion_dataframe_column(self, data, full_indexer):
376376
# https://github.com/pandas-dev/pandas/issues/32395
377-
df = expected = pd.DataFrame({0: pd.Series(data)})
377+
df = expected = pd.DataFrame(pd.Series(data))
378378
result = pd.DataFrame(index=df.index)
379379

380380
key = full_indexer(df)

pandas/tests/frame/indexing/test_indexing.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,7 @@ def test_single_element_ix_dont_upcast(self, float_frame):
991991
result = df.loc[0, "b"]
992992
assert is_integer(result)
993993

994-
expected = Series([666], [0], name="b")
994+
expected = Series([666], index=range(1), name="b")
995995
result = df.loc[[0], "b"]
996996
tm.assert_series_equal(result, expected)
997997

@@ -1193,7 +1193,7 @@ def test_type_error_multiindex(self):
11931193
# See gh-12218
11941194
mi = MultiIndex.from_product([["x", "y"], [0, 1]], names=[None, "c"])
11951195
dg = DataFrame(
1196-
[[1, 1, 2, 2], [3, 3, 4, 4]], columns=mi, index=Index([0, 1], name="i")
1196+
[[1, 1, 2, 2], [3, 3, 4, 4]], columns=mi, index=Index(range(2), name="i")
11971197
)
11981198
with pytest.raises(InvalidIndexError, match="slice"):
11991199
dg[:, 0]
@@ -1452,7 +1452,7 @@ def test_iloc_ea_series_indexer(self):
14521452
indexer = Series([0, 1], dtype="Int64")
14531453
row_indexer = Series([1], dtype="Int64")
14541454
result = df.iloc[row_indexer, indexer]
1455-
expected = DataFrame([[5, 6]], index=[1])
1455+
expected = DataFrame([[5, 6]], index=range(1, 2))
14561456
tm.assert_frame_equal(result, expected)
14571457

14581458
result = df.iloc[row_indexer.values, indexer.values]

pandas/tests/frame/indexing/test_setitem.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def test_setitem_timestamp_empty_columns(self):
165165
df["now"] = Timestamp("20130101", tz="UTC")
166166

167167
expected = DataFrame(
168-
[[Timestamp("20130101", tz="UTC")]] * 3, index=[0, 1, 2], columns=["now"]
168+
[[Timestamp("20130101", tz="UTC")]] * 3, index=range(3), columns=["now"]
169169
)
170170
tm.assert_frame_equal(df, expected)
171171

pandas/tests/frame/methods/test_compare.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ def test_compare_axis(align_axis):
2121
result = df.compare(df2, align_axis=align_axis)
2222

2323
if align_axis in (1, "columns"):
24-
indices = pd.Index([0, 2])
24+
indices = pd.RangeIndex(0, 4, 2)
2525
columns = pd.MultiIndex.from_product([["col1", "col3"], ["self", "other"]])
2626
expected = pd.DataFrame(
2727
[["a", "c", np.nan, np.nan], [np.nan, np.nan, 3.0, 4.0]],
2828
index=indices,
2929
columns=columns,
3030
)
3131
else:
32-
indices = pd.MultiIndex.from_product([[0, 2], ["self", "other"]])
32+
indices = pd.MultiIndex.from_product([range(0, 4, 2), ["self", "other"]])
3333
columns = pd.Index(["col1", "col3"])
3434
expected = pd.DataFrame(
3535
[["a", np.nan], ["c", np.nan], [np.nan, 3.0], [np.nan, 4.0]],
@@ -60,7 +60,7 @@ def test_compare_various_formats(keep_shape, keep_equal):
6060
result = df.compare(df2, keep_shape=keep_shape, keep_equal=keep_equal)
6161

6262
if keep_shape:
63-
indices = pd.Index([0, 1, 2])
63+
indices = pd.RangeIndex(3)
6464
columns = pd.MultiIndex.from_product(
6565
[["col1", "col2", "col3"], ["self", "other"]]
6666
)
@@ -85,7 +85,7 @@ def test_compare_various_formats(keep_shape, keep_equal):
8585
columns=columns,
8686
)
8787
else:
88-
indices = pd.Index([0, 2])
88+
indices = pd.RangeIndex(0, 4, 2)
8989
columns = pd.MultiIndex.from_product([["col1", "col3"], ["self", "other"]])
9090
expected = pd.DataFrame(
9191
[["a", "c", 1.0, 1.0], ["c", "c", 3.0, 4.0]], index=indices, columns=columns
@@ -203,6 +203,7 @@ def test_compare_result_names():
203203
},
204204
)
205205
result = df1.compare(df2, result_names=("left", "right"))
206+
result.index = pd.Index([0, 2])
206207
expected = pd.DataFrame(
207208
{
208209
("col1", "left"): {0: "a", 2: np.nan},

pandas/tests/frame/methods/test_drop_duplicates.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,10 +411,15 @@ def test_drop_duplicates_inplace():
411411
@pytest.mark.parametrize(
412412
"origin_dict, output_dict, ignore_index, output_index",
413413
[
414-
({"A": [2, 2, 3]}, {"A": [2, 3]}, True, [0, 1]),
415-
({"A": [2, 2, 3]}, {"A": [2, 3]}, False, [0, 2]),
416-
({"A": [2, 2, 3], "B": [2, 2, 4]}, {"A": [2, 3], "B": [2, 4]}, True, [0, 1]),
417-
({"A": [2, 2, 3], "B": [2, 2, 4]}, {"A": [2, 3], "B": [2, 4]}, False, [0, 2]),
414+
({"A": [2, 2, 3]}, {"A": [2, 3]}, True, range(2)),
415+
({"A": [2, 2, 3]}, {"A": [2, 3]}, False, range(0, 4, 2)),
416+
({"A": [2, 2, 3], "B": [2, 2, 4]}, {"A": [2, 3], "B": [2, 4]}, True, range(2)),
417+
(
418+
{"A": [2, 2, 3], "B": [2, 2, 4]},
419+
{"A": [2, 3], "B": [2, 4]},
420+
False,
421+
range(0, 4, 2),
422+
),
418423
],
419424
)
420425
def test_drop_duplicates_ignore_index(

0 commit comments

Comments
 (0)