Skip to content

Commit ace8f9e

Browse files
jbrockmendelproost
authored andcommitted
CLN: no need for mixin, move non-index timedelta arithmetic test (pandas-dev#30220)
1 parent 1dc0244 commit ace8f9e

File tree

3 files changed

+64
-72
lines changed

3 files changed

+64
-72
lines changed

pandas/tests/indexes/test_frozen.py

+13-21
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@
55
from pandas.core.indexes.frozen import FrozenList
66

77

8-
class CheckImmutableMixin:
9-
mutable_regex = re.compile("does not support mutable operations")
8+
class TestFrozenList:
9+
10+
unicode_container = FrozenList(["\u05d0", "\u05d1", "c"])
11+
12+
def setup_method(self, _):
13+
self.lst = [1, 2, 3, 4, 5]
14+
self.container = FrozenList(self.lst)
1015

1116
def check_mutable_error(self, *args, **kwargs):
1217
# Pass whatever function you normally would to pytest.raises
1318
# (after the Exception kind).
19+
mutable_regex = re.compile("does not support mutable operations")
1420
with pytest.raises(TypeError):
15-
self.mutable_regex(*args, **kwargs)
21+
mutable_regex(*args, **kwargs)
1622

1723
def test_no_mutable_funcs(self):
1824
def setitem():
@@ -34,7 +40,8 @@ def delslice():
3440
del self.container[0:3]
3541

3642
self.check_mutable_error(delslice)
37-
mutable_methods = getattr(self, "mutable_methods", [])
43+
44+
mutable_methods = ("extend", "pop", "remove", "insert")
3845

3946
for meth in mutable_methods:
4047
self.check_mutable_error(getattr(self.container, meth))
@@ -44,34 +51,19 @@ def test_slicing_maintains_type(self):
4451
expected = self.lst[1:2]
4552
self.check_result(result, expected)
4653

47-
def check_result(self, result, expected, klass=None):
48-
klass = klass or self.klass
49-
assert isinstance(result, klass)
54+
def check_result(self, result, expected):
55+
assert isinstance(result, FrozenList)
5056
assert result == expected
5157

52-
53-
class CheckStringMixin:
5458
def test_string_methods_dont_fail(self):
5559
repr(self.container)
5660
str(self.container)
5761
bytes(self.container)
5862

5963
def test_tricky_container(self):
60-
if not hasattr(self, "unicode_container"):
61-
pytest.skip("Need unicode_container to test with this")
6264
repr(self.unicode_container)
6365
str(self.unicode_container)
6466

65-
66-
class TestFrozenList(CheckImmutableMixin, CheckStringMixin):
67-
mutable_methods = ("extend", "pop", "remove", "insert")
68-
unicode_container = FrozenList(["\u05d0", "\u05d1", "c"])
69-
70-
def setup_method(self, _):
71-
self.lst = [1, 2, 3, 4, 5]
72-
self.container = FrozenList(self.lst)
73-
self.klass = FrozenList
74-
7567
def test_add(self):
7668
result = self.container + (1, 2, 3)
7769
expected = FrozenList(self.lst + [1, 2, 3])

pandas/tests/indexes/timedeltas/test_arithmetic.py

-51
Original file line numberDiff line numberDiff line change
@@ -118,57 +118,6 @@ def test_tdi_isub_timedeltalike(self, delta):
118118

119119
# -------------------------------------------------------------
120120

121-
# TODO: after #24365 this probably belongs in scalar tests
122-
def test_ops_ndarray(self):
123-
td = Timedelta("1 day")
124-
125-
# timedelta, timedelta
126-
other = pd.to_timedelta(["1 day"]).values
127-
expected = pd.to_timedelta(["2 days"]).values
128-
tm.assert_numpy_array_equal(td + other, expected)
129-
tm.assert_numpy_array_equal(other + td, expected)
130-
msg = r"unsupported operand type\(s\) for \+: 'Timedelta' and 'int'"
131-
with pytest.raises(TypeError, match=msg):
132-
td + np.array([1])
133-
msg = r"unsupported operand type\(s\) for \+: 'numpy.ndarray' and 'Timedelta'"
134-
with pytest.raises(TypeError, match=msg):
135-
np.array([1]) + td
136-
137-
expected = pd.to_timedelta(["0 days"]).values
138-
tm.assert_numpy_array_equal(td - other, expected)
139-
tm.assert_numpy_array_equal(-other + td, expected)
140-
msg = r"unsupported operand type\(s\) for -: 'Timedelta' and 'int'"
141-
with pytest.raises(TypeError, match=msg):
142-
td - np.array([1])
143-
msg = r"unsupported operand type\(s\) for -: 'numpy.ndarray' and 'Timedelta'"
144-
with pytest.raises(TypeError, match=msg):
145-
np.array([1]) - td
146-
147-
expected = pd.to_timedelta(["2 days"]).values
148-
tm.assert_numpy_array_equal(td * np.array([2]), expected)
149-
tm.assert_numpy_array_equal(np.array([2]) * td, expected)
150-
msg = (
151-
"ufunc '?multiply'? cannot use operands with types"
152-
r" dtype\('<m8\[ns\]'\) and dtype\('<m8\[ns\]'\)"
153-
)
154-
with pytest.raises(TypeError, match=msg):
155-
td * other
156-
with pytest.raises(TypeError, match=msg):
157-
other * td
158-
159-
tm.assert_numpy_array_equal(td / other, np.array([1], dtype=np.float64))
160-
tm.assert_numpy_array_equal(other / td, np.array([1], dtype=np.float64))
161-
162-
# timedelta, datetime
163-
other = pd.to_datetime(["2000-01-01"]).values
164-
expected = pd.to_datetime(["2000-01-02"]).values
165-
tm.assert_numpy_array_equal(td + other, expected)
166-
tm.assert_numpy_array_equal(other + td, expected)
167-
168-
expected = pd.to_datetime(["1999-12-31"]).values
169-
tm.assert_numpy_array_equal(-td + other, expected)
170-
tm.assert_numpy_array_equal(other - td, expected)
171-
172121
def test_tdi_ops_attributes(self):
173122
rng = timedelta_range("2 days", periods=5, freq="2D", name="x")
174123

pandas/tests/scalar/timedelta/test_arithmetic.py

+51
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,57 @@ def test_td_add_mixed_timedeltalike_object_dtype_array(self, op):
241241
res = op(arr, Timedelta("1D"))
242242
tm.assert_numpy_array_equal(res, exp)
243243

244+
# TODO: moved from index tests following #24365, may need de-duplication
245+
def test_ops_ndarray(self):
246+
td = Timedelta("1 day")
247+
248+
# timedelta, timedelta
249+
other = pd.to_timedelta(["1 day"]).values
250+
expected = pd.to_timedelta(["2 days"]).values
251+
tm.assert_numpy_array_equal(td + other, expected)
252+
tm.assert_numpy_array_equal(other + td, expected)
253+
msg = r"unsupported operand type\(s\) for \+: 'Timedelta' and 'int'"
254+
with pytest.raises(TypeError, match=msg):
255+
td + np.array([1])
256+
msg = r"unsupported operand type\(s\) for \+: 'numpy.ndarray' and 'Timedelta'"
257+
with pytest.raises(TypeError, match=msg):
258+
np.array([1]) + td
259+
260+
expected = pd.to_timedelta(["0 days"]).values
261+
tm.assert_numpy_array_equal(td - other, expected)
262+
tm.assert_numpy_array_equal(-other + td, expected)
263+
msg = r"unsupported operand type\(s\) for -: 'Timedelta' and 'int'"
264+
with pytest.raises(TypeError, match=msg):
265+
td - np.array([1])
266+
msg = r"unsupported operand type\(s\) for -: 'numpy.ndarray' and 'Timedelta'"
267+
with pytest.raises(TypeError, match=msg):
268+
np.array([1]) - td
269+
270+
expected = pd.to_timedelta(["2 days"]).values
271+
tm.assert_numpy_array_equal(td * np.array([2]), expected)
272+
tm.assert_numpy_array_equal(np.array([2]) * td, expected)
273+
msg = (
274+
"ufunc '?multiply'? cannot use operands with types"
275+
r" dtype\('<m8\[ns\]'\) and dtype\('<m8\[ns\]'\)"
276+
)
277+
with pytest.raises(TypeError, match=msg):
278+
td * other
279+
with pytest.raises(TypeError, match=msg):
280+
other * td
281+
282+
tm.assert_numpy_array_equal(td / other, np.array([1], dtype=np.float64))
283+
tm.assert_numpy_array_equal(other / td, np.array([1], dtype=np.float64))
284+
285+
# timedelta, datetime
286+
other = pd.to_datetime(["2000-01-01"]).values
287+
expected = pd.to_datetime(["2000-01-02"]).values
288+
tm.assert_numpy_array_equal(td + other, expected)
289+
tm.assert_numpy_array_equal(other + td, expected)
290+
291+
expected = pd.to_datetime(["1999-12-31"]).values
292+
tm.assert_numpy_array_equal(-td + other, expected)
293+
tm.assert_numpy_array_equal(other - td, expected)
294+
244295

245296
class TestTimedeltaMultiplicationDivision:
246297
"""

0 commit comments

Comments
 (0)