Skip to content

Commit 22799d7

Browse files
authored
CI/CLN: Lint more class references in tests (pandas-dev#37401)
* CI/CLN: Lint MultiIndex in tests * CI/CLN: Lint Timestamp in tests * Lint * Categorical * Fix
1 parent 5a45c0d commit 22799d7

File tree

103 files changed

+930
-983
lines changed

Some content is hidden

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

103 files changed

+930
-983
lines changed

ci/code_checks.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ function invgrep {
3838
}
3939

4040
function check_namespace {
41-
local -r CLASS="${1}"
42-
grep -R -l --include "*.py" " ${CLASS}(" pandas/tests | xargs grep -n "pd\.${CLASS}("
41+
local -r CLASS=${1}
42+
grep -R -l --include "*.py" " ${CLASS}(" pandas/tests | xargs grep -n "pd\.${CLASS}[(\.]"
4343
test $? -gt 0
4444
}
4545

@@ -146,7 +146,7 @@ if [[ -z "$CHECK" || "$CHECK" == "patterns" ]]; then
146146
RET=$(($RET + $?)) ; echo $MSG "DONE"
147147

148148
MSG='Check for inconsistent use of pandas namespace in tests' ; echo $MSG
149-
for class in "Series" "DataFrame" "Index"; do
149+
for class in "Series" "DataFrame" "Index" "MultiIndex" "Timestamp" "Timedelta" "TimedeltaIndex" "DatetimeIndex" "Categorical"; do
150150
check_namespace ${class}
151151
RET=$(($RET + $?))
152152
done

pandas/tests/arithmetic/test_datetime64.py

+62-64
Large diffs are not rendered by default.

pandas/tests/arithmetic/test_numeric.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def test_mul_td64arr(self, left, box_cls):
135135
right = np.array([1, 2, 3], dtype="m8[s]")
136136
right = box_cls(right)
137137

138-
expected = pd.TimedeltaIndex(["10s", "40s", "90s"])
138+
expected = TimedeltaIndex(["10s", "40s", "90s"])
139139
if isinstance(left, Series) or box_cls is Series:
140140
expected = Series(expected)
141141

@@ -155,7 +155,7 @@ def test_div_td64arr(self, left, box_cls):
155155
right = np.array([10, 40, 90], dtype="m8[s]")
156156
right = box_cls(right)
157157

158-
expected = pd.TimedeltaIndex(["1s", "2s", "3s"])
158+
expected = TimedeltaIndex(["1s", "2s", "3s"])
159159
if isinstance(left, Series) or box_cls is Series:
160160
expected = Series(expected)
161161

@@ -189,7 +189,7 @@ def test_numeric_arr_mul_tdscalar(self, scalar_td, numeric_idx, box_with_array):
189189
# GH#19333
190190
box = box_with_array
191191
index = numeric_idx
192-
expected = pd.TimedeltaIndex([pd.Timedelta(days=n) for n in range(len(index))])
192+
expected = TimedeltaIndex([Timedelta(days=n) for n in range(len(index))])
193193

194194
index = tm.box_expected(index, box)
195195
expected = tm.box_expected(expected, box)
@@ -244,10 +244,10 @@ def test_numeric_arr_rdiv_tdscalar(self, three_days, numeric_idx, box_with_array
244244
@pytest.mark.parametrize(
245245
"other",
246246
[
247-
pd.Timedelta(hours=31),
248-
pd.Timedelta(hours=31).to_pytimedelta(),
249-
pd.Timedelta(hours=31).to_timedelta64(),
250-
pd.Timedelta(hours=31).to_timedelta64().astype("m8[h]"),
247+
Timedelta(hours=31),
248+
Timedelta(hours=31).to_pytimedelta(),
249+
Timedelta(hours=31).to_timedelta64(),
250+
Timedelta(hours=31).to_timedelta64().astype("m8[h]"),
251251
np.timedelta64("NaT"),
252252
np.timedelta64("NaT", "D"),
253253
pd.offsets.Minute(3),

pandas/tests/arithmetic/test_object.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -196,17 +196,17 @@ def test_mixed_timezone_series_ops_object(self):
196196
# GH#13043
197197
ser = Series(
198198
[
199-
pd.Timestamp("2015-01-01", tz="US/Eastern"),
200-
pd.Timestamp("2015-01-01", tz="Asia/Tokyo"),
199+
Timestamp("2015-01-01", tz="US/Eastern"),
200+
Timestamp("2015-01-01", tz="Asia/Tokyo"),
201201
],
202202
name="xxx",
203203
)
204204
assert ser.dtype == object
205205

206206
exp = Series(
207207
[
208-
pd.Timestamp("2015-01-02", tz="US/Eastern"),
209-
pd.Timestamp("2015-01-02", tz="Asia/Tokyo"),
208+
Timestamp("2015-01-02", tz="US/Eastern"),
209+
Timestamp("2015-01-02", tz="Asia/Tokyo"),
210210
],
211211
name="xxx",
212212
)
@@ -216,8 +216,8 @@ def test_mixed_timezone_series_ops_object(self):
216216
# object series & object series
217217
ser2 = Series(
218218
[
219-
pd.Timestamp("2015-01-03", tz="US/Eastern"),
220-
pd.Timestamp("2015-01-05", tz="Asia/Tokyo"),
219+
Timestamp("2015-01-03", tz="US/Eastern"),
220+
Timestamp("2015-01-05", tz="Asia/Tokyo"),
221221
],
222222
name="xxx",
223223
)
@@ -326,7 +326,7 @@ def test_rsub_object(self):
326326
"foo" - index
327327

328328
with pytest.raises(TypeError, match=msg):
329-
np.array([True, pd.Timestamp.now()]) - index
329+
np.array([True, Timestamp.now()]) - index
330330

331331

332332
class MyIndex(pd.Index):

pandas/tests/arithmetic/test_period.py

+13-15
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from pandas.errors import PerformanceWarning
1111

1212
import pandas as pd
13-
from pandas import PeriodIndex, Series, TimedeltaIndex, period_range
13+
from pandas import PeriodIndex, Series, Timedelta, TimedeltaIndex, period_range
1414
import pandas._testing as tm
1515
from pandas.core import ops
1616
from pandas.core.arrays import TimedeltaArray
@@ -41,9 +41,7 @@ def test_compare_zerodim(self, box_with_array):
4141
expected = tm.box_expected(expected, xbox)
4242
tm.assert_equal(result, expected)
4343

44-
@pytest.mark.parametrize(
45-
"scalar", ["foo", pd.Timestamp.now(), pd.Timedelta(days=4)]
46-
)
44+
@pytest.mark.parametrize("scalar", ["foo", Timestamp.now(), Timedelta(days=4)])
4745
def test_compare_invalid_scalar(self, box_with_array, scalar):
4846
# comparison with scalar that cannot be interpreted as a Period
4947
pi = pd.period_range("2000", periods=4)
@@ -698,9 +696,9 @@ def test_parr_add_sub_float_raises(self, op, other, box_with_array):
698696
"other",
699697
[
700698
# datetime scalars
701-
pd.Timestamp.now(),
702-
pd.Timestamp.now().to_pydatetime(),
703-
pd.Timestamp.now().to_datetime64(),
699+
Timestamp.now(),
700+
Timestamp.now().to_pydatetime(),
701+
Timestamp.now().to_datetime64(),
704702
# datetime-like arrays
705703
pd.date_range("2016-01-01", periods=3, freq="H"),
706704
pd.date_range("2016-01-01", periods=3, tz="Europe/Brussels"),
@@ -733,7 +731,7 @@ def test_parr_add_sub_invalid(self, other, box_with_array):
733731

734732
def test_pi_add_sub_td64_array_non_tick_raises(self):
735733
rng = pd.period_range("1/1/2000", freq="Q", periods=3)
736-
tdi = pd.TimedeltaIndex(["-1 Day", "-1 Day", "-1 Day"])
734+
tdi = TimedeltaIndex(["-1 Day", "-1 Day", "-1 Day"])
737735
tdarr = tdi.values
738736

739737
msg = r"Cannot add or subtract timedelta64\[ns\] dtype from period\[Q-DEC\]"
@@ -752,7 +750,7 @@ def test_pi_add_sub_td64_array_tick(self):
752750
# PeriodIndex + Timedelta-like is allowed only with
753751
# tick-like frequencies
754752
rng = pd.period_range("1/1/2000", freq="90D", periods=3)
755-
tdi = pd.TimedeltaIndex(["-1 Day", "-1 Day", "-1 Day"])
753+
tdi = TimedeltaIndex(["-1 Day", "-1 Day", "-1 Day"])
756754
tdarr = tdi.values
757755

758756
expected = pd.period_range("12/31/1999", freq="90D", periods=3)
@@ -1227,7 +1225,7 @@ def test_parr_add_sub_object_array(self):
12271225
pi = pd.period_range("2000-12-31", periods=3, freq="D")
12281226
parr = pi.array
12291227

1230-
other = np.array([pd.Timedelta(days=1), pd.offsets.Day(2), 3])
1228+
other = np.array([Timedelta(days=1), pd.offsets.Day(2), 3])
12311229

12321230
with tm.assert_produces_warning(PerformanceWarning):
12331231
result = parr + other
@@ -1258,10 +1256,10 @@ def test_ops_series_timedelta(self):
12581256
name="xxx",
12591257
)
12601258

1261-
result = ser + pd.Timedelta("1 days")
1259+
result = ser + Timedelta("1 days")
12621260
tm.assert_series_equal(result, expected)
12631261

1264-
result = pd.Timedelta("1 days") + ser
1262+
result = Timedelta("1 days") + ser
12651263
tm.assert_series_equal(result, expected)
12661264

12671265
result = ser + pd.tseries.offsets.Day()
@@ -1492,7 +1490,7 @@ def test_pi_sub_period(self):
14921490
result = np.subtract(pd.Period("2012-01", freq="M"), idx)
14931491
tm.assert_index_equal(result, exp)
14941492

1495-
exp = pd.TimedeltaIndex([np.nan, np.nan, np.nan, np.nan], name="idx")
1493+
exp = TimedeltaIndex([np.nan, np.nan, np.nan, np.nan], name="idx")
14961494
result = idx - pd.Period("NaT", freq="M")
14971495
tm.assert_index_equal(result, exp)
14981496
assert result.freq == exp.freq
@@ -1506,7 +1504,7 @@ def test_pi_sub_pdnat(self):
15061504
idx = PeriodIndex(
15071505
["2011-01", "2011-02", "NaT", "2011-04"], freq="M", name="idx"
15081506
)
1509-
exp = pd.TimedeltaIndex([pd.NaT] * 4, name="idx")
1507+
exp = TimedeltaIndex([pd.NaT] * 4, name="idx")
15101508
tm.assert_index_equal(pd.NaT - idx, exp)
15111509
tm.assert_index_equal(idx - pd.NaT, exp)
15121510

@@ -1525,7 +1523,7 @@ def test_pi_sub_period_nat(self):
15251523
exp = pd.Index([12 * off, pd.NaT, 10 * off, 9 * off], name="idx")
15261524
tm.assert_index_equal(result, exp)
15271525

1528-
exp = pd.TimedeltaIndex([np.nan, np.nan, np.nan, np.nan], name="idx")
1526+
exp = TimedeltaIndex([np.nan, np.nan, np.nan, np.nan], name="idx")
15291527
tm.assert_index_equal(idx - pd.Period("NaT", freq="M"), exp)
15301528
tm.assert_index_equal(pd.Period("NaT", freq="M") - idx, exp)
15311529

pandas/tests/arithmetic/test_timedelta64.py

+15-19
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def test_td64arr_cmp_arraylike_invalid(self, other):
119119
def test_td64arr_cmp_mixed_invalid(self):
120120
rng = timedelta_range("1 days", periods=5)._data
121121

122-
other = np.array([0, 1, 2, rng[3], pd.Timestamp.now()])
122+
other = np.array([0, 1, 2, rng[3], Timestamp.now()])
123123
result = rng == other
124124
expected = np.array([False, False, False, True, False])
125125
tm.assert_numpy_array_equal(result, expected)
@@ -143,10 +143,8 @@ class TestTimedelta64ArrayComparisons:
143143

144144
@pytest.mark.parametrize("dtype", [None, object])
145145
def test_comp_nat(self, dtype):
146-
left = pd.TimedeltaIndex(
147-
[pd.Timedelta("1 days"), pd.NaT, pd.Timedelta("3 days")]
148-
)
149-
right = pd.TimedeltaIndex([pd.NaT, pd.NaT, pd.Timedelta("3 days")])
146+
left = TimedeltaIndex([Timedelta("1 days"), pd.NaT, Timedelta("3 days")])
147+
right = TimedeltaIndex([pd.NaT, pd.NaT, Timedelta("3 days")])
150148

151149
lhs, rhs = left, right
152150
if dtype is object:
@@ -173,7 +171,7 @@ def test_comp_nat(self, dtype):
173171
tm.assert_numpy_array_equal(pd.NaT > lhs, expected)
174172

175173
def test_comparisons_nat(self):
176-
tdidx1 = pd.TimedeltaIndex(
174+
tdidx1 = TimedeltaIndex(
177175
[
178176
"1 day",
179177
pd.NaT,
@@ -183,7 +181,7 @@ def test_comparisons_nat(self):
183181
"5 day 00:00:03",
184182
]
185183
)
186-
tdidx2 = pd.TimedeltaIndex(
184+
tdidx2 = TimedeltaIndex(
187185
["2 day", "2 day", pd.NaT, pd.NaT, "1 day 00:00:02", "5 days 00:00:03"]
188186
)
189187
tdarr = np.array(
@@ -1030,7 +1028,7 @@ def test_tdi_sub_dt64_array(self, box_with_array):
10301028
dti = pd.date_range("2016-01-01", periods=3)
10311029
tdi = dti - dti.shift(1)
10321030
dtarr = dti.values
1033-
expected = pd.DatetimeIndex(dtarr) - tdi
1031+
expected = DatetimeIndex(dtarr) - tdi
10341032

10351033
tdi = tm.box_expected(tdi, box_with_array)
10361034
expected = tm.box_expected(expected, box_with_array)
@@ -1047,7 +1045,7 @@ def test_tdi_add_dt64_array(self, box_with_array):
10471045
dti = pd.date_range("2016-01-01", periods=3)
10481046
tdi = dti - dti.shift(1)
10491047
dtarr = dti.values
1050-
expected = pd.DatetimeIndex(dtarr) + tdi
1048+
expected = DatetimeIndex(dtarr) + tdi
10511049

10521050
tdi = tm.box_expected(tdi, box_with_array)
10531051
expected = tm.box_expected(expected, box_with_array)
@@ -1062,7 +1060,7 @@ def test_td64arr_add_datetime64_nat(self, box_with_array):
10621060
other = np.datetime64("NaT")
10631061

10641062
tdi = timedelta_range("1 day", periods=3)
1065-
expected = pd.DatetimeIndex(["NaT", "NaT", "NaT"])
1063+
expected = DatetimeIndex(["NaT", "NaT", "NaT"])
10661064

10671065
tdser = tm.box_expected(tdi, box_with_array)
10681066
expected = tm.box_expected(expected, box_with_array)
@@ -1246,9 +1244,9 @@ def test_td64arr_add_sub_tdi(self, box_with_array, names):
12461244
def test_td64arr_add_sub_td64_nat(self, box_with_array):
12471245
# GH#23320 special handling for timedelta64("NaT")
12481246
box = box_with_array
1249-
tdi = pd.TimedeltaIndex([NaT, Timedelta("1s")])
1247+
tdi = TimedeltaIndex([NaT, Timedelta("1s")])
12501248
other = np.timedelta64("NaT")
1251-
expected = pd.TimedeltaIndex(["NaT"] * 2)
1249+
expected = TimedeltaIndex(["NaT"] * 2)
12521250

12531251
obj = tm.box_expected(tdi, box)
12541252
expected = tm.box_expected(expected, box)
@@ -1472,14 +1470,14 @@ def test_td64arr_add_sub_object_array(self, box_with_array):
14721470
tdarr = tm.box_expected(tdi, box)
14731471

14741472
other = np.array(
1475-
[pd.Timedelta(days=1), pd.offsets.Day(2), pd.Timestamp("2000-01-04")]
1473+
[Timedelta(days=1), pd.offsets.Day(2), Timestamp("2000-01-04")]
14761474
)
14771475

14781476
with tm.assert_produces_warning(PerformanceWarning):
14791477
result = tdarr + other
14801478

14811479
expected = pd.Index(
1482-
[pd.Timedelta(days=2), pd.Timedelta(days=4), pd.Timestamp("2000-01-07")]
1480+
[Timedelta(days=2), Timedelta(days=4), Timestamp("2000-01-07")]
14831481
)
14841482
expected = tm.box_expected(expected, xbox)
14851483
tm.assert_equal(result, expected)
@@ -1492,9 +1490,7 @@ def test_td64arr_add_sub_object_array(self, box_with_array):
14921490
with tm.assert_produces_warning(PerformanceWarning):
14931491
result = other - tdarr
14941492

1495-
expected = pd.Index(
1496-
[pd.Timedelta(0), pd.Timedelta(0), pd.Timestamp("2000-01-01")]
1497-
)
1493+
expected = pd.Index([Timedelta(0), Timedelta(0), Timestamp("2000-01-01")])
14981494
expected = tm.box_expected(expected, xbox)
14991495
tm.assert_equal(result, expected)
15001496

@@ -2188,9 +2184,9 @@ def test_td64arr_pow_invalid(self, scalar_td, box_with_array):
21882184

21892185
def test_add_timestamp_to_timedelta():
21902186
# GH: 35897
2191-
timestamp = pd.Timestamp.now()
2187+
timestamp = Timestamp.now()
21922188
result = timestamp + pd.timedelta_range("0s", "1s", periods=31)
2193-
expected = pd.DatetimeIndex(
2189+
expected = DatetimeIndex(
21942190
[
21952191
timestamp
21962192
+ (

pandas/tests/arrays/categorical/test_constructors.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def test_constructor_empty(self):
5454

5555
def test_constructor_empty_boolean(self):
5656
# see gh-22702
57-
cat = pd.Categorical([], categories=[True, False])
57+
cat = Categorical([], categories=[True, False])
5858
categories = sorted(cat.categories.tolist())
5959
assert categories == [False, True]
6060

@@ -412,7 +412,7 @@ def test_constructor_str_unknown(self):
412412

413413
def test_constructor_np_strs(self):
414414
# GH#31499 Hastable.map_locations needs to work on np.str_ objects
415-
cat = pd.Categorical(["1", "0", "1"], [np.str_("0"), np.str_("1")])
415+
cat = Categorical(["1", "0", "1"], [np.str_("0"), np.str_("1")])
416416
assert all(isinstance(x, np.str_) for x in cat.categories)
417417

418418
def test_constructor_from_categorical_with_dtype(self):
@@ -637,48 +637,48 @@ def test_constructor_imaginary(self):
637637

638638
def test_constructor_string_and_tuples(self):
639639
# GH 21416
640-
c = pd.Categorical(np.array(["c", ("a", "b"), ("b", "a"), "c"], dtype=object))
640+
c = Categorical(np.array(["c", ("a", "b"), ("b", "a"), "c"], dtype=object))
641641
expected_index = Index([("a", "b"), ("b", "a"), "c"])
642642
assert c.categories.equals(expected_index)
643643

644644
def test_interval(self):
645645
idx = pd.interval_range(0, 10, periods=10)
646-
cat = pd.Categorical(idx, categories=idx)
646+
cat = Categorical(idx, categories=idx)
647647
expected_codes = np.arange(10, dtype="int8")
648648
tm.assert_numpy_array_equal(cat.codes, expected_codes)
649649
tm.assert_index_equal(cat.categories, idx)
650650

651651
# infer categories
652-
cat = pd.Categorical(idx)
652+
cat = Categorical(idx)
653653
tm.assert_numpy_array_equal(cat.codes, expected_codes)
654654
tm.assert_index_equal(cat.categories, idx)
655655

656656
# list values
657-
cat = pd.Categorical(list(idx))
657+
cat = Categorical(list(idx))
658658
tm.assert_numpy_array_equal(cat.codes, expected_codes)
659659
tm.assert_index_equal(cat.categories, idx)
660660

661661
# list values, categories
662-
cat = pd.Categorical(list(idx), categories=list(idx))
662+
cat = Categorical(list(idx), categories=list(idx))
663663
tm.assert_numpy_array_equal(cat.codes, expected_codes)
664664
tm.assert_index_equal(cat.categories, idx)
665665

666666
# shuffled
667667
values = idx.take([1, 2, 0])
668-
cat = pd.Categorical(values, categories=idx)
668+
cat = Categorical(values, categories=idx)
669669
tm.assert_numpy_array_equal(cat.codes, np.array([1, 2, 0], dtype="int8"))
670670
tm.assert_index_equal(cat.categories, idx)
671671

672672
# extra
673673
values = pd.interval_range(8, 11, periods=3)
674-
cat = pd.Categorical(values, categories=idx)
674+
cat = Categorical(values, categories=idx)
675675
expected_codes = np.array([8, 9, -1], dtype="int8")
676676
tm.assert_numpy_array_equal(cat.codes, expected_codes)
677677
tm.assert_index_equal(cat.categories, idx)
678678

679679
# overlapping
680680
idx = pd.IntervalIndex([pd.Interval(0, 2), pd.Interval(0, 1)])
681-
cat = pd.Categorical(idx, categories=idx)
681+
cat = Categorical(idx, categories=idx)
682682
expected_codes = np.array([0, 1], dtype="int8")
683683
tm.assert_numpy_array_equal(cat.codes, expected_codes)
684684
tm.assert_index_equal(cat.categories, idx)

0 commit comments

Comments
 (0)