Skip to content

Commit 0686736

Browse files
authored
Move inconsistent namespace check to pre-commit, fixup more files (#37662)
* check for inconsistent namespace usage * doc * typos * verbose regex * use verbose flag * use verbose flag * match both directions * add test * don't import annotations from future * update extra couple of cases * 🚚 rename * typing * don't use subprocess * don't type tests * use pathlib
1 parent 0755915 commit 0686736

25 files changed

+193
-109
lines changed

.pre-commit-config.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ repos:
119119
entry: python scripts/validate_unwanted_patterns.py --validation-type="private_function_across_module"
120120
types: [python]
121121
exclude: ^(asv_bench|pandas/tests|doc)/
122+
- id: inconsistent-namespace-usage
123+
name: 'Check for inconsistent use of pandas namespace in tests'
124+
entry: python scripts/check_for_inconsistent_pandas_namespace.py
125+
language: python
126+
types: [python]
127+
files: ^pandas/tests/
122128
- id: FrameOrSeriesUnion
123129
name: Check for use of Union[Series, DataFrame] instead of FrameOrSeriesUnion alias
124130
entry: Union\[.*(Series.*DataFrame|DataFrame.*Series).*\]

ci/code_checks.sh

-13
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@ function invgrep {
3737
return $((! $EXIT_STATUS))
3838
}
3939

40-
function check_namespace {
41-
local -r CLASS=${1}
42-
grep -R -l --include "*.py" " ${CLASS}(" pandas/tests | xargs grep -n "pd\.${CLASS}[(\.]"
43-
test $? -gt 0
44-
}
45-
4640
if [[ "$GITHUB_ACTIONS" == "true" ]]; then
4741
FLAKE8_FORMAT="##[error]%(path)s:%(row)s:%(col)s:%(code)s:%(text)s"
4842
INVGREP_PREPEND="##[error]"
@@ -120,13 +114,6 @@ if [[ -z "$CHECK" || "$CHECK" == "patterns" ]]; then
120114
MSG='Check for use of {foo!r} instead of {repr(foo)}' ; echo $MSG
121115
invgrep -R --include=*.{py,pyx} '!r}' pandas
122116
RET=$(($RET + $?)) ; echo $MSG "DONE"
123-
124-
# -------------------------------------------------------------------------
125-
MSG='Check for inconsistent use of pandas namespace in tests' ; echo $MSG
126-
for class in "Series" "DataFrame" "Index" "MultiIndex" "Timestamp" "Timedelta" "TimedeltaIndex" "DatetimeIndex" "Categorical"; do
127-
check_namespace ${class}
128-
RET=$(($RET + $?))
129-
done
130117
echo $MSG "DONE"
131118
fi
132119

pandas/tests/dtypes/test_inference.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ def test_infer_dtype_datetime_with_na(self, na_value, time_stamp):
862862
@pytest.mark.parametrize(
863863
"arr",
864864
[
865-
np.array([pd.Timedelta("1 days"), pd.Timedelta("2 days")]),
865+
np.array([Timedelta("1 days"), Timedelta("2 days")]),
866866
np.array([np.timedelta64(1, "D"), np.timedelta64(2, "D")], dtype=object),
867867
np.array([timedelta(1), timedelta(2)]),
868868
],

pandas/tests/extension/test_categorical.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def test_cast_category_to_extension_dtype(self, expected):
192192
(
193193
"datetime64[ns, MET]",
194194
pd.DatetimeIndex(
195-
[pd.Timestamp("2015-01-01 00:00:00+0100", tz="MET")]
195+
[Timestamp("2015-01-01 00:00:00+0100", tz="MET")]
196196
).array,
197197
),
198198
],
@@ -254,7 +254,7 @@ def _compare_other(self, s, data, op_name, other):
254254

255255
@pytest.mark.parametrize(
256256
"categories",
257-
[["a", "b"], [0, 1], [pd.Timestamp("2019"), pd.Timestamp("2020")]],
257+
[["a", "b"], [0, 1], [Timestamp("2019"), Timestamp("2020")]],
258258
)
259259
def test_not_equal_with_na(self, categories):
260260
# https://github.com/pandas-dev/pandas/issues/32276

pandas/tests/groupby/aggregate/test_other.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ def test_agg_over_numpy_arrays():
425425
result = df.groupby("category").agg(sum)
426426

427427
expected_data = [[np.array([50, 70, 90])], [np.array([20, 30, 40])]]
428-
expected_index = pd.Index([1, 2], name="category")
428+
expected_index = Index([1, 2], name="category")
429429
expected_column = ["arraydata"]
430430
expected = DataFrame(expected_data, index=expected_index, columns=expected_column)
431431

@@ -497,7 +497,7 @@ def test_sum_uint64_overflow():
497497
df = DataFrame([[1, 2], [3, 4], [5, 6]], dtype=object)
498498
df = df + 9223372036854775807
499499

500-
index = pd.Index(
500+
index = Index(
501501
[9223372036854775808, 9223372036854775810, 9223372036854775812], dtype=np.uint64
502502
)
503503
expected = DataFrame(
@@ -596,7 +596,7 @@ def test_agg_lambda_with_timezone():
596596
result = df.groupby("tag").agg({"date": lambda e: e.head(1)})
597597
expected = DataFrame(
598598
[pd.Timestamp("2018-01-01", tz="UTC")],
599-
index=pd.Index([1], name="tag"),
599+
index=Index([1], name="tag"),
600600
columns=["date"],
601601
)
602602
tm.assert_frame_equal(result, expected)

pandas/tests/groupby/test_counting.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import numpy as np
55
import pytest
66

7-
import pandas as pd
87
from pandas import (
98
DataFrame,
109
Index,
@@ -260,7 +259,7 @@ def test_groupby_timedelta_cython_count():
260259
df = DataFrame(
261260
{"g": list("ab" * 2), "delt": np.arange(4).astype("timedelta64[ns]")}
262261
)
263-
expected = Series([2, 2], index=pd.Index(["a", "b"], name="g"), name="delt")
262+
expected = Series([2, 2], index=Index(["a", "b"], name="g"), name="delt")
264263
result = df.groupby("g").delt.count()
265264
tm.assert_series_equal(expected, result)
266265

@@ -317,12 +316,12 @@ def test_count_non_nulls():
317316
def test_count_object():
318317
df = DataFrame({"a": ["a"] * 3 + ["b"] * 3, "c": [2] * 3 + [3] * 3})
319318
result = df.groupby("c").a.count()
320-
expected = Series([3, 3], index=pd.Index([2, 3], name="c"), name="a")
319+
expected = Series([3, 3], index=Index([2, 3], name="c"), name="a")
321320
tm.assert_series_equal(result, expected)
322321

323322
df = DataFrame({"a": ["a", np.nan, np.nan] + ["b"] * 3, "c": [2] * 3 + [3] * 3})
324323
result = df.groupby("c").a.count()
325-
expected = Series([1, 3], index=pd.Index([2, 3], name="c"), name="a")
324+
expected = Series([1, 3], index=Index([2, 3], name="c"), name="a")
326325
tm.assert_series_equal(result, expected)
327326

328327

@@ -354,7 +353,7 @@ def test_lower_int_prec_count():
354353
)
355354
result = df.groupby("grp").count()
356355
expected = DataFrame(
357-
{"a": [2, 2], "b": [2, 2], "c": [2, 2]}, index=pd.Index(list("ab"), name="grp")
356+
{"a": [2, 2], "b": [2, 2], "c": [2, 2]}, index=Index(list("ab"), name="grp")
358357
)
359358
tm.assert_frame_equal(result, expected)
360359

@@ -374,5 +373,5 @@ def __eq__(self, other):
374373

375374
df = DataFrame({"a": [RaisingObject() for _ in range(4)], "grp": list("ab" * 2)})
376375
result = df.groupby("grp").count()
377-
expected = DataFrame({"a": [2, 2]}, index=pd.Index(list("ab"), name="grp"))
376+
expected = DataFrame({"a": [2, 2]}, index=Index(list("ab"), name="grp"))
378377
tm.assert_frame_equal(result, expected)

pandas/tests/groupby/test_grouping.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -614,12 +614,12 @@ def test_list_grouper_with_nat(self):
614614

615615
# Grouper in a list grouping
616616
result = df.groupby([grouper])
617-
expected = {pd.Timestamp("2011-01-01"): Index(list(range(364)))}
617+
expected = {Timestamp("2011-01-01"): Index(list(range(364)))}
618618
tm.assert_dict_equal(result.groups, expected)
619619

620620
# Test case without a list
621621
result = df.groupby(grouper)
622-
expected = {pd.Timestamp("2011-01-01"): 365}
622+
expected = {Timestamp("2011-01-01"): 365}
623623
tm.assert_dict_equal(result.groups, expected)
624624

625625
@pytest.mark.parametrize(
@@ -938,12 +938,12 @@ def test_groupby_with_small_elem(self):
938938
grouped = df.groupby([pd.Grouper(freq="M"), "event"])
939939
assert len(grouped.groups) == 2
940940
assert grouped.ngroups == 2
941-
assert (pd.Timestamp("2014-09-30"), "start") in grouped.groups
942-
assert (pd.Timestamp("2013-10-31"), "start") in grouped.groups
941+
assert (Timestamp("2014-09-30"), "start") in grouped.groups
942+
assert (Timestamp("2013-10-31"), "start") in grouped.groups
943943

944-
res = grouped.get_group((pd.Timestamp("2014-09-30"), "start"))
944+
res = grouped.get_group((Timestamp("2014-09-30"), "start"))
945945
tm.assert_frame_equal(res, df.iloc[[0], :])
946-
res = grouped.get_group((pd.Timestamp("2013-10-31"), "start"))
946+
res = grouped.get_group((Timestamp("2013-10-31"), "start"))
947947
tm.assert_frame_equal(res, df.iloc[[1], :])
948948

949949
df = DataFrame(
@@ -953,12 +953,12 @@ def test_groupby_with_small_elem(self):
953953
grouped = df.groupby([pd.Grouper(freq="M"), "event"])
954954
assert len(grouped.groups) == 2
955955
assert grouped.ngroups == 2
956-
assert (pd.Timestamp("2014-09-30"), "start") in grouped.groups
957-
assert (pd.Timestamp("2013-10-31"), "start") in grouped.groups
956+
assert (Timestamp("2014-09-30"), "start") in grouped.groups
957+
assert (Timestamp("2013-10-31"), "start") in grouped.groups
958958

959-
res = grouped.get_group((pd.Timestamp("2014-09-30"), "start"))
959+
res = grouped.get_group((Timestamp("2014-09-30"), "start"))
960960
tm.assert_frame_equal(res, df.iloc[[0, 2], :])
961-
res = grouped.get_group((pd.Timestamp("2013-10-31"), "start"))
961+
res = grouped.get_group((Timestamp("2013-10-31"), "start"))
962962
tm.assert_frame_equal(res, df.iloc[[1], :])
963963

964964
# length=3
@@ -969,15 +969,15 @@ def test_groupby_with_small_elem(self):
969969
grouped = df.groupby([pd.Grouper(freq="M"), "event"])
970970
assert len(grouped.groups) == 3
971971
assert grouped.ngroups == 3
972-
assert (pd.Timestamp("2014-09-30"), "start") in grouped.groups
973-
assert (pd.Timestamp("2013-10-31"), "start") in grouped.groups
974-
assert (pd.Timestamp("2014-08-31"), "start") in grouped.groups
972+
assert (Timestamp("2014-09-30"), "start") in grouped.groups
973+
assert (Timestamp("2013-10-31"), "start") in grouped.groups
974+
assert (Timestamp("2014-08-31"), "start") in grouped.groups
975975

976-
res = grouped.get_group((pd.Timestamp("2014-09-30"), "start"))
976+
res = grouped.get_group((Timestamp("2014-09-30"), "start"))
977977
tm.assert_frame_equal(res, df.iloc[[0], :])
978-
res = grouped.get_group((pd.Timestamp("2013-10-31"), "start"))
978+
res = grouped.get_group((Timestamp("2013-10-31"), "start"))
979979
tm.assert_frame_equal(res, df.iloc[[1], :])
980-
res = grouped.get_group((pd.Timestamp("2014-08-31"), "start"))
980+
res = grouped.get_group((Timestamp("2014-08-31"), "start"))
981981
tm.assert_frame_equal(res, df.iloc[[2], :])
982982

983983
def test_grouping_string_repr(self):

pandas/tests/groupby/test_missing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ def test_groupby_column_index_name_lost_fill_funcs(func):
1111
# GH: 29764 groupby loses index sometimes
1212
df = DataFrame(
1313
[[1, 1.0, -1.0], [1, np.nan, np.nan], [1, 2.0, -2.0]],
14-
columns=pd.Index(["type", "a", "b"], name="idx"),
14+
columns=Index(["type", "a", "b"], name="idx"),
1515
)
1616
df_grouped = df.groupby(["type"])[["a", "b"]]
1717
result = getattr(df_grouped, func)().columns
18-
expected = pd.Index(["a", "b"], name="idx")
18+
expected = Index(["a", "b"], name="idx")
1919
tm.assert_index_equal(result, expected)
2020

2121

pandas/tests/groupby/test_quantile.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def test_quantile_missing_group_values_correct_results(
194194
df = DataFrame({"key": key, "val": val})
195195

196196
expected = DataFrame(
197-
expected_val, index=pd.Index(expected_key, name="key"), columns=["val"]
197+
expected_val, index=Index(expected_key, name="key"), columns=["val"]
198198
)
199199

200200
grp = df.groupby("key")
@@ -223,7 +223,7 @@ def test_groupby_quantile_nullable_array(values, q):
223223
idx = pd.MultiIndex.from_product((["x", "y"], q), names=["a", None])
224224
true_quantiles = [0.0, 0.5, 1.0]
225225
else:
226-
idx = pd.Index(["x", "y"], name="a")
226+
idx = Index(["x", "y"], name="a")
227227
true_quantiles = [0.5]
228228

229229
expected = pd.Series(true_quantiles * 2, index=idx, name="b")
@@ -251,6 +251,6 @@ def test_groupby_timedelta_quantile():
251251
pd.Timedelta("0 days 00:00:02.990000"),
252252
]
253253
},
254-
index=pd.Index([1, 2], name="group"),
254+
index=Index([1, 2], name="group"),
255255
)
256256
tm.assert_frame_equal(result, expected)

pandas/tests/groupby/test_timegrouper.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -452,15 +452,15 @@ def test_groupby_groups_datetimeindex(self):
452452
result = df.groupby(level="date").groups
453453
dates = ["2015-01-05", "2015-01-04", "2015-01-03", "2015-01-02", "2015-01-01"]
454454
expected = {
455-
Timestamp(date): pd.DatetimeIndex([date], name="date") for date in dates
455+
Timestamp(date): DatetimeIndex([date], name="date") for date in dates
456456
}
457457
tm.assert_dict_equal(result, expected)
458458

459459
grouped = df.groupby(level="date")
460460
for date in dates:
461461
result = grouped.get_group(date)
462462
data = [[df.loc[date, "A"], df.loc[date, "B"]]]
463-
expected_index = pd.DatetimeIndex([date], name="date", freq="D")
463+
expected_index = DatetimeIndex([date], name="date", freq="D")
464464
expected = DataFrame(data, columns=list("AB"), index=expected_index)
465465
tm.assert_frame_equal(result, expected)
466466

@@ -484,7 +484,7 @@ def test_groupby_groups_datetimeindex_tz(self):
484484
)
485485
df["datetime"] = df["datetime"].apply(lambda d: Timestamp(d, tz="US/Pacific"))
486486

487-
exp_idx1 = pd.DatetimeIndex(
487+
exp_idx1 = DatetimeIndex(
488488
[
489489
"2011-07-19 07:00:00",
490490
"2011-07-19 07:00:00",
@@ -508,13 +508,13 @@ def test_groupby_groups_datetimeindex_tz(self):
508508
tm.assert_frame_equal(result, expected)
509509

510510
# by level
511-
didx = pd.DatetimeIndex(dates, tz="Asia/Tokyo")
511+
didx = DatetimeIndex(dates, tz="Asia/Tokyo")
512512
df = DataFrame(
513513
{"value1": np.arange(6, dtype="int64"), "value2": [1, 2, 3, 1, 2, 3]},
514514
index=didx,
515515
)
516516

517-
exp_idx = pd.DatetimeIndex(
517+
exp_idx = DatetimeIndex(
518518
["2011-07-19 07:00:00", "2011-07-19 08:00:00", "2011-07-19 09:00:00"],
519519
tz="Asia/Tokyo",
520520
)

pandas/tests/groupby/transform/test_transform.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,7 @@ def test_categorical_and_not_categorical_key(observed):
11341134
# GH 32494
11351135
df_with_categorical = DataFrame(
11361136
{
1137-
"A": pd.Categorical(["a", "b", "a"], categories=["a", "b", "c"]),
1137+
"A": Categorical(["a", "b", "a"], categories=["a", "b", "c"]),
11381138
"B": [1, 2, 3],
11391139
"C": ["a", "b", "a"],
11401140
}

pandas/tests/indexes/datetimes/test_shift.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,25 @@ class TestDatetimeIndexShift:
2020
def test_dti_shift_tzaware(self, tz_naive_fixture):
2121
# GH#9903
2222
tz = tz_naive_fixture
23-
idx = pd.DatetimeIndex([], name="xxx", tz=tz)
23+
idx = DatetimeIndex([], name="xxx", tz=tz)
2424
tm.assert_index_equal(idx.shift(0, freq="H"), idx)
2525
tm.assert_index_equal(idx.shift(3, freq="H"), idx)
2626

27-
idx = pd.DatetimeIndex(
27+
idx = DatetimeIndex(
2828
["2011-01-01 10:00", "2011-01-01 11:00", "2011-01-01 12:00"],
2929
name="xxx",
3030
tz=tz,
3131
freq="H",
3232
)
3333
tm.assert_index_equal(idx.shift(0, freq="H"), idx)
34-
exp = pd.DatetimeIndex(
34+
exp = DatetimeIndex(
3535
["2011-01-01 13:00", "2011-01-01 14:00", "2011-01-01 15:00"],
3636
name="xxx",
3737
tz=tz,
3838
freq="H",
3939
)
4040
tm.assert_index_equal(idx.shift(3, freq="H"), exp)
41-
exp = pd.DatetimeIndex(
41+
exp = DatetimeIndex(
4242
["2011-01-01 07:00", "2011-01-01 08:00", "2011-01-01 09:00"],
4343
name="xxx",
4444
tz=tz,
@@ -51,21 +51,21 @@ def test_dti_shift_freqs(self):
5151
# GH#8083
5252
drange = pd.date_range("20130101", periods=5)
5353
result = drange.shift(1)
54-
expected = pd.DatetimeIndex(
54+
expected = DatetimeIndex(
5555
["2013-01-02", "2013-01-03", "2013-01-04", "2013-01-05", "2013-01-06"],
5656
freq="D",
5757
)
5858
tm.assert_index_equal(result, expected)
5959

6060
result = drange.shift(-1)
61-
expected = pd.DatetimeIndex(
61+
expected = DatetimeIndex(
6262
["2012-12-31", "2013-01-01", "2013-01-02", "2013-01-03", "2013-01-04"],
6363
freq="D",
6464
)
6565
tm.assert_index_equal(result, expected)
6666

6767
result = drange.shift(3, freq="2D")
68-
expected = pd.DatetimeIndex(
68+
expected = DatetimeIndex(
6969
["2013-01-07", "2013-01-08", "2013-01-09", "2013-01-10", "2013-01-11"],
7070
freq="D",
7171
)
@@ -84,7 +84,7 @@ def test_dti_shift_int(self):
8484

8585
def test_dti_shift_no_freq(self):
8686
# GH#19147
87-
dti = pd.DatetimeIndex(["2011-01-01 10:00", "2011-01-01"], freq=None)
87+
dti = DatetimeIndex(["2011-01-01 10:00", "2011-01-01"], freq=None)
8888
with pytest.raises(NullFrequencyError, match="Cannot shift with no freq"):
8989
dti.shift(2)
9090

pandas/tests/indexes/period/test_formats.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pytest
33

44
import pandas as pd
5-
from pandas import PeriodIndex
5+
from pandas import PeriodIndex, Series
66
import pandas._testing as tm
77

88

@@ -154,7 +154,7 @@ def test_representation_to_series(self):
154154
[idx1, idx2, idx3, idx4, idx5, idx6, idx7, idx8, idx9],
155155
[exp1, exp2, exp3, exp4, exp5, exp6, exp7, exp8, exp9],
156156
):
157-
result = repr(pd.Series(idx))
157+
result = repr(Series(idx))
158158
assert result == expected
159159

160160
def test_summary(self):

0 commit comments

Comments
 (0)