Skip to content

Commit 16c693a

Browse files
authored
Updated script to check inconsistent pandas namespace (#37848)
1 parent 458d756 commit 16c693a

File tree

8 files changed

+44
-51
lines changed

8 files changed

+44
-51
lines changed

pandas/tests/arithmetic/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def zero(request):
8181
8282
Examples
8383
--------
84-
>>> arr = pd.RangeIndex(5)
84+
>>> arr = RangeIndex(5)
8585
>>> arr / zeros
8686
Float64Index([nan, inf, inf, inf, inf], dtype='float64')
8787
"""

pandas/tests/arithmetic/test_timedelta64.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ def test_addition_ops(self):
465465
tdi + pd.Int64Index([1, 2, 3])
466466

467467
# this is a union!
468-
# pytest.raises(TypeError, lambda : Int64Index([1,2,3]) + tdi)
468+
# pytest.raises(TypeError, lambda : pd.Int64Index([1,2,3]) + tdi)
469469

470470
result = tdi + dti # name will be reset
471471
expected = DatetimeIndex(["20130102", pd.NaT, "20130105"])

pandas/tests/arrays/categorical/test_constructors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ def test_interval(self):
677677
tm.assert_index_equal(cat.categories, idx)
678678

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

pandas/tests/frame/test_constructors.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -720,8 +720,8 @@ def test_constructor_period_dict(self):
720720
@pytest.mark.parametrize(
721721
"data,dtype",
722722
[
723-
(pd.Period("2012-01", freq="M"), "period[M]"),
724-
(pd.Period("2012-02-01", freq="D"), "period[D]"),
723+
(Period("2012-01", freq="M"), "period[M]"),
724+
(Period("2012-02-01", freq="D"), "period[D]"),
725725
(Interval(left=0, right=5), IntervalDtype("int64")),
726726
(Interval(left=0.1, right=0.5), IntervalDtype("float64")),
727727
],
@@ -2577,7 +2577,7 @@ def test_from_records_series_list_dict(self):
25772577
def test_from_records_series_categorical_index(self):
25782578
# GH 32805
25792579
index = CategoricalIndex(
2580-
[pd.Interval(-20, -10), pd.Interval(-10, 0), pd.Interval(0, 10)]
2580+
[Interval(-20, -10), Interval(-10, 0), Interval(0, 10)]
25812581
)
25822582
series_of_dicts = Series([{"a": 1}, {"a": 2}, {"b": 3}], index=index)
25832583
frame = DataFrame.from_records(series_of_dicts, index=index)
@@ -2628,7 +2628,7 @@ class List(list):
26282628
[
26292629
Categorical(list("aabbc")),
26302630
SparseArray([1, np.nan, np.nan, np.nan]),
2631-
IntervalArray([pd.Interval(0, 1), pd.Interval(1, 5)]),
2631+
IntervalArray([Interval(0, 1), Interval(1, 5)]),
26322632
PeriodArray(pd.period_range(start="1/1/2017", end="1/1/2018", freq="M")),
26332633
],
26342634
)
@@ -2648,12 +2648,10 @@ def test_datetime_date_tuple_columns_from_dict(self):
26482648

26492649
def test_construct_with_two_categoricalindex_series(self):
26502650
# GH 14600
2651-
s1 = Series(
2652-
[39, 6, 4], index=pd.CategoricalIndex(["female", "male", "unknown"])
2653-
)
2651+
s1 = Series([39, 6, 4], index=CategoricalIndex(["female", "male", "unknown"]))
26542652
s2 = Series(
26552653
[2, 152, 2, 242, 150],
2656-
index=pd.CategoricalIndex(["f", "female", "m", "male", "unknown"]),
2654+
index=CategoricalIndex(["f", "female", "m", "male", "unknown"]),
26572655
)
26582656
result = DataFrame([s1, s2])
26592657
expected = DataFrame(
@@ -2717,7 +2715,7 @@ def test_dataframe_constructor_infer_multiindex(self):
27172715
(["1", "2"]),
27182716
(list(date_range("1/1/2011", periods=2, freq="H"))),
27192717
(list(date_range("1/1/2011", periods=2, freq="H", tz="US/Eastern"))),
2720-
([pd.Interval(left=0, right=5)]),
2718+
([Interval(left=0, right=5)]),
27212719
],
27222720
)
27232721
def test_constructor_list_str(self, input_vals, string_dtype):

pandas/tests/indexes/interval/test_interval.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def test_is_unique_interval(self, closed):
228228
assert idx.is_unique is True
229229

230230
# unique overlapping - shared endpoints
231-
idx = pd.IntervalIndex.from_tuples([(1, 2), (1, 3), (2, 3)], closed=closed)
231+
idx = IntervalIndex.from_tuples([(1, 2), (1, 3), (2, 3)], closed=closed)
232232
assert idx.is_unique is True
233233

234234
# unique nested
@@ -279,14 +279,14 @@ def test_monotonic(self, closed):
279279
assert idx._is_strictly_monotonic_decreasing is False
280280

281281
# increasing overlapping shared endpoints
282-
idx = pd.IntervalIndex.from_tuples([(1, 2), (1, 3), (2, 3)], closed=closed)
282+
idx = IntervalIndex.from_tuples([(1, 2), (1, 3), (2, 3)], closed=closed)
283283
assert idx.is_monotonic is True
284284
assert idx._is_strictly_monotonic_increasing is True
285285
assert idx.is_monotonic_decreasing is False
286286
assert idx._is_strictly_monotonic_decreasing is False
287287

288288
# decreasing overlapping shared endpoints
289-
idx = pd.IntervalIndex.from_tuples([(2, 3), (1, 3), (1, 2)], closed=closed)
289+
idx = IntervalIndex.from_tuples([(2, 3), (1, 3), (1, 2)], closed=closed)
290290
assert idx.is_monotonic is False
291291
assert idx._is_strictly_monotonic_increasing is False
292292
assert idx.is_monotonic_decreasing is True
@@ -872,7 +872,7 @@ def test_is_all_dates(self):
872872
year_2017 = Interval(
873873
Timestamp("2017-01-01 00:00:00"), Timestamp("2018-01-01 00:00:00")
874874
)
875-
year_2017_index = pd.IntervalIndex([year_2017])
875+
year_2017_index = IntervalIndex([year_2017])
876876
assert not year_2017_index._is_all_dates
877877

878878
@pytest.mark.parametrize("key", [[5], (2, 3)])

pandas/tests/indexes/test_numeric.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ def test_constructor_coercion_signed_to_unsigned(self, uint_dtype):
522522

523523
def test_constructor_unwraps_index(self):
524524
idx = Index([1, 2])
525-
result = pd.Int64Index(idx)
525+
result = Int64Index(idx)
526526
expected = np.array([1, 2], dtype="int64")
527527
tm.assert_numpy_array_equal(result._data, expected)
528528

@@ -614,8 +614,8 @@ def test_int_float_union_dtype(dtype):
614614
# https://github.com/pandas-dev/pandas/issues/26778
615615
# [u]int | float -> float
616616
index = Index([0, 2, 3], dtype=dtype)
617-
other = pd.Float64Index([0.5, 1.5])
618-
expected = pd.Float64Index([0.0, 0.5, 1.5, 2.0, 3.0])
617+
other = Float64Index([0.5, 1.5])
618+
expected = Float64Index([0.0, 0.5, 1.5, 2.0, 3.0])
619619
result = index.union(other)
620620
tm.assert_index_equal(result, expected)
621621

@@ -626,9 +626,9 @@ def test_int_float_union_dtype(dtype):
626626
def test_range_float_union_dtype():
627627
# https://github.com/pandas-dev/pandas/issues/26778
628628
index = pd.RangeIndex(start=0, stop=3)
629-
other = pd.Float64Index([0.5, 1.5])
629+
other = Float64Index([0.5, 1.5])
630630
result = index.union(other)
631-
expected = pd.Float64Index([0.0, 0.5, 1, 1.5, 2.0])
631+
expected = Float64Index([0.0, 0.5, 1, 1.5, 2.0])
632632
tm.assert_index_equal(result, expected)
633633

634634
result = other.union(index)

pandas/tests/series/test_constructors.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ def test_construction_consistency(self):
10401040
"data_constructor", [list, np.array], ids=["list", "ndarray[object]"]
10411041
)
10421042
def test_constructor_infer_period(self, data_constructor):
1043-
data = [pd.Period("2000", "D"), pd.Period("2001", "D"), None]
1043+
data = [Period("2000", "D"), Period("2001", "D"), None]
10441044
result = Series(data_constructor(data))
10451045
expected = Series(period_array(data))
10461046
tm.assert_series_equal(result, expected)
@@ -1057,7 +1057,7 @@ def test_construct_from_ints_including_iNaT_scalar_period_dtype(self):
10571057
assert isna(series[2])
10581058

10591059
def test_constructor_period_incompatible_frequency(self):
1060-
data = [pd.Period("2000", "D"), pd.Period("2001", "A")]
1060+
data = [Period("2000", "D"), Period("2001", "A")]
10611061
result = Series(data)
10621062
assert result.dtype == object
10631063
assert result.tolist() == data
@@ -1539,7 +1539,7 @@ def test_constructor_list_of_periods_infers_period_dtype(self):
15391539
assert series.dtype == "Period[D]"
15401540

15411541
series = Series(
1542-
[pd.Period("2011-01-01", freq="D"), pd.Period("2011-02-01", freq="D")]
1542+
[Period("2011-01-01", freq="D"), Period("2011-02-01", freq="D")]
15431543
)
15441544
assert series.dtype == "Period[D]"
15451545

scripts/check_for_inconsistent_pandas_namespace.py

+22-27
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,18 @@
1616

1717
PATTERN = r"""
1818
(
19-
(?<!pd\.)(?<!\w) # check class_name start with pd. or character
20-
{class_name}\( # match DataFrame but not pd.DataFrame or tm.makeDataFrame
19+
(?<!pd\.)(?<!\w) # check class_name doesn't start with pd. or character
20+
([A-Z]\w+)\( # match DataFrame but not pd.DataFrame or tm.makeDataFrame
2121
.* # match anything
22-
pd\.{class_name}\( # only match e.g. pd.DataFrame
22+
pd\.\2\( # only match e.g. pd.DataFrame
2323
)|
2424
(
25-
pd\.{class_name}\( # only match e.g. pd.DataFrame
25+
pd\.([A-Z]\w+)\( # only match e.g. pd.DataFrame
2626
.* # match anything
27-
(?<!pd\.)(?<!\w) # check class_name start with pd. or character
28-
{class_name}\( # match DataFrame but not pd.DataFrame or tm.makeDataFrame
27+
(?<!pd\.)(?<!\w) # check class_name doesn't start with pd. or character
28+
\4\( # match DataFrame but not pd.DataFrame or tm.makeDataFrame
2929
)
3030
"""
31-
CLASS_NAMES = (
32-
"Series",
33-
"DataFrame",
34-
"Index",
35-
"MultiIndex",
36-
"Timestamp",
37-
"Timedelta",
38-
"TimedeltaIndex",
39-
"DatetimeIndex",
40-
"Categorical",
41-
)
4231
ERROR_MESSAGE = "Found both `pd.{class_name}` and `{class_name}` in {path}"
4332

4433

@@ -47,16 +36,22 @@ def main(argv: Optional[Sequence[str]] = None) -> None:
4736
parser.add_argument("paths", nargs="*", type=Path)
4837
args = parser.parse_args(argv)
4938

50-
for class_name in CLASS_NAMES:
51-
pattern = re.compile(
52-
PATTERN.format(class_name=class_name).encode(),
53-
flags=re.MULTILINE | re.DOTALL | re.VERBOSE,
54-
)
55-
for path in args.paths:
56-
contents = path.read_bytes()
57-
match = pattern.search(contents)
58-
assert match is None, ERROR_MESSAGE.format(
59-
class_name=class_name, path=str(path)
39+
pattern = re.compile(
40+
PATTERN.encode(),
41+
flags=re.MULTILINE | re.DOTALL | re.VERBOSE,
42+
)
43+
for path in args.paths:
44+
contents = path.read_bytes()
45+
match = pattern.search(contents)
46+
if match is None:
47+
continue
48+
if match.group(2) is not None:
49+
raise AssertionError(
50+
ERROR_MESSAGE.format(class_name=match.group(2).decode(), path=str(path))
51+
)
52+
if match.group(4) is not None:
53+
raise AssertionError(
54+
ERROR_MESSAGE.format(class_name=match.group(4).decode(), path=str(path))
6055
)
6156

6257

0 commit comments

Comments
 (0)