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

+5-4
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

+13-12
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

+1-1
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

+1-1
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

+1-1
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

+1-1
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

+1-1
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

+5-5
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

+2-2
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

+1-1
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")

pandas/tests/reductions/test_reductions.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def test_ops(self, opname, obj):
5555
if not isinstance(obj, PeriodIndex):
5656
expected = getattr(obj.values, opname)()
5757
else:
58-
expected = pd.Period(ordinal=getattr(obj.asi8, opname)(), freq=obj.freq)
58+
expected = Period(ordinal=getattr(obj.asi8, opname)(), freq=obj.freq)
5959

6060
if getattr(obj, "tz", None) is not None:
6161
# We need to de-localize before comparing to the numpy-produced result
@@ -470,19 +470,19 @@ def test_numpy_minmax_datetime64(self):
470470
def test_minmax_period(self):
471471

472472
# monotonic
473-
idx1 = pd.PeriodIndex([NaT, "2011-01-01", "2011-01-02", "2011-01-03"], freq="D")
473+
idx1 = PeriodIndex([NaT, "2011-01-01", "2011-01-02", "2011-01-03"], freq="D")
474474
assert not idx1.is_monotonic
475475
assert idx1[1:].is_monotonic
476476

477477
# non-monotonic
478-
idx2 = pd.PeriodIndex(
478+
idx2 = PeriodIndex(
479479
["2011-01-01", NaT, "2011-01-03", "2011-01-02", NaT], freq="D"
480480
)
481481
assert not idx2.is_monotonic
482482

483483
for idx in [idx1, idx2]:
484-
assert idx.min() == pd.Period("2011-01-01", freq="D")
485-
assert idx.max() == pd.Period("2011-01-03", freq="D")
484+
assert idx.min() == Period("2011-01-01", freq="D")
485+
assert idx.max() == Period("2011-01-03", freq="D")
486486
assert idx1.argmin() == 1
487487
assert idx2.argmin() == 0
488488
assert idx1.argmax() == 3

pandas/tests/resample/test_datetime_index.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -1285,7 +1285,7 @@ def test_resample_timegrouper():
12851285
expected.index = expected.index._with_freq(None)
12861286
tm.assert_frame_equal(result, expected)
12871287

1288-
result = df.groupby(pd.Grouper(freq="M", key="A")).count()
1288+
result = df.groupby(Grouper(freq="M", key="A")).count()
12891289
tm.assert_frame_equal(result, expected)
12901290

12911291
df = DataFrame(dict(A=dates, B=np.arange(len(dates)), C=np.arange(len(dates))))
@@ -1299,7 +1299,7 @@ def test_resample_timegrouper():
12991299
expected.index = expected.index._with_freq(None)
13001300
tm.assert_frame_equal(result, expected)
13011301

1302-
result = df.groupby(pd.Grouper(freq="M", key="A")).count()
1302+
result = df.groupby(Grouper(freq="M", key="A")).count()
13031303
tm.assert_frame_equal(result, expected)
13041304

13051305

@@ -1319,8 +1319,8 @@ def test_resample_nunique():
13191319
}
13201320
)
13211321
r = df.resample("D")
1322-
g = df.groupby(pd.Grouper(freq="D"))
1323-
expected = df.groupby(pd.Grouper(freq="D")).ID.apply(lambda x: x.nunique())
1322+
g = df.groupby(Grouper(freq="D"))
1323+
expected = df.groupby(Grouper(freq="D")).ID.apply(lambda x: x.nunique())
13241324
assert expected.name == "ID"
13251325

13261326
for t in [r, g]:
@@ -1330,7 +1330,7 @@ def test_resample_nunique():
13301330
result = df.ID.resample("D").nunique()
13311331
tm.assert_series_equal(result, expected)
13321332

1333-
result = df.ID.groupby(pd.Grouper(freq="D")).nunique()
1333+
result = df.ID.groupby(Grouper(freq="D")).nunique()
13341334
tm.assert_series_equal(result, expected)
13351335

13361336

@@ -1443,7 +1443,7 @@ def test_groupby_with_dst_time_change():
14431443
).tz_convert("America/Chicago")
14441444

14451445
df = DataFrame([1, 2], index=index)
1446-
result = df.groupby(pd.Grouper(freq="1d")).last()
1446+
result = df.groupby(Grouper(freq="1d")).last()
14471447
expected_index_values = pd.date_range(
14481448
"2016-11-02", "2016-11-24", freq="d", tz="America/Chicago"
14491449
)
@@ -1587,7 +1587,7 @@ def test_downsample_dst_at_midnight():
15871587
index = index.tz_localize("UTC").tz_convert("America/Havana")
15881588
data = list(range(len(index)))
15891589
dataframe = DataFrame(data, index=index)
1590-
result = dataframe.groupby(pd.Grouper(freq="1D")).mean()
1590+
result = dataframe.groupby(Grouper(freq="1D")).mean()
15911591

15921592
dti = date_range("2018-11-03", periods=3).tz_localize(
15931593
"America/Havana", ambiguous=True
@@ -1709,9 +1709,9 @@ def test_resample_equivalent_offsets(n1, freq1, n2, freq2, k):
17091709
],
17101710
)
17111711
def test_get_timestamp_range_edges(first, last, freq, exp_first, exp_last):
1712-
first = pd.Period(first)
1712+
first = Period(first)
17131713
first = first.to_timestamp(first.freq)
1714-
last = pd.Period(last)
1714+
last = Period(last)
17151715
last = last.to_timestamp(last.freq)
17161716

17171717
exp_first = Timestamp(exp_first, freq=freq)

pandas/tests/resample/test_period_index.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -845,11 +845,11 @@ def test_resample_with_offset(self, start, end, start_freq, end_freq, offset):
845845
],
846846
)
847847
def test_get_period_range_edges(self, first, last, freq, exp_first, exp_last):
848-
first = pd.Period(first)
849-
last = pd.Period(last)
848+
first = Period(first)
849+
last = Period(last)
850850

851-
exp_first = pd.Period(exp_first, freq=freq)
852-
exp_last = pd.Period(exp_last, freq=freq)
851+
exp_first = Period(exp_first, freq=freq)
852+
exp_last = Period(exp_last, freq=freq)
853853

854854
freq = pd.tseries.frequencies.to_offset(freq)
855855
result = _get_period_range_edges(first, last, freq)

pandas/tests/reshape/test_pivot.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -411,16 +411,14 @@ def test_pivot_no_values(self):
411411
},
412412
index=idx,
413413
)
414-
res = df.pivot_table(
415-
index=df.index.month, columns=pd.Grouper(key="dt", freq="M")
416-
)
414+
res = df.pivot_table(index=df.index.month, columns=Grouper(key="dt", freq="M"))
417415
exp_columns = MultiIndex.from_tuples([("A", pd.Timestamp("2011-01-31"))])
418416
exp_columns.names = [None, "dt"]
419417
exp = DataFrame([3.25, 2.0], index=[1, 2], columns=exp_columns)
420418
tm.assert_frame_equal(res, exp)
421419

422420
res = df.pivot_table(
423-
index=pd.Grouper(freq="A"), columns=pd.Grouper(key="dt", freq="M")
421+
index=Grouper(freq="A"), columns=Grouper(key="dt", freq="M")
424422
)
425423
exp = DataFrame(
426424
[3], index=pd.DatetimeIndex(["2011-12-31"], freq="A"), columns=exp_columns

0 commit comments

Comments
 (0)