1111import pytest
1212
1313import 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+ )
1525import pandas ._testing as tm
1626from 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
4555ser_or_index : List [Any ] = [Series , Index ]
46- lefts : List [Any ] = [pd . RangeIndex (10 , 40 , 10 )]
56+ lefts : List [Any ] = [RangeIndex (10 , 40 , 10 )]
4757lefts .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):
10401050class 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 )
0 commit comments