Skip to content

Commit 5a15a37

Browse files
jbrockmendeljreback
authored andcommitted
TST/REF: Collect Reduction, Arithmetic Tests (#24776)
1 parent 8ce64b7 commit 5a15a37

File tree

10 files changed

+393
-412
lines changed

10 files changed

+393
-412
lines changed

pandas/tests/arithmetic/test_numeric.py

+19
Original file line numberDiff line numberDiff line change
@@ -1055,3 +1055,22 @@ def test_numeric_compat2(self):
10551055
(pd.RangeIndex(-100, -200, 3), 2, pd.RangeIndex(0))]
10561056
for idx, div, expected in cases_exact:
10571057
tm.assert_index_equal(idx // div, expected, exact=True)
1058+
1059+
@pytest.mark.parametrize('dtype', [np.int64, np.float64])
1060+
@pytest.mark.parametrize('delta', [1, 0, -1])
1061+
def test_addsub_arithmetic(self, dtype, delta):
1062+
# GH#8142
1063+
delta = dtype(delta)
1064+
index = pd.Index([10, 11, 12], dtype=dtype)
1065+
result = index + delta
1066+
expected = pd.Index(index.values + delta, dtype=dtype)
1067+
tm.assert_index_equal(result, expected)
1068+
1069+
# this subtraction used to fail
1070+
result = index - delta
1071+
expected = pd.Index(index.values - delta, dtype=dtype)
1072+
tm.assert_index_equal(result, expected)
1073+
1074+
tm.assert_index_equal(index + index, 2 * index)
1075+
tm.assert_index_equal(index - index, 0 * index)
1076+
assert not (index - index).empty

pandas/tests/arithmetic/test_object.py

+88
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Arithmetc tests for DataFrame/Series/Index/Array classes that should
33
# behave identically.
44
# Specifically for object dtype
5+
from decimal import Decimal
56
import operator
67

78
import numpy as np
@@ -224,3 +225,90 @@ def test_mixed_timezone_series_ops_object(self):
224225
name='xxx')
225226
tm.assert_series_equal(ser + pd.Timedelta('00:30:00'), exp)
226227
tm.assert_series_equal(pd.Timedelta('00:30:00') + ser, exp)
228+
229+
# TODO: cleanup & parametrize over box
230+
def test_iadd_preserves_name(self):
231+
# GH#17067, GH#19723 __iadd__ and __isub__ should preserve index name
232+
ser = pd.Series([1, 2, 3])
233+
ser.index.name = 'foo'
234+
235+
ser.index += 1
236+
assert ser.index.name == "foo"
237+
238+
ser.index -= 1
239+
assert ser.index.name == "foo"
240+
241+
def test_add_string(self):
242+
# from bug report
243+
index = pd.Index(['a', 'b', 'c'])
244+
index2 = index + 'foo'
245+
246+
assert 'a' not in index2
247+
assert 'afoo' in index2
248+
249+
def test_iadd_string(self):
250+
index = pd.Index(['a', 'b', 'c'])
251+
# doesn't fail test unless there is a check before `+=`
252+
assert 'a' in index
253+
254+
index += '_x'
255+
assert 'a_x' in index
256+
257+
def test_add(self):
258+
index = tm.makeStringIndex(100)
259+
expected = pd.Index(index.values * 2)
260+
tm.assert_index_equal(index + index, expected)
261+
tm.assert_index_equal(index + index.tolist(), expected)
262+
tm.assert_index_equal(index.tolist() + index, expected)
263+
264+
# test add and radd
265+
index = pd.Index(list('abc'))
266+
expected = pd.Index(['a1', 'b1', 'c1'])
267+
tm.assert_index_equal(index + '1', expected)
268+
expected = pd.Index(['1a', '1b', '1c'])
269+
tm.assert_index_equal('1' + index, expected)
270+
271+
def test_sub_fail(self):
272+
index = tm.makeStringIndex(100)
273+
with pytest.raises(TypeError):
274+
index - 'a'
275+
with pytest.raises(TypeError):
276+
index - index
277+
with pytest.raises(TypeError):
278+
index - index.tolist()
279+
with pytest.raises(TypeError):
280+
index.tolist() - index
281+
282+
def test_sub_object(self):
283+
# GH#19369
284+
index = pd.Index([Decimal(1), Decimal(2)])
285+
expected = pd.Index([Decimal(0), Decimal(1)])
286+
287+
result = index - Decimal(1)
288+
tm.assert_index_equal(result, expected)
289+
290+
result = index - pd.Index([Decimal(1), Decimal(1)])
291+
tm.assert_index_equal(result, expected)
292+
293+
with pytest.raises(TypeError):
294+
index - 'foo'
295+
296+
with pytest.raises(TypeError):
297+
index - np.array([2, 'foo'])
298+
299+
def test_rsub_object(self):
300+
# GH#19369
301+
index = pd.Index([Decimal(1), Decimal(2)])
302+
expected = pd.Index([Decimal(1), Decimal(0)])
303+
304+
result = Decimal(2) - index
305+
tm.assert_index_equal(result, expected)
306+
307+
result = np.array([Decimal(2), Decimal(2)]) - index
308+
tm.assert_index_equal(result, expected)
309+
310+
with pytest.raises(TypeError):
311+
'foo' - index
312+
313+
with pytest.raises(TypeError):
314+
np.array([True, pd.Timestamp.now()]) - index

pandas/tests/indexes/datetimes/test_ops.py

-53
Original file line numberDiff line numberDiff line change
@@ -47,59 +47,6 @@ def test_ops_properties_basic(self):
4747
assert s.day == 10
4848
pytest.raises(AttributeError, lambda: s.weekday)
4949

50-
def test_minmax_tz(self, tz_naive_fixture):
51-
tz = tz_naive_fixture
52-
# monotonic
53-
idx1 = pd.DatetimeIndex(['2011-01-01', '2011-01-02',
54-
'2011-01-03'], tz=tz)
55-
assert idx1.is_monotonic
56-
57-
# non-monotonic
58-
idx2 = pd.DatetimeIndex(['2011-01-01', pd.NaT, '2011-01-03',
59-
'2011-01-02', pd.NaT], tz=tz)
60-
assert not idx2.is_monotonic
61-
62-
for idx in [idx1, idx2]:
63-
assert idx.min() == Timestamp('2011-01-01', tz=tz)
64-
assert idx.max() == Timestamp('2011-01-03', tz=tz)
65-
assert idx.argmin() == 0
66-
assert idx.argmax() == 2
67-
68-
@pytest.mark.parametrize('op', ['min', 'max'])
69-
def test_minmax_nat(self, op):
70-
# Return NaT
71-
obj = DatetimeIndex([])
72-
assert pd.isna(getattr(obj, op)())
73-
74-
obj = DatetimeIndex([pd.NaT])
75-
assert pd.isna(getattr(obj, op)())
76-
77-
obj = DatetimeIndex([pd.NaT, pd.NaT, pd.NaT])
78-
assert pd.isna(getattr(obj, op)())
79-
80-
def test_numpy_minmax(self):
81-
dr = pd.date_range(start='2016-01-15', end='2016-01-20')
82-
83-
assert np.min(dr) == Timestamp('2016-01-15 00:00:00', freq='D')
84-
assert np.max(dr) == Timestamp('2016-01-20 00:00:00', freq='D')
85-
86-
errmsg = "the 'out' parameter is not supported"
87-
with pytest.raises(ValueError, match=errmsg):
88-
np.min(dr, out=0)
89-
90-
with pytest.raises(ValueError, match=errmsg):
91-
np.max(dr, out=0)
92-
93-
assert np.argmin(dr) == 0
94-
assert np.argmax(dr) == 5
95-
96-
errmsg = "the 'out' parameter is not supported"
97-
with pytest.raises(ValueError, match=errmsg):
98-
np.argmin(dr, out=0)
99-
100-
with pytest.raises(ValueError, match=errmsg):
101-
np.argmax(dr, out=0)
102-
10350
def test_repeat_range(self, tz_naive_fixture):
10451
tz = tz_naive_fixture
10552
rng = date_range('1/1/2000', '1/1/2001')

pandas/tests/indexes/period/test_ops.py

+1-56
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44

55
import pandas as pd
6-
from pandas import DatetimeIndex, Index, NaT, Period, PeriodIndex, Series
6+
from pandas import DatetimeIndex, Index, NaT, PeriodIndex, Series
77
from pandas.core.arrays import PeriodArray
88
from pandas.tests.test_base import Ops
99
import pandas.util.testing as tm
@@ -24,61 +24,6 @@ def test_ops_properties(self):
2424
self.check_ops_properties(PeriodArray._object_ops, f)
2525
self.check_ops_properties(PeriodArray._bool_ops, f)
2626

27-
def test_minmax(self):
28-
29-
# monotonic
30-
idx1 = pd.PeriodIndex([NaT, '2011-01-01', '2011-01-02',
31-
'2011-01-03'], freq='D')
32-
assert idx1.is_monotonic
33-
34-
# non-monotonic
35-
idx2 = pd.PeriodIndex(['2011-01-01', NaT, '2011-01-03',
36-
'2011-01-02', NaT], freq='D')
37-
assert not idx2.is_monotonic
38-
39-
for idx in [idx1, idx2]:
40-
assert idx.min() == pd.Period('2011-01-01', freq='D')
41-
assert idx.max() == pd.Period('2011-01-03', freq='D')
42-
assert idx1.argmin() == 1
43-
assert idx2.argmin() == 0
44-
assert idx1.argmax() == 3
45-
assert idx2.argmax() == 2
46-
47-
for op in ['min', 'max']:
48-
# Return NaT
49-
obj = PeriodIndex([], freq='M')
50-
result = getattr(obj, op)()
51-
assert result is NaT
52-
53-
obj = PeriodIndex([NaT], freq='M')
54-
result = getattr(obj, op)()
55-
assert result is NaT
56-
57-
obj = PeriodIndex([NaT, NaT, NaT], freq='M')
58-
result = getattr(obj, op)()
59-
assert result is NaT
60-
61-
def test_numpy_minmax(self):
62-
pr = pd.period_range(start='2016-01-15', end='2016-01-20')
63-
64-
assert np.min(pr) == Period('2016-01-15', freq='D')
65-
assert np.max(pr) == Period('2016-01-20', freq='D')
66-
67-
errmsg = "the 'out' parameter is not supported"
68-
with pytest.raises(ValueError, match=errmsg):
69-
np.min(pr, out=0)
70-
with pytest.raises(ValueError, match=errmsg):
71-
np.max(pr, out=0)
72-
73-
assert np.argmin(pr) == 0
74-
assert np.argmax(pr) == 5
75-
76-
errmsg = "the 'out' parameter is not supported"
77-
with pytest.raises(ValueError, match=errmsg):
78-
np.argmin(pr, out=0)
79-
with pytest.raises(ValueError, match=errmsg):
80-
np.argmax(pr, out=0)
81-
8227
def test_resolution(self):
8328
for freq, expected in zip(['A', 'Q', 'M', 'D', 'H',
8429
'T', 'S', 'L', 'U'],

pandas/tests/indexes/test_base.py

-102
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from collections import defaultdict
44
from datetime import datetime, timedelta
5-
from decimal import Decimal
65
import math
76
import sys
87

@@ -834,61 +833,6 @@ def test_union_dt_as_obj(self):
834833
tm.assert_contains_all(self.strIndex, secondCat)
835834
tm.assert_contains_all(self.dateIndex, firstCat)
836835

837-
def test_add(self):
838-
index = self.strIndex
839-
expected = Index(self.strIndex.values * 2)
840-
tm.assert_index_equal(index + index, expected)
841-
tm.assert_index_equal(index + index.tolist(), expected)
842-
tm.assert_index_equal(index.tolist() + index, expected)
843-
844-
# test add and radd
845-
index = Index(list('abc'))
846-
expected = Index(['a1', 'b1', 'c1'])
847-
tm.assert_index_equal(index + '1', expected)
848-
expected = Index(['1a', '1b', '1c'])
849-
tm.assert_index_equal('1' + index, expected)
850-
851-
def test_sub_fail(self):
852-
index = self.strIndex
853-
pytest.raises(TypeError, lambda: index - 'a')
854-
pytest.raises(TypeError, lambda: index - index)
855-
pytest.raises(TypeError, lambda: index - index.tolist())
856-
pytest.raises(TypeError, lambda: index.tolist() - index)
857-
858-
def test_sub_object(self):
859-
# GH#19369
860-
index = pd.Index([Decimal(1), Decimal(2)])
861-
expected = pd.Index([Decimal(0), Decimal(1)])
862-
863-
result = index - Decimal(1)
864-
tm.assert_index_equal(result, expected)
865-
866-
result = index - pd.Index([Decimal(1), Decimal(1)])
867-
tm.assert_index_equal(result, expected)
868-
869-
with pytest.raises(TypeError):
870-
index - 'foo'
871-
872-
with pytest.raises(TypeError):
873-
index - np.array([2, 'foo'])
874-
875-
def test_rsub_object(self):
876-
# GH#19369
877-
index = pd.Index([Decimal(1), Decimal(2)])
878-
expected = pd.Index([Decimal(1), Decimal(0)])
879-
880-
result = Decimal(2) - index
881-
tm.assert_index_equal(result, expected)
882-
883-
result = np.array([Decimal(2), Decimal(2)]) - index
884-
tm.assert_index_equal(result, expected)
885-
886-
with pytest.raises(TypeError):
887-
'foo' - index
888-
889-
with pytest.raises(TypeError):
890-
np.array([True, pd.Timestamp.now()]) - index
891-
892836
def test_map_identity_mapping(self):
893837
# GH 12766
894838
# TODO: replace with fixture
@@ -1008,22 +952,6 @@ def test_append_empty_preserve_name(self, name, expected):
1008952
result = left.append(right)
1009953
assert result.name == expected
1010954

1011-
def test_add_string(self):
1012-
# from bug report
1013-
index = Index(['a', 'b', 'c'])
1014-
index2 = index + 'foo'
1015-
1016-
assert 'a' not in index2
1017-
assert 'afoo' in index2
1018-
1019-
def test_iadd_string(self):
1020-
index = pd.Index(['a', 'b', 'c'])
1021-
# doesn't fail test unless there is a check before `+=`
1022-
assert 'a' in index
1023-
1024-
index += '_x'
1025-
assert 'a_x' in index
1026-
1027955
@pytest.mark.parametrize("second_name,expected", [
1028956
(None, None), ('name', 'name')])
1029957
@pytest.mark.parametrize("sort", [True, False])
@@ -2146,36 +2074,6 @@ def test_string_index_repr_with_unicode_option_compat(self, index,
21462074
result = unicode(index) # noqa
21472075
assert result == expected
21482076

2149-
@pytest.mark.parametrize('dtype', [np.int64, np.float64])
2150-
@pytest.mark.parametrize('delta', [1, 0, -1])
2151-
def test_addsub_arithmetic(self, dtype, delta):
2152-
# GH 8142
2153-
delta = dtype(delta)
2154-
index = pd.Index([10, 11, 12], dtype=dtype)
2155-
result = index + delta
2156-
expected = pd.Index(index.values + delta, dtype=dtype)
2157-
tm.assert_index_equal(result, expected)
2158-
2159-
# this subtraction used to fail
2160-
result = index - delta
2161-
expected = pd.Index(index.values - delta, dtype=dtype)
2162-
tm.assert_index_equal(result, expected)
2163-
2164-
tm.assert_index_equal(index + index, 2 * index)
2165-
tm.assert_index_equal(index - index, 0 * index)
2166-
assert not (index - index).empty
2167-
2168-
def test_iadd_preserves_name(self):
2169-
# GH#17067, GH#19723 __iadd__ and __isub__ should preserve index name
2170-
ser = pd.Series([1, 2, 3])
2171-
ser.index.name = 'foo'
2172-
2173-
ser.index += 1
2174-
assert ser.index.name == "foo"
2175-
2176-
ser.index -= 1
2177-
assert ser.index.name == "foo"
2178-
21792077
def test_cached_properties_not_settable(self):
21802078
index = pd.Index([1, 2, 3])
21812079
with pytest.raises(AttributeError, match="Can't set attribute"):

0 commit comments

Comments
 (0)