Skip to content

Commit 6932a2c

Browse files
author
MomIsBestFriend
committed
Refactored "test_binop_other"
1 parent a72eef5 commit 6932a2c

File tree

1 file changed

+111
-42
lines changed

1 file changed

+111
-42
lines changed

pandas/tests/internals/test_internals.py

Lines changed: 111 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,63 +1164,132 @@ def any(self, axis=None):
11641164

11651165

11661166
class TestCanHoldElement:
1167-
@pytest.mark.parametrize(
1168-
"value, dtype",
1169-
[
1170-
(1, "i8"),
1171-
(1.0, "f8"),
1172-
(2 ** 63, "f8"),
1173-
(1j, "complex128"),
1174-
(2 ** 63, "complex128"),
1175-
(True, "bool"),
1176-
(np.timedelta64(20, "ns"), "<m8[ns]"),
1177-
(np.datetime64(20, "ns"), "<M8[ns]"),
1178-
],
1179-
)
1180-
@pytest.mark.parametrize(
1167+
#############################
1168+
1169+
# THINGS TO SOLVE BEFORE MERGING THIS PULL REQUEST:
1170+
1171+
# Delete this comment :)
1172+
1173+
# Should "ops" be called "_ops" instead ?
1174+
1175+
# What to do about the comment that was in the "else" block?
1176+
# """
1177+
# FIXME: Since dispatching to Series, this test no longer
1178+
# asserts anything meaningful
1179+
# """
1180+
1181+
#############################
1182+
1183+
ops = pytest.mark.parametrize(
11811184
"op",
11821185
[
11831186
operator.add,
1184-
operator.sub,
1185-
operator.mul,
1186-
operator.truediv,
11871187
operator.mod,
1188+
operator.mul,
11881189
operator.pow,
1190+
operator.sub,
1191+
operator.truediv,
11891192
],
11901193
ids=lambda x: x.__name__,
11911194
)
1192-
def test_binop_other(self, op, value, dtype):
1193-
skip = {
1194-
(operator.add, "bool"),
1195-
(operator.sub, "bool"),
1196-
(operator.mul, "bool"),
1197-
(operator.truediv, "bool"),
1198-
(operator.mod, "i8"),
1199-
(operator.mod, "complex128"),
1200-
(operator.pow, "bool"),
1201-
}
1202-
if (op, dtype) in skip:
1203-
pytest.skip("Invalid combination {},{}".format(op, dtype))
1195+
1196+
@ops
1197+
@pytest.mark.parametrize("value", [True])
1198+
def test_binop_bool(self, op, value):
1199+
dtype = "bool"
1200+
1201+
if op in [
1202+
operator.add,
1203+
operator.mul,
1204+
operator.pow,
1205+
operator.sub,
1206+
operator.truediv,
1207+
]:
1208+
pytest.skip(f"Invalid combination {op},{dtype}")
1209+
1210+
e = DummyElement(value, dtype)
1211+
s = pd.DataFrame({"A": [e.value, e.value]}, dtype=e.dtype)
1212+
1213+
result = op(s, e.value).dtypes
1214+
expected = op(s, value).dtypes
1215+
tm.assert_series_equal(result, expected)
1216+
1217+
@ops
1218+
@pytest.mark.parametrize("value", [1j, 2 ** 63])
1219+
def test_binop_complex128(self, op, value):
1220+
dtype = "complex128"
1221+
1222+
if op == operator.mod:
1223+
pytest.skip(f"Invalid combination {op},{dtype}")
1224+
1225+
e = DummyElement(value, dtype)
1226+
s = pd.DataFrame({"A": [e.value, e.value]}, dtype=e.dtype)
1227+
1228+
result = op(s, e.value).dtypes
1229+
expected = op(s, value).dtypes
1230+
tm.assert_series_equal(result, expected)
1231+
1232+
@ops
1233+
@pytest.mark.parametrize("value", [1.0, 2 ** 63])
1234+
def test_binop_f8(self, op, value):
1235+
dtype = "f8"
1236+
1237+
e = DummyElement(value, dtype)
1238+
s = pd.DataFrame({"A": [e.value, e.value]}, dtype=e.dtype)
1239+
1240+
result = op(s, e.value).dtypes
1241+
expected = op(s, value).dtypes
1242+
tm.assert_series_equal(result, expected)
1243+
1244+
@ops
1245+
@pytest.mark.parametrize("value", [1])
1246+
def test_binop_i8(self, op, value):
1247+
dtype = "i8"
1248+
1249+
if op == operator.mod:
1250+
pytest.skip(f"Invalid combination {op},{dtype}")
1251+
1252+
e = DummyElement(value, dtype)
1253+
s = pd.DataFrame({"A": [e.value, e.value]}, dtype=e.dtype)
1254+
1255+
result = op(s, e.value).dtypes
1256+
expected = op(s, value).dtypes
1257+
tm.assert_series_equal(result, expected)
1258+
1259+
@ops
1260+
@pytest.mark.parametrize("value", [np.timedelta64(20, "ns")])
1261+
def test_binop_m(self, op, value):
1262+
dtype = "<m8[ns]"
1263+
1264+
e = DummyElement(value, dtype)
1265+
s = pd.DataFrame({"A": [e.value, e.value]}, dtype=e.dtype)
1266+
1267+
if op in [operator.mul, operator.pow]:
1268+
with pytest.raises(TypeError):
1269+
op(s, e.value)
1270+
else:
1271+
result = op(s, e.value).dtypes
1272+
expected = op(s, value).dtypes
1273+
tm.assert_series_equal(result, expected)
1274+
1275+
@ops
1276+
@pytest.mark.parametrize("value", [np.datetime64(20, "ns")])
1277+
def test_binop_m_capitalized(self, op, value):
1278+
dtype = "<M8[ns]"
12041279

12051280
e = DummyElement(value, dtype)
12061281
s = pd.DataFrame({"A": [e.value, e.value]}, dtype=e.dtype)
12071282

1208-
invalid = {
1209-
(operator.pow, "<M8[ns]"),
1210-
(operator.mod, "<M8[ns]"),
1211-
(operator.truediv, "<M8[ns]"),
1212-
(operator.mul, "<M8[ns]"),
1213-
(operator.add, "<M8[ns]"),
1214-
(operator.pow, "<m8[ns]"),
1215-
(operator.mul, "<m8[ns]"),
1216-
}
1217-
1218-
if (op, dtype) in invalid:
1283+
if op in [
1284+
operator.add,
1285+
operator.mod,
1286+
operator.mul,
1287+
operator.pow,
1288+
operator.truediv,
1289+
]:
12191290
with pytest.raises(TypeError):
12201291
op(s, e.value)
12211292
else:
1222-
# FIXME: Since dispatching to Series, this test no longer
1223-
# asserts anything meaningful
12241293
result = op(s, e.value).dtypes
12251294
expected = op(s, value).dtypes
12261295
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)