Skip to content

Commit

Permalink
TST/REF: Collect Reduction, Arithmetic Tests (#24776)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and jreback committed Jan 16, 2019
1 parent 8ce64b7 commit 5a15a37
Show file tree
Hide file tree
Showing 10 changed files with 393 additions and 412 deletions.
19 changes: 19 additions & 0 deletions pandas/tests/arithmetic/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,3 +1055,22 @@ def test_numeric_compat2(self):
(pd.RangeIndex(-100, -200, 3), 2, pd.RangeIndex(0))]
for idx, div, expected in cases_exact:
tm.assert_index_equal(idx // div, expected, exact=True)

@pytest.mark.parametrize('dtype', [np.int64, np.float64])
@pytest.mark.parametrize('delta', [1, 0, -1])
def test_addsub_arithmetic(self, dtype, delta):
# GH#8142
delta = dtype(delta)
index = pd.Index([10, 11, 12], dtype=dtype)
result = index + delta
expected = pd.Index(index.values + delta, dtype=dtype)
tm.assert_index_equal(result, expected)

# this subtraction used to fail
result = index - delta
expected = pd.Index(index.values - delta, dtype=dtype)
tm.assert_index_equal(result, expected)

tm.assert_index_equal(index + index, 2 * index)
tm.assert_index_equal(index - index, 0 * index)
assert not (index - index).empty
88 changes: 88 additions & 0 deletions pandas/tests/arithmetic/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Arithmetc tests for DataFrame/Series/Index/Array classes that should
# behave identically.
# Specifically for object dtype
from decimal import Decimal
import operator

import numpy as np
Expand Down Expand Up @@ -224,3 +225,90 @@ def test_mixed_timezone_series_ops_object(self):
name='xxx')
tm.assert_series_equal(ser + pd.Timedelta('00:30:00'), exp)
tm.assert_series_equal(pd.Timedelta('00:30:00') + ser, exp)

# TODO: cleanup & parametrize over box
def test_iadd_preserves_name(self):
# GH#17067, GH#19723 __iadd__ and __isub__ should preserve index name
ser = pd.Series([1, 2, 3])
ser.index.name = 'foo'

ser.index += 1
assert ser.index.name == "foo"

ser.index -= 1
assert ser.index.name == "foo"

def test_add_string(self):
# from bug report
index = pd.Index(['a', 'b', 'c'])
index2 = index + 'foo'

assert 'a' not in index2
assert 'afoo' in index2

def test_iadd_string(self):
index = pd.Index(['a', 'b', 'c'])
# doesn't fail test unless there is a check before `+=`
assert 'a' in index

index += '_x'
assert 'a_x' in index

def test_add(self):
index = tm.makeStringIndex(100)
expected = pd.Index(index.values * 2)
tm.assert_index_equal(index + index, expected)
tm.assert_index_equal(index + index.tolist(), expected)
tm.assert_index_equal(index.tolist() + index, expected)

# test add and radd
index = pd.Index(list('abc'))
expected = pd.Index(['a1', 'b1', 'c1'])
tm.assert_index_equal(index + '1', expected)
expected = pd.Index(['1a', '1b', '1c'])
tm.assert_index_equal('1' + index, expected)

def test_sub_fail(self):
index = tm.makeStringIndex(100)
with pytest.raises(TypeError):
index - 'a'
with pytest.raises(TypeError):
index - index
with pytest.raises(TypeError):
index - index.tolist()
with pytest.raises(TypeError):
index.tolist() - index

def test_sub_object(self):
# GH#19369
index = pd.Index([Decimal(1), Decimal(2)])
expected = pd.Index([Decimal(0), Decimal(1)])

result = index - Decimal(1)
tm.assert_index_equal(result, expected)

result = index - pd.Index([Decimal(1), Decimal(1)])
tm.assert_index_equal(result, expected)

with pytest.raises(TypeError):
index - 'foo'

with pytest.raises(TypeError):
index - np.array([2, 'foo'])

def test_rsub_object(self):
# GH#19369
index = pd.Index([Decimal(1), Decimal(2)])
expected = pd.Index([Decimal(1), Decimal(0)])

result = Decimal(2) - index
tm.assert_index_equal(result, expected)

result = np.array([Decimal(2), Decimal(2)]) - index
tm.assert_index_equal(result, expected)

with pytest.raises(TypeError):
'foo' - index

with pytest.raises(TypeError):
np.array([True, pd.Timestamp.now()]) - index
53 changes: 0 additions & 53 deletions pandas/tests/indexes/datetimes/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,59 +47,6 @@ def test_ops_properties_basic(self):
assert s.day == 10
pytest.raises(AttributeError, lambda: s.weekday)

def test_minmax_tz(self, tz_naive_fixture):
tz = tz_naive_fixture
# monotonic
idx1 = pd.DatetimeIndex(['2011-01-01', '2011-01-02',
'2011-01-03'], tz=tz)
assert idx1.is_monotonic

# non-monotonic
idx2 = pd.DatetimeIndex(['2011-01-01', pd.NaT, '2011-01-03',
'2011-01-02', pd.NaT], tz=tz)
assert not idx2.is_monotonic

for idx in [idx1, idx2]:
assert idx.min() == Timestamp('2011-01-01', tz=tz)
assert idx.max() == Timestamp('2011-01-03', tz=tz)
assert idx.argmin() == 0
assert idx.argmax() == 2

@pytest.mark.parametrize('op', ['min', 'max'])
def test_minmax_nat(self, op):
# Return NaT
obj = DatetimeIndex([])
assert pd.isna(getattr(obj, op)())

obj = DatetimeIndex([pd.NaT])
assert pd.isna(getattr(obj, op)())

obj = DatetimeIndex([pd.NaT, pd.NaT, pd.NaT])
assert pd.isna(getattr(obj, op)())

def test_numpy_minmax(self):
dr = pd.date_range(start='2016-01-15', end='2016-01-20')

assert np.min(dr) == Timestamp('2016-01-15 00:00:00', freq='D')
assert np.max(dr) == Timestamp('2016-01-20 00:00:00', freq='D')

errmsg = "the 'out' parameter is not supported"
with pytest.raises(ValueError, match=errmsg):
np.min(dr, out=0)

with pytest.raises(ValueError, match=errmsg):
np.max(dr, out=0)

assert np.argmin(dr) == 0
assert np.argmax(dr) == 5

errmsg = "the 'out' parameter is not supported"
with pytest.raises(ValueError, match=errmsg):
np.argmin(dr, out=0)

with pytest.raises(ValueError, match=errmsg):
np.argmax(dr, out=0)

def test_repeat_range(self, tz_naive_fixture):
tz = tz_naive_fixture
rng = date_range('1/1/2000', '1/1/2001')
Expand Down
57 changes: 1 addition & 56 deletions pandas/tests/indexes/period/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

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

def test_minmax(self):

# monotonic
idx1 = pd.PeriodIndex([NaT, '2011-01-01', '2011-01-02',
'2011-01-03'], freq='D')
assert idx1.is_monotonic

# non-monotonic
idx2 = pd.PeriodIndex(['2011-01-01', NaT, '2011-01-03',
'2011-01-02', NaT], freq='D')
assert not idx2.is_monotonic

for idx in [idx1, idx2]:
assert idx.min() == pd.Period('2011-01-01', freq='D')
assert idx.max() == pd.Period('2011-01-03', freq='D')
assert idx1.argmin() == 1
assert idx2.argmin() == 0
assert idx1.argmax() == 3
assert idx2.argmax() == 2

for op in ['min', 'max']:
# Return NaT
obj = PeriodIndex([], freq='M')
result = getattr(obj, op)()
assert result is NaT

obj = PeriodIndex([NaT], freq='M')
result = getattr(obj, op)()
assert result is NaT

obj = PeriodIndex([NaT, NaT, NaT], freq='M')
result = getattr(obj, op)()
assert result is NaT

def test_numpy_minmax(self):
pr = pd.period_range(start='2016-01-15', end='2016-01-20')

assert np.min(pr) == Period('2016-01-15', freq='D')
assert np.max(pr) == Period('2016-01-20', freq='D')

errmsg = "the 'out' parameter is not supported"
with pytest.raises(ValueError, match=errmsg):
np.min(pr, out=0)
with pytest.raises(ValueError, match=errmsg):
np.max(pr, out=0)

assert np.argmin(pr) == 0
assert np.argmax(pr) == 5

errmsg = "the 'out' parameter is not supported"
with pytest.raises(ValueError, match=errmsg):
np.argmin(pr, out=0)
with pytest.raises(ValueError, match=errmsg):
np.argmax(pr, out=0)

def test_resolution(self):
for freq, expected in zip(['A', 'Q', 'M', 'D', 'H',
'T', 'S', 'L', 'U'],
Expand Down
102 changes: 0 additions & 102 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from collections import defaultdict
from datetime import datetime, timedelta
from decimal import Decimal
import math
import sys

Expand Down Expand Up @@ -834,61 +833,6 @@ def test_union_dt_as_obj(self):
tm.assert_contains_all(self.strIndex, secondCat)
tm.assert_contains_all(self.dateIndex, firstCat)

def test_add(self):
index = self.strIndex
expected = Index(self.strIndex.values * 2)
tm.assert_index_equal(index + index, expected)
tm.assert_index_equal(index + index.tolist(), expected)
tm.assert_index_equal(index.tolist() + index, expected)

# test add and radd
index = Index(list('abc'))
expected = Index(['a1', 'b1', 'c1'])
tm.assert_index_equal(index + '1', expected)
expected = Index(['1a', '1b', '1c'])
tm.assert_index_equal('1' + index, expected)

def test_sub_fail(self):
index = self.strIndex
pytest.raises(TypeError, lambda: index - 'a')
pytest.raises(TypeError, lambda: index - index)
pytest.raises(TypeError, lambda: index - index.tolist())
pytest.raises(TypeError, lambda: index.tolist() - index)

def test_sub_object(self):
# GH#19369
index = pd.Index([Decimal(1), Decimal(2)])
expected = pd.Index([Decimal(0), Decimal(1)])

result = index - Decimal(1)
tm.assert_index_equal(result, expected)

result = index - pd.Index([Decimal(1), Decimal(1)])
tm.assert_index_equal(result, expected)

with pytest.raises(TypeError):
index - 'foo'

with pytest.raises(TypeError):
index - np.array([2, 'foo'])

def test_rsub_object(self):
# GH#19369
index = pd.Index([Decimal(1), Decimal(2)])
expected = pd.Index([Decimal(1), Decimal(0)])

result = Decimal(2) - index
tm.assert_index_equal(result, expected)

result = np.array([Decimal(2), Decimal(2)]) - index
tm.assert_index_equal(result, expected)

with pytest.raises(TypeError):
'foo' - index

with pytest.raises(TypeError):
np.array([True, pd.Timestamp.now()]) - index

def test_map_identity_mapping(self):
# GH 12766
# TODO: replace with fixture
Expand Down Expand Up @@ -1008,22 +952,6 @@ def test_append_empty_preserve_name(self, name, expected):
result = left.append(right)
assert result.name == expected

def test_add_string(self):
# from bug report
index = Index(['a', 'b', 'c'])
index2 = index + 'foo'

assert 'a' not in index2
assert 'afoo' in index2

def test_iadd_string(self):
index = pd.Index(['a', 'b', 'c'])
# doesn't fail test unless there is a check before `+=`
assert 'a' in index

index += '_x'
assert 'a_x' in index

@pytest.mark.parametrize("second_name,expected", [
(None, None), ('name', 'name')])
@pytest.mark.parametrize("sort", [True, False])
Expand Down Expand Up @@ -2146,36 +2074,6 @@ def test_string_index_repr_with_unicode_option_compat(self, index,
result = unicode(index) # noqa
assert result == expected

@pytest.mark.parametrize('dtype', [np.int64, np.float64])
@pytest.mark.parametrize('delta', [1, 0, -1])
def test_addsub_arithmetic(self, dtype, delta):
# GH 8142
delta = dtype(delta)
index = pd.Index([10, 11, 12], dtype=dtype)
result = index + delta
expected = pd.Index(index.values + delta, dtype=dtype)
tm.assert_index_equal(result, expected)

# this subtraction used to fail
result = index - delta
expected = pd.Index(index.values - delta, dtype=dtype)
tm.assert_index_equal(result, expected)

tm.assert_index_equal(index + index, 2 * index)
tm.assert_index_equal(index - index, 0 * index)
assert not (index - index).empty

def test_iadd_preserves_name(self):
# GH#17067, GH#19723 __iadd__ and __isub__ should preserve index name
ser = pd.Series([1, 2, 3])
ser.index.name = 'foo'

ser.index += 1
assert ser.index.name == "foo"

ser.index -= 1
assert ser.index.name == "foo"

def test_cached_properties_not_settable(self):
index = pd.Index([1, 2, 3])
with pytest.raises(AttributeError, match="Can't set attribute"):
Expand Down
Loading

0 comments on commit 5a15a37

Please sign in to comment.