|
15 | 15 | from pandas._libs.tslibs import IncompatibleFrequency
|
16 | 16 | from pandas import (
|
17 | 17 | timedelta_range,
|
18 |
| - Timedelta, Timestamp, NaT, Series, TimedeltaIndex, DatetimeIndex, |
| 18 | + Timedelta, Timestamp, NaT, Series, Index, TimedeltaIndex, DatetimeIndex, |
19 | 19 | DataFrame)
|
20 | 20 |
|
21 | 21 |
|
@@ -73,6 +73,41 @@ def box_df_fail(request):
|
73 | 73 | return request.param
|
74 | 74 |
|
75 | 75 |
|
| 76 | +# ------------------------------------------------------------------ |
| 77 | +# Numeric types Arithmetic Operations |
| 78 | + |
| 79 | +class TestNumericOps(object): |
| 80 | + |
| 81 | + @pytest.mark.parametrize('opname', ['neg', 'pos', 'abs', 'inv', 'invert']) |
| 82 | + @pytest.mark.parametrize('dtype', [int, float, object]) |
| 83 | + def test_numeric_unary_ops(self, box, dtype, opname): |
| 84 | + # GH 22335 |
| 85 | + data = [-2, 0, 4, 5] |
| 86 | + obj = box(data, dtype=dtype) |
| 87 | + |
| 88 | + def call_method(): |
| 89 | + return getattr(obj, '__{}__'.format(opname))() |
| 90 | + |
| 91 | + if opname == 'inv': |
| 92 | + # GH 22335 - 'inv' was wrong and was removed |
| 93 | + pytest.raises(AttributeError, call_method) |
| 94 | + return |
| 95 | + if opname == 'invert' and dtype is float: |
| 96 | + # inversion of floats is undefined |
| 97 | + pytest.raises(TypeError, call_method) |
| 98 | + return |
| 99 | + if dtype in (object, bool) and isinstance(obj, Index): |
| 100 | + # GH 16873 - numeric operations on 'object' dtype might be removed |
| 101 | + # from NDFrames as well |
| 102 | + pytest.raises(TypeError, call_method) |
| 103 | + return |
| 104 | + |
| 105 | + result = call_method() |
| 106 | + library_op = getattr(operator, opname) |
| 107 | + expected = box([library_op(i) for i in data], dtype=dtype) |
| 108 | + tm.assert_equal(result, expected) |
| 109 | + |
| 110 | + |
76 | 111 | # ------------------------------------------------------------------
|
77 | 112 | # Timedelta64[ns] dtype Comparisons
|
78 | 113 |
|
|
0 commit comments