Skip to content

Commit 9b3cc20

Browse files
authored
Fix cases of inconsistent namespacing in tests (#37838)
* Fix cases of inconsistent namespacing in tests Sometimes e.g. Series and pd.Series are used in the same test file. This fixes some of these cases, generally by using the explicitly imported class. * Formatting with black * Ran pre-commit and reverted docstring
1 parent 50ae0bf commit 9b3cc20

File tree

14 files changed

+51
-51
lines changed

14 files changed

+51
-51
lines changed

pandas/tests/arithmetic/conftest.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pytest
33

44
import pandas as pd
5+
from pandas import Float64Index, Int64Index, RangeIndex, UInt64Index
56
import pandas._testing as tm
67

78
# ------------------------------------------------------------------
@@ -93,10 +94,10 @@ def zero(request):
9394

9495
@pytest.fixture(
9596
params=[
96-
pd.Float64Index(np.arange(5, dtype="float64")),
97-
pd.Int64Index(np.arange(5, dtype="int64")),
98-
pd.UInt64Index(np.arange(5, dtype="uint64")),
99-
pd.RangeIndex(5),
97+
Float64Index(np.arange(5, dtype="float64")),
98+
Int64Index(np.arange(5, dtype="int64")),
99+
UInt64Index(np.arange(5, dtype="uint64")),
100+
RangeIndex(5),
100101
],
101102
ids=lambda x: type(x).__name__,
102103
)

pandas/tests/arithmetic/test_datetime64.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import pandas as pd
1919
from pandas import (
20+
DateOffset,
2021
DatetimeIndex,
2122
NaT,
2223
Period,
@@ -166,8 +167,8 @@ class TestDatetime64SeriesComparison:
166167
[NaT, NaT, Timedelta("3 days")],
167168
),
168169
(
169-
[pd.Period("2011-01", freq="M"), NaT, pd.Period("2011-03", freq="M")],
170-
[NaT, NaT, pd.Period("2011-03", freq="M")],
170+
[Period("2011-01", freq="M"), NaT, Period("2011-03", freq="M")],
171+
[NaT, NaT, Period("2011-03", freq="M")],
171172
),
172173
],
173174
)
@@ -1078,7 +1079,7 @@ def test_dt64arr_add_timestamp_raises(self, box_with_array):
10781079
3.14,
10791080
np.array([2.0, 3.0]),
10801081
# GH#13078 datetime +/- Period is invalid
1081-
pd.Period("2011-01-01", freq="D"),
1082+
Period("2011-01-01", freq="D"),
10821083
# https://github.com/pandas-dev/pandas/issues/10329
10831084
time(1, 2, 3),
10841085
],
@@ -1288,7 +1289,7 @@ def test_dt64arr_add_sub_relativedelta_offsets(self, box_with_array):
12881289
("microseconds", 5),
12891290
]
12901291
for i, kwd in enumerate(relative_kwargs):
1291-
off = pd.DateOffset(**dict([kwd]))
1292+
off = DateOffset(**dict([kwd]))
12921293

12931294
expected = DatetimeIndex([x + off for x in vec_items])
12941295
expected = tm.box_expected(expected, box_with_array)
@@ -1298,7 +1299,7 @@ def test_dt64arr_add_sub_relativedelta_offsets(self, box_with_array):
12981299
expected = tm.box_expected(expected, box_with_array)
12991300
tm.assert_equal(expected, vec - off)
13001301

1301-
off = pd.DateOffset(**dict(relative_kwargs[: i + 1]))
1302+
off = DateOffset(**dict(relative_kwargs[: i + 1]))
13021303

13031304
expected = DatetimeIndex([x + off for x in vec_items])
13041305
expected = tm.box_expected(expected, box_with_array)
@@ -1431,14 +1432,14 @@ def test_dt64arr_add_sub_DateOffset(self, box_with_array):
14311432
# GH#10699
14321433
s = date_range("2000-01-01", "2000-01-31", name="a")
14331434
s = tm.box_expected(s, box_with_array)
1434-
result = s + pd.DateOffset(years=1)
1435-
result2 = pd.DateOffset(years=1) + s
1435+
result = s + DateOffset(years=1)
1436+
result2 = DateOffset(years=1) + s
14361437
exp = date_range("2001-01-01", "2001-01-31", name="a")._with_freq(None)
14371438
exp = tm.box_expected(exp, box_with_array)
14381439
tm.assert_equal(result, exp)
14391440
tm.assert_equal(result2, exp)
14401441

1441-
result = s - pd.DateOffset(years=1)
1442+
result = s - DateOffset(years=1)
14421443
exp = date_range("1999-01-01", "1999-01-31", name="a")._with_freq(None)
14431444
exp = tm.box_expected(exp, box_with_array)
14441445
tm.assert_equal(result, exp)
@@ -1527,7 +1528,7 @@ def test_dt64arr_add_sub_offset_array(
15271528
[
15281529
(
15291530
"__add__",
1530-
pd.DateOffset(months=3, days=10),
1531+
DateOffset(months=3, days=10),
15311532
[
15321533
Timestamp("2014-04-11"),
15331534
Timestamp("2015-04-11"),
@@ -1538,7 +1539,7 @@ def test_dt64arr_add_sub_offset_array(
15381539
),
15391540
(
15401541
"__add__",
1541-
pd.DateOffset(months=3),
1542+
DateOffset(months=3),
15421543
[
15431544
Timestamp("2014-04-01"),
15441545
Timestamp("2015-04-01"),
@@ -1549,7 +1550,7 @@ def test_dt64arr_add_sub_offset_array(
15491550
),
15501551
(
15511552
"__sub__",
1552-
pd.DateOffset(months=3, days=10),
1553+
DateOffset(months=3, days=10),
15531554
[
15541555
Timestamp("2013-09-21"),
15551556
Timestamp("2014-09-21"),
@@ -1560,7 +1561,7 @@ def test_dt64arr_add_sub_offset_array(
15601561
),
15611562
(
15621563
"__sub__",
1563-
pd.DateOffset(months=3),
1564+
DateOffset(months=3),
15641565
[
15651566
Timestamp("2013-10-01"),
15661567
Timestamp("2014-10-01"),

pandas/tests/arrays/integer/test_construction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010

1111
def test_uses_pandas_na():
12-
a = pd.array([1, None], dtype=pd.Int64Dtype())
12+
a = pd.array([1, None], dtype=Int64Dtype())
1313
assert a[1] is pd.NA
1414

1515

pandas/tests/arrays/sparse/test_array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def test_take(self):
274274
tm.assert_sp_array_equal(self.arr.take([0, 1, 2]), exp)
275275

276276
def test_take_all_empty(self):
277-
a = pd.array([0, 0], dtype=pd.SparseDtype("int64"))
277+
a = pd.array([0, 0], dtype=SparseDtype("int64"))
278278
result = a.take([0, 1], allow_fill=True, fill_value=np.nan)
279279
tm.assert_sp_array_equal(a, result)
280280

pandas/tests/extension/test_sparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ def test_astype_object_frame(self, all_data):
355355

356356
def test_astype_str(self, data):
357357
result = pd.Series(data[:5]).astype(str)
358-
expected_dtype = pd.SparseDtype(str, str(data.fill_value))
358+
expected_dtype = SparseDtype(str, str(data.fill_value))
359359
expected = pd.Series([str(x) for x in data[:5]], dtype=expected_dtype)
360360
self.assert_series_equal(result, expected)
361361

pandas/tests/frame/test_constructors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2456,7 +2456,7 @@ def test_from_records_sequencelike(self):
24562456

24572457
# tuples is in the order of the columns
24582458
result = DataFrame.from_records(tuples)
2459-
tm.assert_index_equal(result.columns, pd.RangeIndex(8))
2459+
tm.assert_index_equal(result.columns, RangeIndex(8))
24602460

24612461
# test exclude parameter & we are casting the results here (as we don't
24622462
# have dtype info to recover)

pandas/tests/indexes/datetimes/test_constructors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_shallow_copy_inherits_array_freq(self, index):
5353
def test_categorical_preserves_tz(self):
5454
# GH#18664 retain tz when going DTI-->Categorical-->DTI
5555
# TODO: parametrize over DatetimeIndex/DatetimeArray
56-
# once CategoricalIndex(DTA) works
56+
# once pd.CategoricalIndex(DTA) works
5757

5858
dti = DatetimeIndex(
5959
[pd.NaT, "2015-01-01", "1999-04-06 15:14:13", "2015-01-01"], tz="US/Eastern"

pandas/tests/indexes/test_numeric.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pandas._libs.tslibs import Timestamp
77

88
import pandas as pd
9-
from pandas import Float64Index, Index, Int64Index, Series, UInt64Index
9+
from pandas import Float64Index, Index, Int64Index, RangeIndex, Series, UInt64Index
1010
import pandas._testing as tm
1111
from pandas.tests.indexes.common import Base
1212

@@ -171,10 +171,10 @@ def test_constructor(self):
171171
@pytest.mark.parametrize(
172172
"index, dtype",
173173
[
174-
(pd.Int64Index, "float64"),
175-
(pd.UInt64Index, "categorical"),
176-
(pd.Float64Index, "datetime64"),
177-
(pd.RangeIndex, "float64"),
174+
(Int64Index, "float64"),
175+
(UInt64Index, "categorical"),
176+
(Float64Index, "datetime64"),
177+
(RangeIndex, "float64"),
178178
],
179179
)
180180
def test_invalid_dtype(self, index, dtype):

pandas/tests/indexing/test_categorical.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,11 +445,11 @@ def test_loc_slice(self):
445445

446446
def test_loc_and_at_with_categorical_index(self):
447447
# GH 20629
448-
s = Series([1, 2, 3], index=pd.CategoricalIndex(["A", "B", "C"]))
448+
s = Series([1, 2, 3], index=CategoricalIndex(["A", "B", "C"]))
449449
assert s.loc["A"] == 1
450450
assert s.at["A"] == 1
451451
df = DataFrame(
452-
[[1, 2], [3, 4], [5, 6]], index=pd.CategoricalIndex(["A", "B", "C"])
452+
[[1, 2], [3, 4], [5, 6]], index=CategoricalIndex(["A", "B", "C"])
453453
)
454454
assert df.loc["B", 1] == 4
455455
assert df.at["B", 1] == 4

pandas/tests/io/excel/test_xlrd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ def test_read_xlrd_book(read_ext, frame):
3838
# TODO: test for openpyxl as well
3939
def test_excel_table_sheet_by_index(datapath, read_ext):
4040
path = datapath("io", "data", "excel", f"test1{read_ext}")
41-
with pd.ExcelFile(path) as excel:
41+
with ExcelFile(path) as excel:
4242
with pytest.raises(xlrd.XLRDError):
4343
pd.read_excel(excel, sheet_name="asdf")

0 commit comments

Comments
 (0)