Skip to content

CI/CLN: Add more test file linting #37351

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 3 commits into from
Oct 23, 2020
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: 4 additions & 4 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ if [[ -z "$CHECK" || "$CHECK" == "patterns" ]]; then
RET=$(($RET + $?)) ; echo $MSG "DONE"

MSG='Check for inconsistent use of pandas namespace in tests' ; echo $MSG
check_namespace "Series"
RET=$(($RET + $?))
check_namespace "DataFrame"
RET=$(($RET + $?))
for class in "Series" "DataFrame" "Index"; do
check_namespace ${class}
RET=$(($RET + $?))
done
echo $MSG "DONE"
fi

Expand Down
34 changes: 16 additions & 18 deletions pandas/tests/arithmetic/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def adjust_negative_zero(zero, expected):
# TODO: remove this kludge once mypy stops giving false positives here
# List comprehension has incompatible type List[PandasObject]; expected List[RangeIndex]
# See GH#29725
ser_or_index: List[Any] = [Series, pd.Index]
ser_or_index: List[Any] = [Series, Index]
lefts: List[Any] = [pd.RangeIndex(10, 40, 10)]
lefts.extend(
[
Expand Down Expand Up @@ -100,7 +100,7 @@ def test_compare_invalid(self):
def test_numeric_cmp_string_numexpr_path(self, box_with_array):
# GH#36377, GH#35700
box = box_with_array
xbox = box if box is not pd.Index else np.ndarray
xbox = box if box is not Index else np.ndarray

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

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

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

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

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

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

exleft = pd.Index([np.nan, np.inf, np.inf, np.inf, np.inf], dtype=np.float64)
exright = pd.Index([np.nan, np.nan, np.nan, np.nan, np.nan], dtype=np.float64)
exleft = Index([np.nan, np.inf, np.inf, np.inf, np.inf], dtype=np.float64)
exright = Index([np.nan, np.nan, np.nan, np.nan, np.nan], dtype=np.float64)
exleft = adjust_negative_zero(zero, exleft)

result = divmod(idx, zero)
Expand All @@ -368,9 +368,7 @@ def test_div_negative_zero(self, zero, numeric_idx, op):
return
idx = numeric_idx - 3

expected = pd.Index(
[-np.inf, -np.inf, -np.inf, np.nan, np.inf], dtype=np.float64
)
expected = Index([-np.inf, -np.inf, -np.inf, np.nan, np.inf], dtype=np.float64)
expected = adjust_negative_zero(zero, expected)

result = op(idx, zero)
Expand Down Expand Up @@ -1045,7 +1043,7 @@ class TestUFuncCompat:
[pd.Int64Index, pd.UInt64Index, pd.Float64Index, pd.RangeIndex, Series],
)
def test_ufunc_compat(self, holder):
box = Series if holder is Series else pd.Index
box = Series if holder is Series else Index

if holder is pd.RangeIndex:
idx = pd.RangeIndex(0, 5)
Expand All @@ -1060,7 +1058,7 @@ def test_ufunc_compat(self, holder):
)
def test_ufunc_coercions(self, holder):
idx = holder([1, 2, 3, 4, 5], name="x")
box = Series if holder is Series else pd.Index
box = Series if holder is Series else Index

result = np.sqrt(idx)
assert result.dtype == "f8" and isinstance(result, box)
Expand Down Expand Up @@ -1104,7 +1102,7 @@ def test_ufunc_coercions(self, holder):
)
def test_ufunc_multiple_return_values(self, holder):
obj = holder([1, 2, 3], name="x")
box = Series if holder is Series else pd.Index
box = Series if holder is Series else Index

result = np.modf(obj)
assert isinstance(result, tuple)
Expand Down Expand Up @@ -1299,14 +1297,14 @@ def test_numeric_compat2(self):
def test_addsub_arithmetic(self, dtype, delta):
# GH#8142
delta = dtype(delta)
index = pd.Index([10, 11, 12], dtype=dtype)
index = Index([10, 11, 12], dtype=dtype)
result = index + delta
expected = pd.Index(index.values + delta, dtype=dtype)
expected = Index(index.values + delta, dtype=dtype)
tm.assert_index_equal(result, expected)

# this subtraction used to fail
result = index - delta
expected = pd.Index(index.values - delta, dtype=dtype)
expected = Index(index.values - delta, dtype=dtype)
tm.assert_index_equal(result, expected)

tm.assert_index_equal(index + index, 2 * index)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/categorical/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ def test_constructor_imaginary(self):
def test_constructor_string_and_tuples(self):
# GH 21416
c = pd.Categorical(np.array(["c", ("a", "b"), ("b", "a"), "c"], dtype=object))
expected_index = pd.Index([("a", "b"), ("b", "a"), "c"])
expected_index = Index([("a", "b"), ("b", "a"), "c"])
assert c.categories.equals(expected_index)

def test_interval(self):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/base/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,6 @@ def test_access_by_position(index):

def test_get_indexer_non_unique_dtype_mismatch():
# GH 25459
indexes, missing = pd.Index(["A", "B"]).get_indexer_non_unique(pd.Index([0]))
indexes, missing = Index(["A", "B"]).get_indexer_non_unique(Index([0]))
tm.assert_numpy_array_equal(np.array([-1], dtype=np.intp), indexes)
tm.assert_numpy_array_equal(np.array([0], dtype=np.intp), missing)
2 changes: 1 addition & 1 deletion pandas/tests/base/test_value_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_value_counts(index_or_series_obj):
expected = Series(dict(counter.most_common()), dtype=np.int64, name=obj.name)
expected.index = expected.index.astype(obj.dtype)
if isinstance(obj, pd.MultiIndex):
expected.index = pd.Index(expected.index)
expected.index = Index(expected.index)

# TODO: Order of entries with the same count is inconsistent on CI (gh-32449)
if obj.duplicated().any():
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ def test_maybe_convert_objects_nullable_integer(self, exp):

def test_maybe_convert_objects_bool_nan(self):
# GH32146
ind = pd.Index([True, False, np.nan], dtype=object)
ind = Index([True, False, np.nan], dtype=object)
exp = np.array([True, False, np.nan], dtype=object)
out = lib.maybe_convert_objects(ind.values, safe=1)
tm.assert_numpy_array_equal(out, exp)
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ def test_setitem_empty_frame_with_boolean(self, dtype, kwargs):

def test_setitem_with_empty_listlike(self):
# GH #17101
index = pd.Index([], name="idx")
index = Index([], name="idx")
result = DataFrame(columns=["A"], index=index)
result["A"] = []
expected = DataFrame(columns=["A"], index=index)
Expand Down Expand Up @@ -1630,11 +1630,11 @@ def test_loc_getitem_index_namedtuple(self):
@pytest.mark.parametrize("tpl", [tuple([1]), tuple([1, 2])])
def test_loc_getitem_index_single_double_tuples(self, tpl):
# GH 20991
idx = pd.Index([tuple([1]), tuple([1, 2])], name="A", tupleize_cols=False)
idx = Index([tuple([1]), tuple([1, 2])], name="A", tupleize_cols=False)
df = DataFrame(index=idx)

result = df.loc[[tpl]]
idx = pd.Index([tpl], name="A", tupleize_cols=False)
idx = Index([tpl], name="A", tupleize_cols=False)
expected = DataFrame(index=idx)
tm.assert_frame_equal(result, expected)

Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/frame/indexing/test_xs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import numpy as np
import pytest

import pandas as pd
from pandas import DataFrame, Series
from pandas import DataFrame, Index, Series
import pandas._testing as tm

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

def test_xs_duplicates(self):
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/frame/methods/test_align.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ def test_align_mixed_int(self, mixed_int_frame):
"l_ordered,r_ordered,expected",
[
[True, True, pd.CategoricalIndex],
[True, False, pd.Index],
[False, True, pd.Index],
[True, False, Index],
[False, True, Index],
[False, False, pd.CategoricalIndex],
],
)
Expand Down Expand Up @@ -196,7 +196,7 @@ def test_align_multiindex(self):
midx = pd.MultiIndex.from_product(
[range(2), range(3), range(2)], names=("a", "b", "c")
)
idx = pd.Index(range(2), name="b")
idx = Index(range(2), name="b")
df1 = DataFrame(np.arange(12, dtype="int64"), index=midx)
df2 = DataFrame(np.arange(2, dtype="int64"), index=idx)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/methods/test_drop.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def test_drop(self):
tm.assert_frame_equal(nu_df.drop("b", axis="columns"), nu_df["a"])
tm.assert_frame_equal(nu_df.drop([]), nu_df) # GH 16398

nu_df = nu_df.set_index(pd.Index(["X", "Y", "X"]))
nu_df = nu_df.set_index(Index(["X", "Y", "X"]))
nu_df.columns = list("abc")
tm.assert_frame_equal(nu_df.drop("X", axis="rows"), nu_df.loc[["Y"], :])
tm.assert_frame_equal(nu_df.drop(["X", "Y"], axis=0), nu_df.loc[[], :])
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1661,16 +1661,16 @@ def test_constructor_Series_differently_indexed(self):
def test_constructor_index_names(self, name_in1, name_in2, name_in3, name_out):
# GH13475
indices = [
pd.Index(["a", "b", "c"], name=name_in1),
pd.Index(["b", "c", "d"], name=name_in2),
pd.Index(["c", "d", "e"], name=name_in3),
Index(["a", "b", "c"], name=name_in1),
Index(["b", "c", "d"], name=name_in2),
Index(["c", "d", "e"], name=name_in3),
]
series = {
c: Series([0, 1, 2], index=i) for i, c in zip(indices, ["x", "y", "z"])
}
result = DataFrame(series)

exp_ind = pd.Index(["a", "b", "c", "d", "e"], name=name_out)
exp_ind = Index(["a", "b", "c", "d", "e"], name=name_out)
expected = DataFrame(
{
"x": [0, 1, 2, np.nan, np.nan],
Expand Down
14 changes: 7 additions & 7 deletions pandas/tests/frame/test_stack_unstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def test_unstack_tuplename_in_multiindex(self):
],
names=[None, ("A", "a")],
),
index=pd.Index([1, 2, 3], name=("B", "b")),
index=Index([1, 2, 3], name=("B", "b")),
)
tm.assert_frame_equal(result, expected)

Expand All @@ -301,7 +301,7 @@ def test_unstack_tuplename_in_multiindex(self):
(
(("A", "a"), "B"),
[[1, 1, 1, 1, 2, 2, 2, 2], [1, 1, 1, 1, 2, 2, 2, 2]],
pd.Index([3, 4], name="C"),
Index([3, 4], name="C"),
pd.MultiIndex.from_tuples(
[
("d", "a", 1),
Expand Down Expand Up @@ -512,7 +512,7 @@ def test_unstack_level_binding(self):
[[np.nan, 0], [0, np.nan], [np.nan, 0], [0, np.nan]], dtype=np.float64
),
index=expected_mi,
columns=pd.Index(["a", "b"], name="third"),
columns=Index(["a", "b"], name="third"),
)

tm.assert_frame_equal(result, expected)
Expand Down Expand Up @@ -703,7 +703,7 @@ def test_unstack_long_index(self):
[[0, 0, 1, 0, 0, 0, 1]],
names=["c1", "i2", "i3", "i4", "i5", "i6", "i7"],
),
index=pd.Index([0], name="i1"),
index=Index([0], name="i1"),
)
tm.assert_frame_equal(result, expected)

Expand Down Expand Up @@ -1153,7 +1153,7 @@ def test_unstack_timezone_aware_values():
result = df.set_index(["a", "b"]).unstack()
expected = DataFrame(
[[pd.Timestamp("2017-08-27 01:00:00.709949+0000", tz="UTC"), "c"]],
index=pd.Index(["a"], name="a"),
index=Index(["a"], name="a"),
columns=pd.MultiIndex(
levels=[["timestamp", "c"], ["b"]],
codes=[[0, 1], [0, 0]],
Expand Down Expand Up @@ -1216,7 +1216,7 @@ def test_stack_positional_level_duplicate_column_names():
df = DataFrame([[1, 1, 1, 1]], columns=columns)
result = df.stack(0)

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

Expand Down Expand Up @@ -1276,7 +1276,7 @@ def test_unstack_partial(
columns=pd.MultiIndex.from_product(
[result_columns[2:], [index_product]], names=[None, "ix2"]
),
index=pd.Index([2], name="ix1"),
index=Index([2], name="ix1"),
)
tm.assert_frame_equal(result, expected)

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/frame/test_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def test_to_csv_from_csv4(self):
dt = pd.Timedelta(seconds=1)
df = DataFrame(
{"dt_data": [i * dt for i in range(3)]},
index=pd.Index([i * dt for i in range(3)], name="dt_index"),
index=Index([i * dt for i in range(3)], name="dt_index"),
)
df.to_csv(path)

Expand Down Expand Up @@ -1310,7 +1310,7 @@ def test_multi_index_header(self):

def test_to_csv_single_level_multi_index(self):
# see gh-26303
index = pd.Index([(1,), (2,), (3,)])
index = Index([(1,), (2,), (3,)])
df = DataFrame([[1, 2, 3]], columns=index)
df = df.reindex(columns=[(1,), (3,)])
expected = ",1,3\n0,1,3\n"
Expand Down
Loading