11
11
import pytest
12
12
13
13
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
+ )
15
25
import pandas ._testing as tm
16
26
from pandas .core import ops
17
27
@@ -43,7 +53,7 @@ def adjust_negative_zero(zero, expected):
43
53
# List comprehension has incompatible type List[PandasObject]; expected List[RangeIndex]
44
54
# See GH#29725
45
55
ser_or_index : List [Any ] = [Series , Index ]
46
- lefts : List [Any ] = [pd . RangeIndex (10 , 40 , 10 )]
56
+ lefts : List [Any ] = [RangeIndex (10 , 40 , 10 )]
47
57
lefts .extend (
48
58
[
49
59
cls ([10 , 20 , 30 ], dtype = dtype )
@@ -364,7 +374,7 @@ def test_divmod_zero(self, zero, numeric_idx):
364
374
@pytest .mark .parametrize ("op" , [operator .truediv , operator .floordiv ])
365
375
def test_div_negative_zero (self , zero , numeric_idx , op ):
366
376
# Check that -1 / -0.0 returns np.inf, not -np.inf
367
- if isinstance (numeric_idx , pd . UInt64Index ):
377
+ if isinstance (numeric_idx , UInt64Index ):
368
378
return
369
379
idx = numeric_idx - 3
370
380
@@ -634,15 +644,15 @@ def test_mul_int_array(self, numeric_idx):
634
644
result = idx * np .array (5 , dtype = "int64" )
635
645
tm .assert_index_equal (result , idx * 5 )
636
646
637
- arr_dtype = "uint64" if isinstance (idx , pd . UInt64Index ) else "int64"
647
+ arr_dtype = "uint64" if isinstance (idx , UInt64Index ) else "int64"
638
648
result = idx * np .arange (5 , dtype = arr_dtype )
639
649
tm .assert_index_equal (result , didx )
640
650
641
651
def test_mul_int_series (self , numeric_idx ):
642
652
idx = numeric_idx
643
653
didx = idx * idx
644
654
645
- arr_dtype = "uint64" if isinstance (idx , pd . UInt64Index ) else "int64"
655
+ arr_dtype = "uint64" if isinstance (idx , UInt64Index ) else "int64"
646
656
result = idx * Series (np .arange (5 , dtype = arr_dtype ))
647
657
tm .assert_series_equal (result , Series (didx ))
648
658
@@ -657,7 +667,7 @@ def test_mul_float_series(self, numeric_idx):
657
667
def test_mul_index (self , numeric_idx ):
658
668
# in general not true for RangeIndex
659
669
idx = numeric_idx
660
- if not isinstance (idx , pd . RangeIndex ):
670
+ if not isinstance (idx , RangeIndex ):
661
671
result = idx * idx
662
672
tm .assert_index_equal (result , idx ** 2 )
663
673
@@ -680,7 +690,7 @@ def test_pow_float(self, op, numeric_idx, box_with_array):
680
690
# test power calculations both ways, GH#14973
681
691
box = box_with_array
682
692
idx = numeric_idx
683
- expected = pd . Float64Index (op (idx .values , 2.0 ))
693
+ expected = Float64Index (op (idx .values , 2.0 ))
684
694
685
695
idx = tm .box_expected (idx , box )
686
696
expected = tm .box_expected (expected , box )
@@ -1040,74 +1050,70 @@ def test_series_divmod_zero(self):
1040
1050
class TestUFuncCompat :
1041
1051
@pytest .mark .parametrize (
1042
1052
"holder" ,
1043
- [pd . Int64Index , pd . UInt64Index , pd . Float64Index , pd . RangeIndex , Series ],
1053
+ [Int64Index , UInt64Index , Float64Index , RangeIndex , Series ],
1044
1054
)
1045
1055
def test_ufunc_compat (self , holder ):
1046
1056
box = Series if holder is Series else Index
1047
1057
1048
- if holder is pd . RangeIndex :
1049
- idx = pd . RangeIndex (0 , 5 )
1058
+ if holder is RangeIndex :
1059
+ idx = RangeIndex (0 , 5 )
1050
1060
else :
1051
1061
idx = holder (np .arange (5 , dtype = "int64" ))
1052
1062
result = np .sin (idx )
1053
1063
expected = box (np .sin (np .arange (5 , dtype = "int64" )))
1054
1064
tm .assert_equal (result , expected )
1055
1065
1056
- @pytest .mark .parametrize (
1057
- "holder" , [pd .Int64Index , pd .UInt64Index , pd .Float64Index , Series ]
1058
- )
1066
+ @pytest .mark .parametrize ("holder" , [Int64Index , UInt64Index , Float64Index , Series ])
1059
1067
def test_ufunc_coercions (self , holder ):
1060
1068
idx = holder ([1 , 2 , 3 , 4 , 5 ], name = "x" )
1061
1069
box = Series if holder is Series else Index
1062
1070
1063
1071
result = np .sqrt (idx )
1064
1072
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" )
1066
1074
exp = tm .box_expected (exp , box )
1067
1075
tm .assert_equal (result , exp )
1068
1076
1069
1077
result = np .divide (idx , 2.0 )
1070
1078
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" )
1072
1080
exp = tm .box_expected (exp , box )
1073
1081
tm .assert_equal (result , exp )
1074
1082
1075
1083
# _evaluate_numeric_binop
1076
1084
result = idx + 2.0
1077
1085
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" )
1079
1087
exp = tm .box_expected (exp , box )
1080
1088
tm .assert_equal (result , exp )
1081
1089
1082
1090
result = idx - 2.0
1083
1091
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" )
1085
1093
exp = tm .box_expected (exp , box )
1086
1094
tm .assert_equal (result , exp )
1087
1095
1088
1096
result = idx * 1.0
1089
1097
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" )
1091
1099
exp = tm .box_expected (exp , box )
1092
1100
tm .assert_equal (result , exp )
1093
1101
1094
1102
result = idx / 2.0
1095
1103
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" )
1097
1105
exp = tm .box_expected (exp , box )
1098
1106
tm .assert_equal (result , exp )
1099
1107
1100
- @pytest .mark .parametrize (
1101
- "holder" , [pd .Int64Index , pd .UInt64Index , pd .Float64Index , Series ]
1102
- )
1108
+ @pytest .mark .parametrize ("holder" , [Int64Index , UInt64Index , Float64Index , Series ])
1103
1109
def test_ufunc_multiple_return_values (self , holder ):
1104
1110
obj = holder ([1 , 2 , 3 ], name = "x" )
1105
1111
box = Series if holder is Series else Index
1106
1112
1107
1113
result = np .modf (obj )
1108
1114
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" )
1111
1117
tm .assert_equal (result [0 ], tm .box_expected (exp1 , box ))
1112
1118
tm .assert_equal (result [1 ], tm .box_expected (exp2 , box ))
1113
1119
@@ -1173,12 +1179,12 @@ def check_binop(self, ops, scalars, idxs):
1173
1179
for op in ops :
1174
1180
for a , b in combinations (idxs , 2 ):
1175
1181
result = op (a , b )
1176
- expected = op (pd . Int64Index (a ), pd . Int64Index (b ))
1182
+ expected = op (Int64Index (a ), Int64Index (b ))
1177
1183
tm .assert_index_equal (result , expected )
1178
1184
for idx in idxs :
1179
1185
for scalar in scalars :
1180
1186
result = op (idx , scalar )
1181
- expected = op (pd . Int64Index (idx ), scalar )
1187
+ expected = op (Int64Index (idx ), scalar )
1182
1188
tm .assert_index_equal (result , expected )
1183
1189
1184
1190
def test_binops (self ):
@@ -1191,10 +1197,10 @@ def test_binops(self):
1191
1197
]
1192
1198
scalars = [- 1 , 1 , 2 ]
1193
1199
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 ),
1198
1204
]
1199
1205
self .check_binop (ops , scalars , idxs )
1200
1206
@@ -1203,7 +1209,7 @@ def test_binops_pow(self):
1203
1209
# https://github.com/numpy/numpy/pull/8127
1204
1210
ops = [pow ]
1205
1211
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 )]
1207
1213
self .check_binop (ops , scalars , idxs )
1208
1214
1209
1215
# TODO: mod, divmod?
@@ -1221,7 +1227,7 @@ def test_binops_pow(self):
1221
1227
def test_arithmetic_with_frame_or_series (self , op ):
1222
1228
# check that we return NotImplemented when operating with Series
1223
1229
# or DataFrame
1224
- index = pd . RangeIndex (5 )
1230
+ index = RangeIndex (5 )
1225
1231
other = Series (np .random .randn (5 ))
1226
1232
1227
1233
expected = op (Series (index ), other )
@@ -1237,26 +1243,26 @@ def test_numeric_compat2(self):
1237
1243
# validate that we are handling the RangeIndex overrides to numeric ops
1238
1244
# and returning RangeIndex where possible
1239
1245
1240
- idx = pd . RangeIndex (0 , 10 , 2 )
1246
+ idx = RangeIndex (0 , 10 , 2 )
1241
1247
1242
1248
result = idx * 2
1243
- expected = pd . RangeIndex (0 , 20 , 4 )
1249
+ expected = RangeIndex (0 , 20 , 4 )
1244
1250
tm .assert_index_equal (result , expected , exact = True )
1245
1251
1246
1252
result = idx + 2
1247
- expected = pd . RangeIndex (2 , 12 , 2 )
1253
+ expected = RangeIndex (2 , 12 , 2 )
1248
1254
tm .assert_index_equal (result , expected , exact = True )
1249
1255
1250
1256
result = idx - 2
1251
- expected = pd . RangeIndex (- 2 , 8 , 2 )
1257
+ expected = RangeIndex (- 2 , 8 , 2 )
1252
1258
tm .assert_index_equal (result , expected , exact = True )
1253
1259
1254
1260
result = idx / 2
1255
- expected = pd . RangeIndex (0 , 5 , 1 ).astype ("float64" )
1261
+ expected = RangeIndex (0 , 5 , 1 ).astype ("float64" )
1256
1262
tm .assert_index_equal (result , expected , exact = True )
1257
1263
1258
1264
result = idx / 4
1259
- expected = pd . RangeIndex (0 , 10 , 2 ) / 4
1265
+ expected = RangeIndex (0 , 10 , 2 ) / 4
1260
1266
tm .assert_index_equal (result , expected , exact = True )
1261
1267
1262
1268
result = idx // 1
@@ -1269,25 +1275,25 @@ def test_numeric_compat2(self):
1269
1275
tm .assert_index_equal (result , expected , exact = True )
1270
1276
1271
1277
# __pow__
1272
- idx = pd . RangeIndex (0 , 1000 , 2 )
1278
+ idx = RangeIndex (0 , 1000 , 2 )
1273
1279
result = idx ** 2
1274
1280
expected = idx ._int64index ** 2
1275
1281
tm .assert_index_equal (Index (result .values ), expected , exact = True )
1276
1282
1277
1283
# __floordiv__
1278
1284
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 ),
1282
1288
(
1283
- pd . RangeIndex (0 , 100 , 1 ),
1289
+ RangeIndex (0 , 100 , 1 ),
1284
1290
2.0 ,
1285
- pd . RangeIndex (0 , 100 , 1 )._int64index // 2.0 ,
1291
+ RangeIndex (0 , 100 , 1 )._int64index // 2.0 ,
1286
1292
),
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 )),
1291
1297
]
1292
1298
for idx , div , expected in cases_exact :
1293
1299
tm .assert_index_equal (idx // div , expected , exact = True )
0 commit comments