Skip to content

Commit f863469

Browse files
committed
TST: Use more explicit object names (pandas-dev#55033)
1 parent c804914 commit f863469

File tree

11 files changed

+92
-68
lines changed

11 files changed

+92
-68
lines changed

pandas/tests/frame/methods/test_reindex.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
isna,
2727
)
2828
import pandas._testing as tm
29-
from pandas.api.types import CategoricalDtype as CDT
29+
from pandas.api.types import CategoricalDtype
3030

3131

3232
class TestReindexSetIndex:
@@ -1082,7 +1082,9 @@ def test_reindex_with_categoricalindex(self):
10821082
{
10831083
"A": np.arange(3, dtype="int64"),
10841084
},
1085-
index=CategoricalIndex(list("abc"), dtype=CDT(list("cabe")), name="B"),
1085+
index=CategoricalIndex(
1086+
list("abc"), dtype=CategoricalDtype(list("cabe")), name="B"
1087+
),
10861088
)
10871089

10881090
# reindexing
@@ -1111,13 +1113,13 @@ def test_reindex_with_categoricalindex(self):
11111113

11121114
result = df.reindex(Categorical(["a", "e"], categories=cats))
11131115
expected = DataFrame(
1114-
{"A": [0, np.nan], "B": Series(list("ae")).astype(CDT(cats))}
1116+
{"A": [0, np.nan], "B": Series(list("ae")).astype(CategoricalDtype(cats))}
11151117
).set_index("B")
11161118
tm.assert_frame_equal(result, expected, check_index_type=True)
11171119

11181120
result = df.reindex(Categorical(["a"], categories=cats))
11191121
expected = DataFrame(
1120-
{"A": [0], "B": Series(list("a")).astype(CDT(cats))}
1122+
{"A": [0], "B": Series(list("a")).astype(CategoricalDtype(cats))}
11211123
).set_index("B")
11221124
tm.assert_frame_equal(result, expected, check_index_type=True)
11231125

@@ -1138,21 +1140,29 @@ def test_reindex_with_categoricalindex(self):
11381140
# give back the type of categorical that we received
11391141
result = df.reindex(Categorical(["a", "e"], categories=cats, ordered=True))
11401142
expected = DataFrame(
1141-
{"A": [0, np.nan], "B": Series(list("ae")).astype(CDT(cats, ordered=True))}
1143+
{
1144+
"A": [0, np.nan],
1145+
"B": Series(list("ae")).astype(CategoricalDtype(cats, ordered=True)),
1146+
}
11421147
).set_index("B")
11431148
tm.assert_frame_equal(result, expected, check_index_type=True)
11441149

11451150
result = df.reindex(Categorical(["a", "d"], categories=["a", "d"]))
11461151
expected = DataFrame(
1147-
{"A": [0, np.nan], "B": Series(list("ad")).astype(CDT(["a", "d"]))}
1152+
{
1153+
"A": [0, np.nan],
1154+
"B": Series(list("ad")).astype(CategoricalDtype(["a", "d"])),
1155+
}
11481156
).set_index("B")
11491157
tm.assert_frame_equal(result, expected, check_index_type=True)
11501158

11511159
df2 = DataFrame(
11521160
{
11531161
"A": np.arange(6, dtype="int64"),
11541162
},
1155-
index=CategoricalIndex(list("aabbca"), dtype=CDT(list("cabe")), name="B"),
1163+
index=CategoricalIndex(
1164+
list("aabbca"), dtype=CategoricalDtype(list("cabe")), name="B"
1165+
),
11561166
)
11571167
# passed duplicate indexers are not allowed
11581168
msg = "cannot reindex on an axis with duplicate labels"

pandas/tests/indexes/ranges/test_range.py

+29-26
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
)
1111
import pandas._testing as tm
1212

13-
# aliases to make some tests easier to read
14-
RI = RangeIndex
15-
1613

1714
class TestRangeIndex:
1815
@pytest.fixture
@@ -507,25 +504,31 @@ def test_len_specialised(self, step):
507504
@pytest.mark.parametrize(
508505
"indices, expected",
509506
[
510-
([RI(1, 12, 5)], RI(1, 12, 5)),
511-
([RI(0, 6, 4)], RI(0, 6, 4)),
512-
([RI(1, 3), RI(3, 7)], RI(1, 7)),
513-
([RI(1, 5, 2), RI(5, 6)], RI(1, 6, 2)),
514-
([RI(1, 3, 2), RI(4, 7, 3)], RI(1, 7, 3)),
515-
([RI(-4, 3, 2), RI(4, 7, 2)], RI(-4, 7, 2)),
516-
([RI(-4, -8), RI(-8, -12)], RI(0, 0)),
517-
([RI(-4, -8), RI(3, -4)], RI(0, 0)),
518-
([RI(-4, -8), RI(3, 5)], RI(3, 5)),
519-
([RI(-4, -2), RI(3, 5)], Index([-4, -3, 3, 4])),
520-
([RI(-2), RI(3, 5)], RI(3, 5)),
521-
([RI(2), RI(2)], Index([0, 1, 0, 1])),
522-
([RI(2), RI(2, 5), RI(5, 8, 4)], RI(0, 6)),
523-
([RI(2), RI(3, 5), RI(5, 8, 4)], Index([0, 1, 3, 4, 5])),
524-
([RI(-2, 2), RI(2, 5), RI(5, 8, 4)], RI(-2, 6)),
525-
([RI(3), Index([-1, 3, 15])], Index([0, 1, 2, -1, 3, 15])),
526-
([RI(3), Index([-1, 3.1, 15.0])], Index([0, 1, 2, -1, 3.1, 15.0])),
527-
([RI(3), Index(["a", None, 14])], Index([0, 1, 2, "a", None, 14])),
528-
([RI(3, 1), Index(["a", None, 14])], Index(["a", None, 14])),
507+
([RangeIndex(1, 12, 5)], RangeIndex(1, 12, 5)),
508+
([RangeIndex(0, 6, 4)], RangeIndex(0, 6, 4)),
509+
([RangeIndex(1, 3), RangeIndex(3, 7)], RangeIndex(1, 7)),
510+
([RangeIndex(1, 5, 2), RangeIndex(5, 6)], RangeIndex(1, 6, 2)),
511+
([RangeIndex(1, 3, 2), RangeIndex(4, 7, 3)], RangeIndex(1, 7, 3)),
512+
([RangeIndex(-4, 3, 2), RangeIndex(4, 7, 2)], RangeIndex(-4, 7, 2)),
513+
([RangeIndex(-4, -8), RangeIndex(-8, -12)], RangeIndex(0, 0)),
514+
([RangeIndex(-4, -8), RangeIndex(3, -4)], RangeIndex(0, 0)),
515+
([RangeIndex(-4, -8), RangeIndex(3, 5)], RangeIndex(3, 5)),
516+
([RangeIndex(-4, -2), RangeIndex(3, 5)], Index([-4, -3, 3, 4])),
517+
([RangeIndex(-2), RangeIndex(3, 5)], RangeIndex(3, 5)),
518+
([RangeIndex(2), RangeIndex(2)], Index([0, 1, 0, 1])),
519+
([RangeIndex(2), RangeIndex(2, 5), RangeIndex(5, 8, 4)], RangeIndex(0, 6)),
520+
(
521+
[RangeIndex(2), RangeIndex(3, 5), RangeIndex(5, 8, 4)],
522+
Index([0, 1, 3, 4, 5]),
523+
),
524+
(
525+
[RangeIndex(-2, 2), RangeIndex(2, 5), RangeIndex(5, 8, 4)],
526+
RangeIndex(-2, 6),
527+
),
528+
([RangeIndex(3), Index([-1, 3, 15])], Index([0, 1, 2, -1, 3, 15])),
529+
([RangeIndex(3), Index([-1, 3.1, 15.0])], Index([0, 1, 2, -1, 3.1, 15.0])),
530+
([RangeIndex(3), Index(["a", None, 14])], Index([0, 1, 2, "a", None, 14])),
531+
([RangeIndex(3, 1), Index(["a", None, 14])], Index(["a", None, 14])),
529532
],
530533
)
531534
def test_append(self, indices, expected):
@@ -567,7 +570,7 @@ def test_format_empty(self):
567570
assert empty_idx.format(name=True) == [""]
568571

569572
@pytest.mark.parametrize(
570-
"RI",
573+
"ri",
571574
[
572575
RangeIndex(0, -1, -1),
573576
RangeIndex(0, 1, 1),
@@ -576,10 +579,10 @@ def test_format_empty(self):
576579
RangeIndex(-3, -5, -2),
577580
],
578581
)
579-
def test_append_len_one(self, RI):
582+
def test_append_len_one(self, ri):
580583
# GH39401
581-
result = RI.append([])
582-
tm.assert_index_equal(result, RI, exact=True)
584+
result = ri.append([])
585+
tm.assert_index_equal(result, ri, exact=True)
583586

584587
@pytest.mark.parametrize("base", [RangeIndex(0, 2), Index([0, 1])])
585588
def test_isin_range(self, base):

pandas/tests/indexing/test_categorical.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
Timestamp,
1717
)
1818
import pandas._testing as tm
19-
from pandas.api.types import CategoricalDtype as CDT
2019

2120

2221
@pytest.fixture
@@ -25,7 +24,9 @@ def df():
2524
{
2625
"A": np.arange(6, dtype="int64"),
2726
},
28-
index=CategoricalIndex(list("aabbca"), dtype=CDT(list("cab")), name="B"),
27+
index=CategoricalIndex(
28+
list("aabbca"), dtype=CategoricalDtype(list("cab")), name="B"
29+
),
2930
)
3031

3132

@@ -35,13 +36,15 @@ def df2():
3536
{
3637
"A": np.arange(6, dtype="int64"),
3738
},
38-
index=CategoricalIndex(list("aabbca"), dtype=CDT(list("cabe")), name="B"),
39+
index=CategoricalIndex(
40+
list("aabbca"), dtype=CategoricalDtype(list("cabe")), name="B"
41+
),
3942
)
4043

4144

4245
class TestCategoricalIndex:
4346
def test_loc_scalar(self, df):
44-
dtype = CDT(list("cab"))
47+
dtype = CategoricalDtype(list("cab"))
4548
result = df.loc["a"]
4649
bidx = Series(list("aaa"), name="B").astype(dtype)
4750
assert bidx.dtype == dtype

pandas/tests/indexing/test_chaining_and_caching.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from string import ascii_letters as letters
1+
from string import ascii_letters
22

33
import numpy as np
44
import pytest
@@ -24,9 +24,9 @@
2424

2525
def random_text(nobs=100):
2626
# Construct a DataFrame where each row is a random slice from 'letters'
27-
idxs = np.random.default_rng(2).integers(len(letters), size=(nobs, 2))
27+
idxs = np.random.default_rng(2).integers(len(ascii_letters), size=(nobs, 2))
2828
idxs.sort(axis=1)
29-
strings = [letters[x[0] : x[1]] for x in idxs]
29+
strings = [ascii_letters[x[0] : x[1]] for x in idxs]
3030

3131
return DataFrame(strings, columns=["letters"])
3232

pandas/tests/io/formats/test_info.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from io import StringIO
22
import re
3-
from string import ascii_uppercase as uppercase
3+
from string import ascii_uppercase
44
import sys
55
import textwrap
66

@@ -452,9 +452,9 @@ def memory_usage(f):
452452
return f.memory_usage(deep=True).sum()
453453

454454
N = 100
455-
M = len(uppercase)
455+
M = len(ascii_uppercase)
456456
index = MultiIndex.from_product(
457-
[list(uppercase), date_range("20160101", periods=N)],
457+
[list(ascii_uppercase), date_range("20160101", periods=N)],
458458
names=["id", "date"],
459459
)
460460
df = DataFrame(

pandas/tests/io/formats/test_series_info.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from io import StringIO
2-
from string import ascii_uppercase as uppercase
2+
from string import ascii_uppercase
33
import textwrap
44

55
import numpy as np
@@ -165,9 +165,9 @@ def test_info_memory_usage_bug_on_multiindex():
165165
# GH 14308
166166
# memory usage introspection should not materialize .values
167167
N = 100
168-
M = len(uppercase)
168+
M = len(ascii_uppercase)
169169
index = MultiIndex.from_product(
170-
[list(uppercase), date_range("20160101", periods=N)],
170+
[list(ascii_uppercase), date_range("20160101", periods=N)],
171171
names=["id", "date"],
172172
)
173173
s = Series(np.random.default_rng(2).standard_normal(N * M), index=index)

pandas/tests/reshape/merge/test_merge.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
TimedeltaIndex,
2727
)
2828
import pandas._testing as tm
29-
from pandas.api.types import CategoricalDtype as CDT
3029
from pandas.core.reshape.concat import concat
3130
from pandas.core.reshape.merge import (
3231
MergeError,
@@ -1842,7 +1841,7 @@ def left():
18421841
{
18431842
"X": Series(
18441843
np.random.default_rng(2).choice(["foo", "bar"], size=(10,))
1845-
).astype(CDT(["foo", "bar"])),
1844+
).astype(CategoricalDtype(["foo", "bar"])),
18461845
"Y": np.random.default_rng(2).choice(["one", "two", "three"], size=(10,)),
18471846
}
18481847
)
@@ -1851,7 +1850,10 @@ def left():
18511850
@pytest.fixture
18521851
def right():
18531852
return DataFrame(
1854-
{"X": Series(["foo", "bar"]).astype(CDT(["foo", "bar"])), "Z": [1, 2]}
1853+
{
1854+
"X": Series(["foo", "bar"]).astype(CategoricalDtype(["foo", "bar"])),
1855+
"Z": [1, 2],
1856+
}
18551857
)
18561858

18571859

@@ -2002,8 +2004,8 @@ def test_other_columns(self, left, right):
20022004
"change",
20032005
[
20042006
lambda x: x,
2005-
lambda x: x.astype(CDT(["foo", "bar", "bah"])),
2006-
lambda x: x.astype(CDT(ordered=True)),
2007+
lambda x: x.astype(CategoricalDtype(["foo", "bar", "bah"])),
2008+
lambda x: x.astype(CategoricalDtype(ordered=True)),
20072009
],
20082010
)
20092011
def test_dtype_on_merged_different(self, change, join_type, left, right):
@@ -2110,11 +2112,13 @@ def test_merging_with_bool_or_int_cateorical_column(
21102112
# GH 17187
21112113
# merging with a boolean/int categorical column
21122114
df1 = DataFrame({"id": [1, 2, 3, 4], "cat": category_column})
2113-
df1["cat"] = df1["cat"].astype(CDT(categories, ordered=ordered))
2115+
df1["cat"] = df1["cat"].astype(CategoricalDtype(categories, ordered=ordered))
21142116
df2 = DataFrame({"id": [2, 4], "num": [1, 9]})
21152117
result = df1.merge(df2)
21162118
expected = DataFrame({"id": [2, 4], "cat": expected_categories, "num": [1, 9]})
2117-
expected["cat"] = expected["cat"].astype(CDT(categories, ordered=ordered))
2119+
expected["cat"] = expected["cat"].astype(
2120+
CategoricalDtype(categories, ordered=ordered)
2121+
)
21182122
tm.assert_frame_equal(expected, result)
21192123

21202124
def test_merge_on_int_array(self):

pandas/tests/reshape/test_cut.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
to_datetime,
2222
)
2323
import pandas._testing as tm
24-
from pandas.api.types import CategoricalDtype as CDT
24+
from pandas.api.types import CategoricalDtype
2525
import pandas.core.reshape.tile as tmod
2626

2727

@@ -359,7 +359,7 @@ def test_cut_return_intervals():
359359
IntervalIndex.from_breaks(exp_bins, closed="right").take(
360360
[0, 0, 0, 1, 1, 1, 2, 2, 2]
361361
)
362-
).astype(CDT(ordered=True))
362+
).astype(CategoricalDtype(ordered=True))
363363
tm.assert_series_equal(result, expected)
364364

365365

@@ -370,7 +370,7 @@ def test_series_ret_bins():
370370

371371
expected = Series(
372372
IntervalIndex.from_breaks([-0.003, 1.5, 3], closed="right").repeat(2)
373-
).astype(CDT(ordered=True))
373+
).astype(CategoricalDtype(ordered=True))
374374
tm.assert_series_equal(result, expected)
375375

376376

@@ -445,7 +445,7 @@ def test_datetime_bin(conv):
445445
Interval(Timestamp(bin_data[1]), Timestamp(bin_data[2])),
446446
]
447447
)
448-
).astype(CDT(ordered=True))
448+
).astype(CategoricalDtype(ordered=True))
449449

450450
bins = [conv(v) for v in bin_data]
451451
result = Series(cut(data, bins=bins))
@@ -491,7 +491,7 @@ def test_datetime_cut(data):
491491
),
492492
]
493493
)
494-
).astype(CDT(ordered=True))
494+
).astype(CategoricalDtype(ordered=True))
495495
tm.assert_series_equal(Series(result), expected)
496496

497497

@@ -534,7 +534,7 @@ def test_datetime_tz_cut(bins, box):
534534
),
535535
]
536536
)
537-
).astype(CDT(ordered=True))
537+
).astype(CategoricalDtype(ordered=True))
538538
tm.assert_series_equal(result, expected)
539539

540540

pandas/tests/reshape/test_pivot.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
date_range,
2424
)
2525
import pandas._testing as tm
26-
from pandas.api.types import CategoricalDtype as CDT
26+
from pandas.api.types import CategoricalDtype
2727
from pandas.core.reshape import reshape as reshape_lib
2828
from pandas.core.reshape.pivot import pivot_table
2929

@@ -219,10 +219,12 @@ def test_pivot_table_dropna_categoricals(self, dropna):
219219
}
220220
)
221221

222-
df["A"] = df["A"].astype(CDT(categories, ordered=False))
222+
df["A"] = df["A"].astype(CategoricalDtype(categories, ordered=False))
223223
result = df.pivot_table(index="B", columns="A", values="C", dropna=dropna)
224224
expected_columns = Series(["a", "b", "c"], name="A")
225-
expected_columns = expected_columns.astype(CDT(categories, ordered=False))
225+
expected_columns = expected_columns.astype(
226+
CategoricalDtype(categories, ordered=False)
227+
)
226228
expected_index = Series([1, 2, 3], name="B")
227229
expected = DataFrame(
228230
[[0.0, 3.0, 6.0], [1.0, 4.0, 7.0], [2.0, 5.0, 8.0]],

0 commit comments

Comments
 (0)