Skip to content

Commit 6799824

Browse files
authored
CI/CLN: Add more test file linting (#37351)
* CI/CLN: Add more test file linting * Fix
1 parent af71aa1 commit 6799824

Some content is hidden

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

54 files changed

+334
-357
lines changed

ci/code_checks.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,10 @@ if [[ -z "$CHECK" || "$CHECK" == "patterns" ]]; then
178178
RET=$(($RET + $?)) ; echo $MSG "DONE"
179179

180180
MSG='Check for inconsistent use of pandas namespace in tests' ; echo $MSG
181-
check_namespace "Series"
182-
RET=$(($RET + $?))
183-
check_namespace "DataFrame"
184-
RET=$(($RET + $?))
181+
for class in "Series" "DataFrame" "Index"; do
182+
check_namespace ${class}
183+
RET=$(($RET + $?))
184+
done
185185
echo $MSG "DONE"
186186
fi
187187

pandas/tests/arithmetic/test_numeric.py

+16-18
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def adjust_negative_zero(zero, expected):
4242
# TODO: remove this kludge once mypy stops giving false positives here
4343
# List comprehension has incompatible type List[PandasObject]; expected List[RangeIndex]
4444
# See GH#29725
45-
ser_or_index: List[Any] = [Series, pd.Index]
45+
ser_or_index: List[Any] = [Series, Index]
4646
lefts: List[Any] = [pd.RangeIndex(10, 40, 10)]
4747
lefts.extend(
4848
[
@@ -100,7 +100,7 @@ def test_compare_invalid(self):
100100
def test_numeric_cmp_string_numexpr_path(self, box_with_array):
101101
# GH#36377, GH#35700
102102
box = box_with_array
103-
xbox = box if box is not pd.Index else np.ndarray
103+
xbox = box if box is not Index else np.ndarray
104104

105105
obj = Series(np.random.randn(10 ** 5))
106106
obj = tm.box_expected(obj, box, transpose=False)
@@ -126,7 +126,7 @@ def test_numeric_cmp_string_numexpr_path(self, box_with_array):
126126
class TestNumericArraylikeArithmeticWithDatetimeLike:
127127

128128
# TODO: also check name retentention
129-
@pytest.mark.parametrize("box_cls", [np.array, pd.Index, Series])
129+
@pytest.mark.parametrize("box_cls", [np.array, Index, Series])
130130
@pytest.mark.parametrize(
131131
"left", lefts, ids=lambda x: type(x).__name__ + str(x.dtype)
132132
)
@@ -146,7 +146,7 @@ def test_mul_td64arr(self, left, box_cls):
146146
tm.assert_equal(result, expected)
147147

148148
# TODO: also check name retentention
149-
@pytest.mark.parametrize("box_cls", [np.array, pd.Index, Series])
149+
@pytest.mark.parametrize("box_cls", [np.array, Index, Series])
150150
@pytest.mark.parametrize(
151151
"left", lefts, ids=lambda x: type(x).__name__ + str(x.dtype)
152152
)
@@ -318,7 +318,7 @@ class TestDivisionByZero:
318318
def test_div_zero(self, zero, numeric_idx):
319319
idx = numeric_idx
320320

321-
expected = pd.Index([np.nan, np.inf, np.inf, np.inf, np.inf], dtype=np.float64)
321+
expected = Index([np.nan, np.inf, np.inf, np.inf, np.inf], dtype=np.float64)
322322
# We only adjust for Index, because Series does not yet apply
323323
# the adjustment correctly.
324324
expected2 = adjust_negative_zero(zero, expected)
@@ -331,7 +331,7 @@ def test_div_zero(self, zero, numeric_idx):
331331
def test_floordiv_zero(self, zero, numeric_idx):
332332
idx = numeric_idx
333333

334-
expected = pd.Index([np.nan, np.inf, np.inf, np.inf, np.inf], dtype=np.float64)
334+
expected = Index([np.nan, np.inf, np.inf, np.inf, np.inf], dtype=np.float64)
335335
# We only adjust for Index, because Series does not yet apply
336336
# the adjustment correctly.
337337
expected2 = adjust_negative_zero(zero, expected)
@@ -344,7 +344,7 @@ def test_floordiv_zero(self, zero, numeric_idx):
344344
def test_mod_zero(self, zero, numeric_idx):
345345
idx = numeric_idx
346346

347-
expected = pd.Index([np.nan, np.nan, np.nan, np.nan, np.nan], dtype=np.float64)
347+
expected = Index([np.nan, np.nan, np.nan, np.nan, np.nan], dtype=np.float64)
348348
result = idx % zero
349349
tm.assert_index_equal(result, expected)
350350
ser_compat = Series(idx).astype("i8") % np.array(zero).astype("i8")
@@ -353,8 +353,8 @@ def test_mod_zero(self, zero, numeric_idx):
353353
def test_divmod_zero(self, zero, numeric_idx):
354354
idx = numeric_idx
355355

356-
exleft = pd.Index([np.nan, np.inf, np.inf, np.inf, np.inf], dtype=np.float64)
357-
exright = pd.Index([np.nan, np.nan, np.nan, np.nan, np.nan], dtype=np.float64)
356+
exleft = Index([np.nan, np.inf, np.inf, np.inf, np.inf], dtype=np.float64)
357+
exright = Index([np.nan, np.nan, np.nan, np.nan, np.nan], dtype=np.float64)
358358
exleft = adjust_negative_zero(zero, exleft)
359359

360360
result = divmod(idx, zero)
@@ -368,9 +368,7 @@ def test_div_negative_zero(self, zero, numeric_idx, op):
368368
return
369369
idx = numeric_idx - 3
370370

371-
expected = pd.Index(
372-
[-np.inf, -np.inf, -np.inf, np.nan, np.inf], dtype=np.float64
373-
)
371+
expected = Index([-np.inf, -np.inf, -np.inf, np.nan, np.inf], dtype=np.float64)
374372
expected = adjust_negative_zero(zero, expected)
375373

376374
result = op(idx, zero)
@@ -1045,7 +1043,7 @@ class TestUFuncCompat:
10451043
[pd.Int64Index, pd.UInt64Index, pd.Float64Index, pd.RangeIndex, Series],
10461044
)
10471045
def test_ufunc_compat(self, holder):
1048-
box = Series if holder is Series else pd.Index
1046+
box = Series if holder is Series else Index
10491047

10501048
if holder is pd.RangeIndex:
10511049
idx = pd.RangeIndex(0, 5)
@@ -1060,7 +1058,7 @@ def test_ufunc_compat(self, holder):
10601058
)
10611059
def test_ufunc_coercions(self, holder):
10621060
idx = holder([1, 2, 3, 4, 5], name="x")
1063-
box = Series if holder is Series else pd.Index
1061+
box = Series if holder is Series else Index
10641062

10651063
result = np.sqrt(idx)
10661064
assert result.dtype == "f8" and isinstance(result, box)
@@ -1104,7 +1102,7 @@ def test_ufunc_coercions(self, holder):
11041102
)
11051103
def test_ufunc_multiple_return_values(self, holder):
11061104
obj = holder([1, 2, 3], name="x")
1107-
box = Series if holder is Series else pd.Index
1105+
box = Series if holder is Series else Index
11081106

11091107
result = np.modf(obj)
11101108
assert isinstance(result, tuple)
@@ -1299,14 +1297,14 @@ def test_numeric_compat2(self):
12991297
def test_addsub_arithmetic(self, dtype, delta):
13001298
# GH#8142
13011299
delta = dtype(delta)
1302-
index = pd.Index([10, 11, 12], dtype=dtype)
1300+
index = Index([10, 11, 12], dtype=dtype)
13031301
result = index + delta
1304-
expected = pd.Index(index.values + delta, dtype=dtype)
1302+
expected = Index(index.values + delta, dtype=dtype)
13051303
tm.assert_index_equal(result, expected)
13061304

13071305
# this subtraction used to fail
13081306
result = index - delta
1309-
expected = pd.Index(index.values - delta, dtype=dtype)
1307+
expected = Index(index.values - delta, dtype=dtype)
13101308
tm.assert_index_equal(result, expected)
13111309

13121310
tm.assert_index_equal(index + index, 2 * index)

pandas/tests/arrays/categorical/test_constructors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ def test_constructor_imaginary(self):
638638
def test_constructor_string_and_tuples(self):
639639
# GH 21416
640640
c = pd.Categorical(np.array(["c", ("a", "b"), ("b", "a"), "c"], dtype=object))
641-
expected_index = pd.Index([("a", "b"), ("b", "a"), "c"])
641+
expected_index = Index([("a", "b"), ("b", "a"), "c"])
642642
assert c.categories.equals(expected_index)
643643

644644
def test_interval(self):

pandas/tests/base/test_misc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,6 @@ def test_access_by_position(index):
200200

201201
def test_get_indexer_non_unique_dtype_mismatch():
202202
# GH 25459
203-
indexes, missing = pd.Index(["A", "B"]).get_indexer_non_unique(pd.Index([0]))
203+
indexes, missing = Index(["A", "B"]).get_indexer_non_unique(Index([0]))
204204
tm.assert_numpy_array_equal(np.array([-1], dtype=np.intp), indexes)
205205
tm.assert_numpy_array_equal(np.array([0], dtype=np.intp), missing)

pandas/tests/base/test_value_counts.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_value_counts(index_or_series_obj):
3333
expected = Series(dict(counter.most_common()), dtype=np.int64, name=obj.name)
3434
expected.index = expected.index.astype(obj.dtype)
3535
if isinstance(obj, pd.MultiIndex):
36-
expected.index = pd.Index(expected.index)
36+
expected.index = Index(expected.index)
3737

3838
# TODO: Order of entries with the same count is inconsistent on CI (gh-32449)
3939
if obj.duplicated().any():

pandas/tests/dtypes/test_inference.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ def test_maybe_convert_objects_nullable_integer(self, exp):
584584

585585
def test_maybe_convert_objects_bool_nan(self):
586586
# GH32146
587-
ind = pd.Index([True, False, np.nan], dtype=object)
587+
ind = Index([True, False, np.nan], dtype=object)
588588
exp = np.array([True, False, np.nan], dtype=object)
589589
out = lib.maybe_convert_objects(ind.values, safe=1)
590590
tm.assert_numpy_array_equal(out, exp)

pandas/tests/frame/indexing/test_indexing.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ def test_setitem_empty_frame_with_boolean(self, dtype, kwargs):
801801

802802
def test_setitem_with_empty_listlike(self):
803803
# GH #17101
804-
index = pd.Index([], name="idx")
804+
index = Index([], name="idx")
805805
result = DataFrame(columns=["A"], index=index)
806806
result["A"] = []
807807
expected = DataFrame(columns=["A"], index=index)
@@ -1630,11 +1630,11 @@ def test_loc_getitem_index_namedtuple(self):
16301630
@pytest.mark.parametrize("tpl", [tuple([1]), tuple([1, 2])])
16311631
def test_loc_getitem_index_single_double_tuples(self, tpl):
16321632
# GH 20991
1633-
idx = pd.Index([tuple([1]), tuple([1, 2])], name="A", tupleize_cols=False)
1633+
idx = Index([tuple([1]), tuple([1, 2])], name="A", tupleize_cols=False)
16341634
df = DataFrame(index=idx)
16351635

16361636
result = df.loc[[tpl]]
1637-
idx = pd.Index([tpl], name="A", tupleize_cols=False)
1637+
idx = Index([tpl], name="A", tupleize_cols=False)
16381638
expected = DataFrame(index=idx)
16391639
tm.assert_frame_equal(result, expected)
16401640

pandas/tests/frame/indexing/test_xs.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import numpy as np
44
import pytest
55

6-
import pandas as pd
7-
from pandas import DataFrame, Series
6+
from pandas import DataFrame, Index, Series
87
import pandas._testing as tm
98

109
from pandas.tseries.offsets import BDay
@@ -59,7 +58,7 @@ def test_xs_corner(self):
5958
# no columns but Index(dtype=object)
6059
df = DataFrame(index=["a", "b", "c"])
6160
result = df.xs("a")
62-
expected = Series([], name="a", index=pd.Index([]), dtype=np.float64)
61+
expected = Series([], name="a", index=Index([]), dtype=np.float64)
6362
tm.assert_series_equal(result, expected)
6463

6564
def test_xs_duplicates(self):

pandas/tests/frame/methods/test_align.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ def test_align_mixed_int(self, mixed_int_frame):
160160
"l_ordered,r_ordered,expected",
161161
[
162162
[True, True, pd.CategoricalIndex],
163-
[True, False, pd.Index],
164-
[False, True, pd.Index],
163+
[True, False, Index],
164+
[False, True, Index],
165165
[False, False, pd.CategoricalIndex],
166166
],
167167
)
@@ -196,7 +196,7 @@ def test_align_multiindex(self):
196196
midx = pd.MultiIndex.from_product(
197197
[range(2), range(3), range(2)], names=("a", "b", "c")
198198
)
199-
idx = pd.Index(range(2), name="b")
199+
idx = Index(range(2), name="b")
200200
df1 = DataFrame(np.arange(12, dtype="int64"), index=midx)
201201
df2 = DataFrame(np.arange(2, dtype="int64"), index=idx)
202202

pandas/tests/frame/methods/test_drop.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def test_drop(self):
141141
tm.assert_frame_equal(nu_df.drop("b", axis="columns"), nu_df["a"])
142142
tm.assert_frame_equal(nu_df.drop([]), nu_df) # GH 16398
143143

144-
nu_df = nu_df.set_index(pd.Index(["X", "Y", "X"]))
144+
nu_df = nu_df.set_index(Index(["X", "Y", "X"]))
145145
nu_df.columns = list("abc")
146146
tm.assert_frame_equal(nu_df.drop("X", axis="rows"), nu_df.loc[["Y"], :])
147147
tm.assert_frame_equal(nu_df.drop(["X", "Y"], axis=0), nu_df.loc[[], :])

pandas/tests/frame/test_constructors.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1661,16 +1661,16 @@ def test_constructor_Series_differently_indexed(self):
16611661
def test_constructor_index_names(self, name_in1, name_in2, name_in3, name_out):
16621662
# GH13475
16631663
indices = [
1664-
pd.Index(["a", "b", "c"], name=name_in1),
1665-
pd.Index(["b", "c", "d"], name=name_in2),
1666-
pd.Index(["c", "d", "e"], name=name_in3),
1664+
Index(["a", "b", "c"], name=name_in1),
1665+
Index(["b", "c", "d"], name=name_in2),
1666+
Index(["c", "d", "e"], name=name_in3),
16671667
]
16681668
series = {
16691669
c: Series([0, 1, 2], index=i) for i, c in zip(indices, ["x", "y", "z"])
16701670
}
16711671
result = DataFrame(series)
16721672

1673-
exp_ind = pd.Index(["a", "b", "c", "d", "e"], name=name_out)
1673+
exp_ind = Index(["a", "b", "c", "d", "e"], name=name_out)
16741674
expected = DataFrame(
16751675
{
16761676
"x": [0, 1, 2, np.nan, np.nan],

pandas/tests/frame/test_stack_unstack.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def test_unstack_tuplename_in_multiindex(self):
280280
],
281281
names=[None, ("A", "a")],
282282
),
283-
index=pd.Index([1, 2, 3], name=("B", "b")),
283+
index=Index([1, 2, 3], name=("B", "b")),
284284
)
285285
tm.assert_frame_equal(result, expected)
286286

@@ -301,7 +301,7 @@ def test_unstack_tuplename_in_multiindex(self):
301301
(
302302
(("A", "a"), "B"),
303303
[[1, 1, 1, 1, 2, 2, 2, 2], [1, 1, 1, 1, 2, 2, 2, 2]],
304-
pd.Index([3, 4], name="C"),
304+
Index([3, 4], name="C"),
305305
pd.MultiIndex.from_tuples(
306306
[
307307
("d", "a", 1),
@@ -512,7 +512,7 @@ def test_unstack_level_binding(self):
512512
[[np.nan, 0], [0, np.nan], [np.nan, 0], [0, np.nan]], dtype=np.float64
513513
),
514514
index=expected_mi,
515-
columns=pd.Index(["a", "b"], name="third"),
515+
columns=Index(["a", "b"], name="third"),
516516
)
517517

518518
tm.assert_frame_equal(result, expected)
@@ -703,7 +703,7 @@ def test_unstack_long_index(self):
703703
[[0, 0, 1, 0, 0, 0, 1]],
704704
names=["c1", "i2", "i3", "i4", "i5", "i6", "i7"],
705705
),
706-
index=pd.Index([0], name="i1"),
706+
index=Index([0], name="i1"),
707707
)
708708
tm.assert_frame_equal(result, expected)
709709

@@ -1153,7 +1153,7 @@ def test_unstack_timezone_aware_values():
11531153
result = df.set_index(["a", "b"]).unstack()
11541154
expected = DataFrame(
11551155
[[pd.Timestamp("2017-08-27 01:00:00.709949+0000", tz="UTC"), "c"]],
1156-
index=pd.Index(["a"], name="a"),
1156+
index=Index(["a"], name="a"),
11571157
columns=pd.MultiIndex(
11581158
levels=[["timestamp", "c"], ["b"]],
11591159
codes=[[0, 1], [0, 0]],
@@ -1216,7 +1216,7 @@ def test_stack_positional_level_duplicate_column_names():
12161216
df = DataFrame([[1, 1, 1, 1]], columns=columns)
12171217
result = df.stack(0)
12181218

1219-
new_columns = pd.Index(["y", "z"], name="a")
1219+
new_columns = Index(["y", "z"], name="a")
12201220
new_index = pd.MultiIndex.from_tuples([(0, "x"), (0, "y")], names=[None, "a"])
12211221
expected = DataFrame([[1, 1], [1, 1]], index=new_index, columns=new_columns)
12221222

@@ -1276,7 +1276,7 @@ def test_unstack_partial(
12761276
columns=pd.MultiIndex.from_product(
12771277
[result_columns[2:], [index_product]], names=[None, "ix2"]
12781278
),
1279-
index=pd.Index([2], name="ix1"),
1279+
index=Index([2], name="ix1"),
12801280
)
12811281
tm.assert_frame_equal(result, expected)
12821282

pandas/tests/frame/test_to_csv.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def test_to_csv_from_csv4(self):
135135
dt = pd.Timedelta(seconds=1)
136136
df = DataFrame(
137137
{"dt_data": [i * dt for i in range(3)]},
138-
index=pd.Index([i * dt for i in range(3)], name="dt_index"),
138+
index=Index([i * dt for i in range(3)], name="dt_index"),
139139
)
140140
df.to_csv(path)
141141

@@ -1310,7 +1310,7 @@ def test_multi_index_header(self):
13101310

13111311
def test_to_csv_single_level_multi_index(self):
13121312
# see gh-26303
1313-
index = pd.Index([(1,), (2,), (3,)])
1313+
index = Index([(1,), (2,), (3,)])
13141314
df = DataFrame([[1, 2, 3]], columns=index)
13151315
df = df.reindex(columns=[(1,), (3,)])
13161316
expected = ",1,3\n0,1,3\n"

0 commit comments

Comments
 (0)