5
5
6
6
from collections import abc
7
7
from decimal import Decimal
8
- from itertools import combinations
9
8
import operator
10
9
from typing import Any
11
10
@@ -928,7 +927,6 @@ def test_datetime64_with_index(self):
928
927
# TODO: taken from tests.frame.test_operators, needs cleanup
929
928
def test_frame_operators (self , float_frame ):
930
929
frame = float_frame
931
- frame2 = pd .DataFrame (float_frame , columns = ["D" , "C" , "B" , "A" ])
932
930
933
931
garbage = np .random .random (4 )
934
932
colSeries = Series (garbage , index = np .array (frame .columns ))
@@ -952,23 +950,27 @@ def test_frame_operators(self, float_frame):
952
950
else :
953
951
assert np .isnan (origVal )
954
952
953
+ def test_frame_operators_col_align (self , float_frame ):
954
+ frame2 = pd .DataFrame (float_frame , columns = ["D" , "C" , "B" , "A" ])
955
955
added = frame2 + frame2
956
956
expected = frame2 * 2
957
957
tm .assert_frame_equal (added , expected )
958
958
959
+ def test_frame_operators_none_to_nan (self ):
959
960
df = pd .DataFrame ({"a" : ["a" , None , "b" ]})
960
961
tm .assert_frame_equal (df + df , pd .DataFrame ({"a" : ["aa" , np .nan , "bb" ]}))
961
962
963
+ @pytest .mark .parametrize ("dtype" , ("float" , "int64" ))
964
+ def test_frame_operators_empty_like (self , dtype ):
962
965
# Test for issue #10181
963
- for dtype in ("float" , "int64" ):
964
- frames = [
965
- pd .DataFrame (dtype = dtype ),
966
- pd .DataFrame (columns = ["A" ], dtype = dtype ),
967
- pd .DataFrame (index = [0 ], dtype = dtype ),
968
- ]
969
- for df in frames :
970
- assert (df + df ).equals (df )
971
- tm .assert_frame_equal (df + df , df )
966
+ frames = [
967
+ pd .DataFrame (dtype = dtype ),
968
+ pd .DataFrame (columns = ["A" ], dtype = dtype ),
969
+ pd .DataFrame (index = [0 ], dtype = dtype ),
970
+ ]
971
+ for df in frames :
972
+ assert (df + df ).equals (df )
973
+ tm .assert_frame_equal (df + df , df )
972
974
973
975
@pytest .mark .parametrize (
974
976
"func" ,
@@ -1164,45 +1166,85 @@ def test_operators_reverse_object(self, op):
1164
1166
class TestNumericArithmeticUnsorted :
1165
1167
# Tests in this class have been moved from type-specific test modules
1166
1168
# but not yet sorted, parametrized, and de-duplicated
1167
-
1168
- def check_binop (self , ops , scalars , idxs ):
1169
- for op in ops :
1170
- for a , b in combinations (idxs , 2 ):
1171
- a = a ._rename ("foo" )
1172
- b = b ._rename ("bar" )
1173
- result = op (a , b )
1174
- expected = op (Int64Index (a ), Int64Index (b ))
1175
- tm .assert_index_equal (result , expected , exact = "equiv" )
1176
- for idx in idxs :
1177
- for scalar in scalars :
1178
- result = op (idx , scalar )
1179
- expected = op (Int64Index (idx ), scalar )
1180
- tm .assert_index_equal (result , expected , exact = "equiv" )
1181
-
1182
- def test_binops (self ):
1183
- ops = [
1169
+ @pytest .mark .parametrize (
1170
+ "op" ,
1171
+ [
1184
1172
operator .add ,
1185
1173
operator .sub ,
1186
1174
operator .mul ,
1187
1175
operator .floordiv ,
1188
1176
operator .truediv ,
1189
- ]
1190
- scalars = [- 1 , 1 , 2 ]
1191
- idxs = [
1177
+ ],
1178
+ )
1179
+ @pytest .mark .parametrize (
1180
+ "idx1" ,
1181
+ [
1192
1182
RangeIndex (0 , 10 , 1 ),
1193
1183
RangeIndex (0 , 20 , 2 ),
1194
1184
RangeIndex (- 10 , 10 , 2 ),
1195
1185
RangeIndex (5 , - 5 , - 1 ),
1196
- ]
1197
- self .check_binop (ops , scalars , idxs )
1186
+ ],
1187
+ )
1188
+ @pytest .mark .parametrize (
1189
+ "idx2" ,
1190
+ [
1191
+ RangeIndex (0 , 10 , 1 ),
1192
+ RangeIndex (0 , 20 , 2 ),
1193
+ RangeIndex (- 10 , 10 , 2 ),
1194
+ RangeIndex (5 , - 5 , - 1 ),
1195
+ ],
1196
+ )
1197
+ def test_binops_index (self , op , idx1 , idx2 ):
1198
+ idx1 = idx1 ._rename ("foo" )
1199
+ idx2 = idx2 ._rename ("bar" )
1200
+ result = op (idx1 , idx2 )
1201
+ expected = op (Int64Index (idx1 ), Int64Index (idx2 ))
1202
+ tm .assert_index_equal (result , expected , exact = "equiv" )
1198
1203
1199
- def test_binops_pow (self ):
1204
+ @pytest .mark .parametrize (
1205
+ "op" ,
1206
+ [
1207
+ operator .add ,
1208
+ operator .sub ,
1209
+ operator .mul ,
1210
+ operator .floordiv ,
1211
+ operator .truediv ,
1212
+ ],
1213
+ )
1214
+ @pytest .mark .parametrize (
1215
+ "idx" ,
1216
+ [
1217
+ RangeIndex (0 , 10 , 1 ),
1218
+ RangeIndex (0 , 20 , 2 ),
1219
+ RangeIndex (- 10 , 10 , 2 ),
1220
+ RangeIndex (5 , - 5 , - 1 ),
1221
+ ],
1222
+ )
1223
+ @pytest .mark .parametrize ("scalar" , [- 1 , 1 , 2 ])
1224
+ def test_binops_index_scalar (self , op , idx , scalar ):
1225
+ result = op (idx , scalar )
1226
+ expected = op (Int64Index (idx ), scalar )
1227
+ tm .assert_index_equal (result , expected , exact = "equiv" )
1228
+
1229
+ @pytest .mark .parametrize ("idx1" , [RangeIndex (0 , 10 , 1 ), RangeIndex (0 , 20 , 2 )])
1230
+ @pytest .mark .parametrize ("idx2" , [RangeIndex (0 , 10 , 1 ), RangeIndex (0 , 20 , 2 )])
1231
+ def test_binops_index_pow (self , idx1 , idx2 ):
1200
1232
# numpy does not allow powers of negative integers so test separately
1201
1233
# https://github.com/numpy/numpy/pull/8127
1202
- ops = [pow ]
1203
- scalars = [1 , 2 ]
1204
- idxs = [RangeIndex (0 , 10 , 1 ), RangeIndex (0 , 20 , 2 )]
1205
- self .check_binop (ops , scalars , idxs )
1234
+ idx1 = idx1 ._rename ("foo" )
1235
+ idx2 = idx2 ._rename ("bar" )
1236
+ result = pow (idx1 , idx2 )
1237
+ expected = pow (Int64Index (idx1 ), Int64Index (idx2 ))
1238
+ tm .assert_index_equal (result , expected , exact = "equiv" )
1239
+
1240
+ @pytest .mark .parametrize ("idx" , [RangeIndex (0 , 10 , 1 ), RangeIndex (0 , 20 , 2 )])
1241
+ @pytest .mark .parametrize ("scalar" , [1 , 2 ])
1242
+ def test_binops_index_scalar_pow (self , idx , scalar ):
1243
+ # numpy does not allow powers of negative integers so test separately
1244
+ # https://github.com/numpy/numpy/pull/8127
1245
+ result = pow (idx , scalar )
1246
+ expected = pow (Int64Index (idx ), scalar )
1247
+ tm .assert_index_equal (result , expected , exact = "equiv" )
1206
1248
1207
1249
# TODO: divmod?
1208
1250
@pytest .mark .parametrize (
@@ -1273,8 +1315,9 @@ def test_numeric_compat2(self):
1273
1315
expected = Int64Index (idx ._values ) ** 2
1274
1316
tm .assert_index_equal (Index (result .values ), expected , exact = True )
1275
1317
1276
- # __floordiv__
1277
- cases_exact = [
1318
+ @pytest .mark .parametrize (
1319
+ "idx, div, expected" ,
1320
+ [
1278
1321
(RangeIndex (0 , 1000 , 2 ), 2 , RangeIndex (0 , 500 , 1 )),
1279
1322
(RangeIndex (- 99 , - 201 , - 3 ), - 3 , RangeIndex (33 , 67 , 1 )),
1280
1323
(
@@ -1291,9 +1334,11 @@ def test_numeric_compat2(self):
1291
1334
(RangeIndex (2 , 4 , 2 ), 3 , RangeIndex (0 , 1 , 1 )),
1292
1335
(RangeIndex (- 5 , - 10 , - 6 ), 4 , RangeIndex (- 2 , - 1 , 1 )),
1293
1336
(RangeIndex (- 100 , - 200 , 3 ), 2 , RangeIndex (0 )),
1294
- ]
1295
- for idx , div , expected in cases_exact :
1296
- tm .assert_index_equal (idx // div , expected , exact = True )
1337
+ ],
1338
+ )
1339
+ def test_numeric_compat2_floordiv (self , idx , div , expected ):
1340
+ # __floordiv__
1341
+ tm .assert_index_equal (idx // div , expected , exact = True )
1297
1342
1298
1343
@pytest .mark .parametrize ("dtype" , [np .int64 , np .float64 ])
1299
1344
@pytest .mark .parametrize ("delta" , [1 , 0 , - 1 ])
0 commit comments