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

+5-5
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

+1-1
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

+1-1
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

+3-2
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

+2-2
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

+1-1
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

+3-3
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

+1-1
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

+5-4
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

+9-4
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(

pandas/tests/frame/methods/test_dropna.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def test_dropna_tz_aware_datetime(self):
195195
# Ex2
196196
df = DataFrame({"Time": [dt1, None, np.nan, dt2]})
197197
result = df.dropna(axis=0)
198-
expected = DataFrame([dt1, dt2], columns=["Time"], index=[0, 3])
198+
expected = DataFrame([dt1, dt2], columns=["Time"], index=range(0, 6, 3))
199199
tm.assert_frame_equal(result, expected)
200200

201201
def test_dropna_categorical_interval_index(self):
@@ -233,7 +233,7 @@ def test_set_single_column_subset(self):
233233
# GH 41021
234234
df = DataFrame({"A": [1, 2, 3], "B": list("abc"), "C": [4, np.nan, 5]})
235235
expected = DataFrame(
236-
{"A": [1, 3], "B": list("ac"), "C": [4.0, 5.0]}, index=[0, 2]
236+
{"A": [1, 3], "B": list("ac"), "C": [4.0, 5.0]}, index=range(0, 4, 2)
237237
)
238238
result = df.dropna(subset="C")
239239
tm.assert_frame_equal(result, expected)

pandas/tests/frame/methods/test_explode.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def test_ignore_index():
210210
df = pd.DataFrame({"id": range(0, 20, 10), "values": [list("ab"), list("cd")]})
211211
result = df.explode("values", ignore_index=True)
212212
expected = pd.DataFrame(
213-
{"id": [0, 0, 10, 10], "values": list("abcd")}, index=[0, 1, 2, 3]
213+
{"id": [0, 0, 10, 10], "values": list("abcd")}, index=range(4)
214214
)
215215
tm.assert_frame_equal(result, expected)
216216

pandas/tests/frame/methods/test_nlargest.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def test_nlargest_n(self, nselect_method, n, order):
8282
else:
8383
ascending = nselect_method == "nsmallest"
8484
result = getattr(df, nselect_method)(n, order)
85+
result.index = pd.Index(list(result.index))
8586
expected = df.sort_values(order, ascending=ascending).head(n)
8687
tm.assert_frame_equal(result, expected)
8788

@@ -132,7 +133,7 @@ def test_nlargest_n_identical_values(self):
132133
df = pd.DataFrame({"a": [1] * 5, "b": [1, 2, 3, 4, 5]})
133134

134135
result = df.nlargest(3, "a")
135-
expected = pd.DataFrame({"a": [1] * 3, "b": [1, 2, 3]}, index=[0, 1, 2])
136+
expected = pd.DataFrame({"a": [1] * 3, "b": [1, 2, 3]}, index=range(3))
136137
tm.assert_frame_equal(result, expected)
137138

138139
result = df.nsmallest(3, "a")
@@ -179,18 +180,20 @@ def test_nlargest_duplicate_keep_all_ties(self):
179180
result = df.nlargest(4, "a", keep="all")
180181
expected = pd.DataFrame(
181182
{
182-
"a": {0: 5, 1: 4, 2: 4, 4: 3, 5: 3, 6: 3, 7: 3},
183-
"b": {0: 10, 1: 9, 2: 8, 4: 5, 5: 50, 6: 10, 7: 20},
184-
}
183+
"a": [5, 4, 4, 3, 3, 3, 3],
184+
"b": [10, 9, 8, 5, 50, 10, 20],
185+
},
186+
index=[0, 1, 2, 4, 5, 6, 7],
185187
)
186188
tm.assert_frame_equal(result, expected)
187189

188190
result = df.nsmallest(2, "a", keep="all")
189191
expected = pd.DataFrame(
190192
{
191-
"a": {3: 2, 4: 3, 5: 3, 6: 3, 7: 3},
192-
"b": {3: 7, 4: 5, 5: 50, 6: 10, 7: 20},
193-
}
193+
"a": [2, 3, 3, 3, 3],
194+
"b": [7, 5, 50, 10, 20],
195+
},
196+
index=range(3, 8),
194197
)
195198
tm.assert_frame_equal(result, expected)
196199

pandas/tests/frame/methods/test_quantile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def test_axis_numeric_only_true(self, interp_method):
127127
result = df.quantile(
128128
0.5, axis=1, numeric_only=True, interpolation=interpolation, method=method
129129
)
130-
expected = Series([3.0, 4.0], index=[0, 1], name=0.5)
130+
expected = Series([3.0, 4.0], index=range(2), name=0.5)
131131
if interpolation == "nearest":
132132
expected = expected.astype(np.int64)
133133
tm.assert_series_equal(result, expected)

pandas/tests/frame/methods/test_sort_values.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def test_sort_values_multicolumn_uint64(self):
170170
"a": pd.Series([18446637057563306014, 1162265347240853609]),
171171
"b": pd.Series([1, 2]),
172172
},
173-
index=pd.Index([1, 0]),
173+
index=range(1, -1, -1),
174174
)
175175

176176
tm.assert_frame_equal(result, expected)
@@ -360,7 +360,7 @@ def test_sort_values_nat_values_in_int_column(self):
360360
df_reversed = DataFrame(
361361
{"int": int_values[::-1], "float": float_values[::-1]},
362362
columns=["int", "float"],
363-
index=[1, 0],
363+
index=range(1, -1, -1),
364364
)
365365

366366
# NaT is not a "na" for int64 columns, so na_position must not
@@ -385,7 +385,7 @@ def test_sort_values_nat_values_in_int_column(self):
385385
df_reversed = DataFrame(
386386
{"datetime": [NaT, Timestamp("2016-01-01")], "float": float_values[::-1]},
387387
columns=["datetime", "float"],
388-
index=[1, 0],
388+
index=range(1, -1, -1),
389389
)
390390

391391
df_sorted = df.sort_values(["datetime", "float"], na_position="first")
@@ -540,19 +540,19 @@ def test_sort_values_na_position_with_categories_raises(self):
540540
@pytest.mark.parametrize(
541541
"original_dict, sorted_dict, ignore_index, output_index",
542542
[
543-
({"A": [1, 2, 3]}, {"A": [3, 2, 1]}, True, [0, 1, 2]),
544-
({"A": [1, 2, 3]}, {"A": [3, 2, 1]}, False, [2, 1, 0]),
543+
({"A": [1, 2, 3]}, {"A": [3, 2, 1]}, True, range(3)),
544+
({"A": [1, 2, 3]}, {"A": [3, 2, 1]}, False, range(2, -1, -1)),
545545
(
546546
{"A": [1, 2, 3], "B": [2, 3, 4]},
547547
{"A": [3, 2, 1], "B": [4, 3, 2]},
548548
True,
549-
[0, 1, 2],
549+
range(3),
550550
),
551551
(
552552
{"A": [1, 2, 3], "B": [2, 3, 4]},
553553
{"A": [3, 2, 1], "B": [4, 3, 2]},
554554
False,
555-
[2, 1, 0],
555+
range(2, -1, -1),
556556
),
557557
],
558558
)

pandas/tests/frame/methods/test_transpose.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def test_transpose_td64_intervals(self):
2525
df = DataFrame(ii)
2626

2727
result = df.T
28+
result.columns = Index(list(range(len(ii))))
2829
expected = DataFrame({i: ii[i : i + 1] for i in range(len(ii))})
2930
tm.assert_frame_equal(result, expected)
3031

@@ -153,7 +154,6 @@ def test_transpose_not_inferring_dt(self):
153154
result = df.T
154155
expected = DataFrame(
155156
[[Timestamp("2019-12-31"), Timestamp("2019-12-31")]],
156-
columns=[0, 1],
157157
index=["a"],
158158
dtype=object,
159159
)
@@ -175,7 +175,6 @@ def test_transpose_not_inferring_dt_mixed_blocks(self):
175175
[Timestamp("2019-12-31"), Timestamp("2019-12-31")],
176176
[Timestamp("2019-12-31"), Timestamp("2019-12-31")],
177177
],
178-
columns=[0, 1],
179178
index=["a", "b"],
180179
dtype=object,
181180
)

0 commit comments

Comments
 (0)