Skip to content

Commit c77fc35

Browse files
making namespace usage more consistent (#37852)
* making namespace usage more consistent * Adding more index classes to test_numeric
1 parent 613f098 commit c77fc35

File tree

8 files changed

+125
-110
lines changed

8 files changed

+125
-110
lines changed

pandas/tests/arithmetic/test_numeric.py

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,17 @@
1111
import pytest
1212

1313
import pandas as pd
14-
from pandas import Index, Int64Index, Series, Timedelta, TimedeltaIndex, array
14+
from pandas import (
15+
Float64Index,
16+
Index,
17+
Int64Index,
18+
RangeIndex,
19+
Series,
20+
Timedelta,
21+
TimedeltaIndex,
22+
UInt64Index,
23+
array,
24+
)
1525
import pandas._testing as tm
1626
from pandas.core import ops
1727

@@ -43,7 +53,7 @@ def adjust_negative_zero(zero, expected):
4353
# List comprehension has incompatible type List[PandasObject]; expected List[RangeIndex]
4454
# See GH#29725
4555
ser_or_index: List[Any] = [Series, Index]
46-
lefts: List[Any] = [pd.RangeIndex(10, 40, 10)]
56+
lefts: List[Any] = [RangeIndex(10, 40, 10)]
4757
lefts.extend(
4858
[
4959
cls([10, 20, 30], dtype=dtype)
@@ -364,7 +374,7 @@ def test_divmod_zero(self, zero, numeric_idx):
364374
@pytest.mark.parametrize("op", [operator.truediv, operator.floordiv])
365375
def test_div_negative_zero(self, zero, numeric_idx, op):
366376
# Check that -1 / -0.0 returns np.inf, not -np.inf
367-
if isinstance(numeric_idx, pd.UInt64Index):
377+
if isinstance(numeric_idx, UInt64Index):
368378
return
369379
idx = numeric_idx - 3
370380

@@ -634,15 +644,15 @@ def test_mul_int_array(self, numeric_idx):
634644
result = idx * np.array(5, dtype="int64")
635645
tm.assert_index_equal(result, idx * 5)
636646

637-
arr_dtype = "uint64" if isinstance(idx, pd.UInt64Index) else "int64"
647+
arr_dtype = "uint64" if isinstance(idx, UInt64Index) else "int64"
638648
result = idx * np.arange(5, dtype=arr_dtype)
639649
tm.assert_index_equal(result, didx)
640650

641651
def test_mul_int_series(self, numeric_idx):
642652
idx = numeric_idx
643653
didx = idx * idx
644654

645-
arr_dtype = "uint64" if isinstance(idx, pd.UInt64Index) else "int64"
655+
arr_dtype = "uint64" if isinstance(idx, UInt64Index) else "int64"
646656
result = idx * Series(np.arange(5, dtype=arr_dtype))
647657
tm.assert_series_equal(result, Series(didx))
648658

@@ -657,7 +667,7 @@ def test_mul_float_series(self, numeric_idx):
657667
def test_mul_index(self, numeric_idx):
658668
# in general not true for RangeIndex
659669
idx = numeric_idx
660-
if not isinstance(idx, pd.RangeIndex):
670+
if not isinstance(idx, RangeIndex):
661671
result = idx * idx
662672
tm.assert_index_equal(result, idx ** 2)
663673

@@ -680,7 +690,7 @@ def test_pow_float(self, op, numeric_idx, box_with_array):
680690
# test power calculations both ways, GH#14973
681691
box = box_with_array
682692
idx = numeric_idx
683-
expected = pd.Float64Index(op(idx.values, 2.0))
693+
expected = Float64Index(op(idx.values, 2.0))
684694

685695
idx = tm.box_expected(idx, box)
686696
expected = tm.box_expected(expected, box)
@@ -1040,74 +1050,70 @@ def test_series_divmod_zero(self):
10401050
class TestUFuncCompat:
10411051
@pytest.mark.parametrize(
10421052
"holder",
1043-
[pd.Int64Index, pd.UInt64Index, pd.Float64Index, pd.RangeIndex, Series],
1053+
[Int64Index, UInt64Index, Float64Index, RangeIndex, Series],
10441054
)
10451055
def test_ufunc_compat(self, holder):
10461056
box = Series if holder is Series else Index
10471057

1048-
if holder is pd.RangeIndex:
1049-
idx = pd.RangeIndex(0, 5)
1058+
if holder is RangeIndex:
1059+
idx = RangeIndex(0, 5)
10501060
else:
10511061
idx = holder(np.arange(5, dtype="int64"))
10521062
result = np.sin(idx)
10531063
expected = box(np.sin(np.arange(5, dtype="int64")))
10541064
tm.assert_equal(result, expected)
10551065

1056-
@pytest.mark.parametrize(
1057-
"holder", [pd.Int64Index, pd.UInt64Index, pd.Float64Index, Series]
1058-
)
1066+
@pytest.mark.parametrize("holder", [Int64Index, UInt64Index, Float64Index, Series])
10591067
def test_ufunc_coercions(self, holder):
10601068
idx = holder([1, 2, 3, 4, 5], name="x")
10611069
box = Series if holder is Series else Index
10621070

10631071
result = np.sqrt(idx)
10641072
assert result.dtype == "f8" and isinstance(result, box)
1065-
exp = pd.Float64Index(np.sqrt(np.array([1, 2, 3, 4, 5])), name="x")
1073+
exp = Float64Index(np.sqrt(np.array([1, 2, 3, 4, 5])), name="x")
10661074
exp = tm.box_expected(exp, box)
10671075
tm.assert_equal(result, exp)
10681076

10691077
result = np.divide(idx, 2.0)
10701078
assert result.dtype == "f8" and isinstance(result, box)
1071-
exp = pd.Float64Index([0.5, 1.0, 1.5, 2.0, 2.5], name="x")
1079+
exp = Float64Index([0.5, 1.0, 1.5, 2.0, 2.5], name="x")
10721080
exp = tm.box_expected(exp, box)
10731081
tm.assert_equal(result, exp)
10741082

10751083
# _evaluate_numeric_binop
10761084
result = idx + 2.0
10771085
assert result.dtype == "f8" and isinstance(result, box)
1078-
exp = pd.Float64Index([3.0, 4.0, 5.0, 6.0, 7.0], name="x")
1086+
exp = Float64Index([3.0, 4.0, 5.0, 6.0, 7.0], name="x")
10791087
exp = tm.box_expected(exp, box)
10801088
tm.assert_equal(result, exp)
10811089

10821090
result = idx - 2.0
10831091
assert result.dtype == "f8" and isinstance(result, box)
1084-
exp = pd.Float64Index([-1.0, 0.0, 1.0, 2.0, 3.0], name="x")
1092+
exp = Float64Index([-1.0, 0.0, 1.0, 2.0, 3.0], name="x")
10851093
exp = tm.box_expected(exp, box)
10861094
tm.assert_equal(result, exp)
10871095

10881096
result = idx * 1.0
10891097
assert result.dtype == "f8" and isinstance(result, box)
1090-
exp = pd.Float64Index([1.0, 2.0, 3.0, 4.0, 5.0], name="x")
1098+
exp = Float64Index([1.0, 2.0, 3.0, 4.0, 5.0], name="x")
10911099
exp = tm.box_expected(exp, box)
10921100
tm.assert_equal(result, exp)
10931101

10941102
result = idx / 2.0
10951103
assert result.dtype == "f8" and isinstance(result, box)
1096-
exp = pd.Float64Index([0.5, 1.0, 1.5, 2.0, 2.5], name="x")
1104+
exp = Float64Index([0.5, 1.0, 1.5, 2.0, 2.5], name="x")
10971105
exp = tm.box_expected(exp, box)
10981106
tm.assert_equal(result, exp)
10991107

1100-
@pytest.mark.parametrize(
1101-
"holder", [pd.Int64Index, pd.UInt64Index, pd.Float64Index, Series]
1102-
)
1108+
@pytest.mark.parametrize("holder", [Int64Index, UInt64Index, Float64Index, Series])
11031109
def test_ufunc_multiple_return_values(self, holder):
11041110
obj = holder([1, 2, 3], name="x")
11051111
box = Series if holder is Series else Index
11061112

11071113
result = np.modf(obj)
11081114
assert isinstance(result, tuple)
1109-
exp1 = pd.Float64Index([0.0, 0.0, 0.0], name="x")
1110-
exp2 = pd.Float64Index([1.0, 2.0, 3.0], name="x")
1115+
exp1 = Float64Index([0.0, 0.0, 0.0], name="x")
1116+
exp2 = Float64Index([1.0, 2.0, 3.0], name="x")
11111117
tm.assert_equal(result[0], tm.box_expected(exp1, box))
11121118
tm.assert_equal(result[1], tm.box_expected(exp2, box))
11131119

@@ -1173,12 +1179,12 @@ def check_binop(self, ops, scalars, idxs):
11731179
for op in ops:
11741180
for a, b in combinations(idxs, 2):
11751181
result = op(a, b)
1176-
expected = op(pd.Int64Index(a), pd.Int64Index(b))
1182+
expected = op(Int64Index(a), Int64Index(b))
11771183
tm.assert_index_equal(result, expected)
11781184
for idx in idxs:
11791185
for scalar in scalars:
11801186
result = op(idx, scalar)
1181-
expected = op(pd.Int64Index(idx), scalar)
1187+
expected = op(Int64Index(idx), scalar)
11821188
tm.assert_index_equal(result, expected)
11831189

11841190
def test_binops(self):
@@ -1191,10 +1197,10 @@ def test_binops(self):
11911197
]
11921198
scalars = [-1, 1, 2]
11931199
idxs = [
1194-
pd.RangeIndex(0, 10, 1),
1195-
pd.RangeIndex(0, 20, 2),
1196-
pd.RangeIndex(-10, 10, 2),
1197-
pd.RangeIndex(5, -5, -1),
1200+
RangeIndex(0, 10, 1),
1201+
RangeIndex(0, 20, 2),
1202+
RangeIndex(-10, 10, 2),
1203+
RangeIndex(5, -5, -1),
11981204
]
11991205
self.check_binop(ops, scalars, idxs)
12001206

@@ -1203,7 +1209,7 @@ def test_binops_pow(self):
12031209
# https://github.com/numpy/numpy/pull/8127
12041210
ops = [pow]
12051211
scalars = [1, 2]
1206-
idxs = [pd.RangeIndex(0, 10, 1), pd.RangeIndex(0, 20, 2)]
1212+
idxs = [RangeIndex(0, 10, 1), RangeIndex(0, 20, 2)]
12071213
self.check_binop(ops, scalars, idxs)
12081214

12091215
# TODO: mod, divmod?
@@ -1221,7 +1227,7 @@ def test_binops_pow(self):
12211227
def test_arithmetic_with_frame_or_series(self, op):
12221228
# check that we return NotImplemented when operating with Series
12231229
# or DataFrame
1224-
index = pd.RangeIndex(5)
1230+
index = RangeIndex(5)
12251231
other = Series(np.random.randn(5))
12261232

12271233
expected = op(Series(index), other)
@@ -1237,26 +1243,26 @@ def test_numeric_compat2(self):
12371243
# validate that we are handling the RangeIndex overrides to numeric ops
12381244
# and returning RangeIndex where possible
12391245

1240-
idx = pd.RangeIndex(0, 10, 2)
1246+
idx = RangeIndex(0, 10, 2)
12411247

12421248
result = idx * 2
1243-
expected = pd.RangeIndex(0, 20, 4)
1249+
expected = RangeIndex(0, 20, 4)
12441250
tm.assert_index_equal(result, expected, exact=True)
12451251

12461252
result = idx + 2
1247-
expected = pd.RangeIndex(2, 12, 2)
1253+
expected = RangeIndex(2, 12, 2)
12481254
tm.assert_index_equal(result, expected, exact=True)
12491255

12501256
result = idx - 2
1251-
expected = pd.RangeIndex(-2, 8, 2)
1257+
expected = RangeIndex(-2, 8, 2)
12521258
tm.assert_index_equal(result, expected, exact=True)
12531259

12541260
result = idx / 2
1255-
expected = pd.RangeIndex(0, 5, 1).astype("float64")
1261+
expected = RangeIndex(0, 5, 1).astype("float64")
12561262
tm.assert_index_equal(result, expected, exact=True)
12571263

12581264
result = idx / 4
1259-
expected = pd.RangeIndex(0, 10, 2) / 4
1265+
expected = RangeIndex(0, 10, 2) / 4
12601266
tm.assert_index_equal(result, expected, exact=True)
12611267

12621268
result = idx // 1
@@ -1269,25 +1275,25 @@ def test_numeric_compat2(self):
12691275
tm.assert_index_equal(result, expected, exact=True)
12701276

12711277
# __pow__
1272-
idx = pd.RangeIndex(0, 1000, 2)
1278+
idx = RangeIndex(0, 1000, 2)
12731279
result = idx ** 2
12741280
expected = idx._int64index ** 2
12751281
tm.assert_index_equal(Index(result.values), expected, exact=True)
12761282

12771283
# __floordiv__
12781284
cases_exact = [
1279-
(pd.RangeIndex(0, 1000, 2), 2, pd.RangeIndex(0, 500, 1)),
1280-
(pd.RangeIndex(-99, -201, -3), -3, pd.RangeIndex(33, 67, 1)),
1281-
(pd.RangeIndex(0, 1000, 1), 2, pd.RangeIndex(0, 1000, 1)._int64index // 2),
1285+
(RangeIndex(0, 1000, 2), 2, RangeIndex(0, 500, 1)),
1286+
(RangeIndex(-99, -201, -3), -3, RangeIndex(33, 67, 1)),
1287+
(RangeIndex(0, 1000, 1), 2, RangeIndex(0, 1000, 1)._int64index // 2),
12821288
(
1283-
pd.RangeIndex(0, 100, 1),
1289+
RangeIndex(0, 100, 1),
12841290
2.0,
1285-
pd.RangeIndex(0, 100, 1)._int64index // 2.0,
1291+
RangeIndex(0, 100, 1)._int64index // 2.0,
12861292
),
1287-
(pd.RangeIndex(0), 50, pd.RangeIndex(0)),
1288-
(pd.RangeIndex(2, 4, 2), 3, pd.RangeIndex(0, 1, 1)),
1289-
(pd.RangeIndex(-5, -10, -6), 4, pd.RangeIndex(-2, -1, 1)),
1290-
(pd.RangeIndex(-100, -200, 3), 2, pd.RangeIndex(0)),
1293+
(RangeIndex(0), 50, RangeIndex(0)),
1294+
(RangeIndex(2, 4, 2), 3, RangeIndex(0, 1, 1)),
1295+
(RangeIndex(-5, -10, -6), 4, RangeIndex(-2, -1, 1)),
1296+
(RangeIndex(-100, -200, 3), 2, RangeIndex(0)),
12911297
]
12921298
for idx, div, expected in cases_exact:
12931299
tm.assert_index_equal(idx // div, expected, exact=True)

pandas/tests/dtypes/test_inference.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
Index,
4646
Interval,
4747
Period,
48+
PeriodIndex,
4849
Series,
4950
Timedelta,
5051
TimedeltaIndex,
@@ -884,30 +885,30 @@ def test_infer_dtype_timedelta_with_na(self, na_value, delta):
884885

885886
def test_infer_dtype_period(self):
886887
# GH 13664
887-
arr = np.array([pd.Period("2011-01", freq="D"), pd.Period("2011-02", freq="D")])
888+
arr = np.array([Period("2011-01", freq="D"), Period("2011-02", freq="D")])
888889
assert lib.infer_dtype(arr, skipna=True) == "period"
889890

890-
arr = np.array([pd.Period("2011-01", freq="D"), pd.Period("2011-02", freq="M")])
891+
arr = np.array([Period("2011-01", freq="D"), Period("2011-02", freq="M")])
891892
assert lib.infer_dtype(arr, skipna=True) == "period"
892893

893894
def test_infer_dtype_period_mixed(self):
894895
arr = np.array(
895-
[pd.Period("2011-01", freq="M"), np.datetime64("nat")], dtype=object
896+
[Period("2011-01", freq="M"), np.datetime64("nat")], dtype=object
896897
)
897898
assert lib.infer_dtype(arr, skipna=False) == "mixed"
898899

899900
arr = np.array(
900-
[np.datetime64("nat"), pd.Period("2011-01", freq="M")], dtype=object
901+
[np.datetime64("nat"), Period("2011-01", freq="M")], dtype=object
901902
)
902903
assert lib.infer_dtype(arr, skipna=False) == "mixed"
903904

904905
@pytest.mark.parametrize("na_value", [pd.NaT, np.nan])
905906
def test_infer_dtype_period_with_na(self, na_value):
906907
# starts with nan
907-
arr = np.array([na_value, pd.Period("2011-01", freq="D")])
908+
arr = np.array([na_value, Period("2011-01", freq="D")])
908909
assert lib.infer_dtype(arr, skipna=True) == "period"
909910

910-
arr = np.array([na_value, pd.Period("2011-01", freq="D"), na_value])
911+
arr = np.array([na_value, Period("2011-01", freq="D"), na_value])
911912
assert lib.infer_dtype(arr, skipna=True) == "period"
912913

913914
@pytest.mark.parametrize(
@@ -1192,8 +1193,8 @@ def test_to_object_array_width(self):
11921193
tm.assert_numpy_array_equal(out, expected)
11931194

11941195
def test_is_period(self):
1195-
assert lib.is_period(pd.Period("2011-01", freq="M"))
1196-
assert not lib.is_period(pd.PeriodIndex(["2011-01"], freq="M"))
1196+
assert lib.is_period(Period("2011-01", freq="M"))
1197+
assert not lib.is_period(PeriodIndex(["2011-01"], freq="M"))
11971198
assert not lib.is_period(Timestamp("2011-01"))
11981199
assert not lib.is_period(1)
11991200
assert not lib.is_period(np.nan)

pandas/tests/groupby/test_groupby.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,24 @@
88
from pandas.errors import PerformanceWarning
99

1010
import pandas as pd
11-
from pandas import DataFrame, Index, MultiIndex, Series, Timestamp, date_range, read_csv
11+
from pandas import (
12+
DataFrame,
13+
Grouper,
14+
Index,
15+
MultiIndex,
16+
Series,
17+
Timestamp,
18+
date_range,
19+
read_csv,
20+
)
1221
import pandas._testing as tm
1322
from pandas.core.base import SpecificationError
1423
import pandas.core.common as com
1524

1625

1726
def test_repr():
1827
# GH18203
19-
result = repr(pd.Grouper(key="A", level="B"))
28+
result = repr(Grouper(key="A", level="B"))
2029
expected = "Grouper(key='A', level='B', axis=0, sort=False)"
2130
assert result == expected
2231

@@ -1218,7 +1227,7 @@ def test_groupby_keys_same_size_as_index():
12181227
start=Timestamp("2015-09-29T11:34:44-0700"), periods=2, freq=freq
12191228
)
12201229
df = DataFrame([["A", 10], ["B", 15]], columns=["metric", "values"], index=index)
1221-
result = df.groupby([pd.Grouper(level=0, freq=freq), "metric"]).mean()
1230+
result = df.groupby([Grouper(level=0, freq=freq), "metric"]).mean()
12221231
expected = df.set_index([df.index, "metric"])
12231232

12241233
tm.assert_frame_equal(result, expected)
@@ -1815,7 +1824,7 @@ def test_groupby_agg_ohlc_non_first():
18151824
index=pd.date_range("2018-01-01", periods=2, freq="D"),
18161825
)
18171826

1818-
result = df.groupby(pd.Grouper(freq="D")).agg(["sum", "ohlc"])
1827+
result = df.groupby(Grouper(freq="D")).agg(["sum", "ohlc"])
18191828

18201829
tm.assert_frame_equal(result, expected)
18211830

@@ -1866,11 +1875,11 @@ def test_groupby_groups_in_BaseGrouper():
18661875
# Test if DataFrame grouped with a pandas.Grouper has correct groups
18671876
mi = MultiIndex.from_product([["A", "B"], ["C", "D"]], names=["alpha", "beta"])
18681877
df = DataFrame({"foo": [1, 2, 1, 2], "bar": [1, 2, 3, 4]}, index=mi)
1869-
result = df.groupby([pd.Grouper(level="alpha"), "beta"])
1878+
result = df.groupby([Grouper(level="alpha"), "beta"])
18701879
expected = df.groupby(["alpha", "beta"])
18711880
assert result.groups == expected.groups
18721881

1873-
result = df.groupby(["beta", pd.Grouper(level="alpha")])
1882+
result = df.groupby(["beta", Grouper(level="alpha")])
18741883
expected = df.groupby(["beta", "alpha"])
18751884
assert result.groups == expected.groups
18761885

0 commit comments

Comments
 (0)