|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Arithmetc tests for DataFrame/Series/Index/Array classes that should |
| 3 | +# behave identically. |
| 4 | +from datetime import timedelta |
| 5 | + |
| 6 | +import pytest |
| 7 | +import numpy as np |
| 8 | + |
| 9 | +import pandas as pd |
| 10 | +import pandas.util.testing as tm |
| 11 | + |
| 12 | +from pandas import Timedelta |
| 13 | + |
| 14 | + |
| 15 | +# ------------------------------------------------------------------ |
| 16 | +# Numeric dtypes Arithmetic with Timedelta Scalar |
| 17 | + |
| 18 | +class TestNumericArraylikeArithmeticWithTimedeltaScalar(object): |
| 19 | + |
| 20 | + @pytest.mark.parametrize('box', [ |
| 21 | + pd.Index, |
| 22 | + pd.Series, |
| 23 | + pytest.param(pd.DataFrame, |
| 24 | + marks=pytest.mark.xfail(reason="block.eval incorrect", |
| 25 | + strict=True)) |
| 26 | + ]) |
| 27 | + @pytest.mark.parametrize('index', [ |
| 28 | + pd.Int64Index(range(1, 11)), |
| 29 | + pd.UInt64Index(range(1, 11)), |
| 30 | + pd.Float64Index(range(1, 11)), |
| 31 | + pd.RangeIndex(1, 11)], |
| 32 | + ids=lambda x: type(x).__name__) |
| 33 | + @pytest.mark.parametrize('scalar_td', [ |
| 34 | + Timedelta(days=1), |
| 35 | + Timedelta(days=1).to_timedelta64(), |
| 36 | + Timedelta(days=1).to_pytimedelta()], |
| 37 | + ids=lambda x: type(x).__name__) |
| 38 | + def test_index_mul_timedelta(self, scalar_td, index, box): |
| 39 | + # GH#19333 |
| 40 | + |
| 41 | + if (box is pd.Series and |
| 42 | + type(scalar_td) is timedelta and index.dtype == 'f8'): |
| 43 | + raise pytest.xfail(reason="Cannot multiply timedelta by float") |
| 44 | + |
| 45 | + expected = pd.timedelta_range('1 days', '10 days') |
| 46 | + |
| 47 | + index = tm.box_expected(index, box) |
| 48 | + expected = tm.box_expected(expected, box) |
| 49 | + |
| 50 | + result = index * scalar_td |
| 51 | + tm.assert_equal(result, expected) |
| 52 | + |
| 53 | + commute = scalar_td * index |
| 54 | + tm.assert_equal(commute, expected) |
| 55 | + |
| 56 | + @pytest.mark.parametrize('box', [pd.Index, pd.Series, pd.DataFrame]) |
| 57 | + @pytest.mark.parametrize('index', [ |
| 58 | + pd.Int64Index(range(1, 3)), |
| 59 | + pd.UInt64Index(range(1, 3)), |
| 60 | + pd.Float64Index(range(1, 3)), |
| 61 | + pd.RangeIndex(1, 3)], |
| 62 | + ids=lambda x: type(x).__name__) |
| 63 | + @pytest.mark.parametrize('scalar_td', [ |
| 64 | + Timedelta(days=1), |
| 65 | + Timedelta(days=1).to_timedelta64(), |
| 66 | + Timedelta(days=1).to_pytimedelta()], |
| 67 | + ids=lambda x: type(x).__name__) |
| 68 | + def test_index_rdiv_timedelta(self, scalar_td, index, box): |
| 69 | + |
| 70 | + if box is pd.Series and type(scalar_td) is timedelta: |
| 71 | + raise pytest.xfail(reason="TODO: Figure out why this case fails") |
| 72 | + if box is pd.DataFrame and isinstance(scalar_td, timedelta): |
| 73 | + raise pytest.xfail(reason="TODO: Figure out why this case fails") |
| 74 | + |
| 75 | + expected = pd.TimedeltaIndex(['1 Day', '12 Hours']) |
| 76 | + |
| 77 | + index = tm.box_expected(index, box) |
| 78 | + expected = tm.box_expected(expected, box) |
| 79 | + |
| 80 | + result = scalar_td / index |
| 81 | + tm.assert_equal(result, expected) |
| 82 | + |
| 83 | + with pytest.raises(TypeError): |
| 84 | + index / scalar_td |
| 85 | + |
| 86 | + |
| 87 | +# ------------------------------------------------------------------ |
| 88 | +# Timedelta64[ns] dtype Arithmetic Operations |
| 89 | + |
| 90 | + |
| 91 | +class TestTimedeltaArraylikeInvalidArithmeticOps(object): |
| 92 | + |
| 93 | + @pytest.mark.parametrize('box', [ |
| 94 | + pd.Index, |
| 95 | + pd.Series, |
| 96 | + pytest.param(pd.DataFrame, |
| 97 | + marks=pytest.mark.xfail(reason="raises ValueError " |
| 98 | + "instead of TypeError", |
| 99 | + strict=True)) |
| 100 | + ]) |
| 101 | + @pytest.mark.parametrize('scalar_td', [ |
| 102 | + timedelta(minutes=5, seconds=4), |
| 103 | + Timedelta('5m4s'), |
| 104 | + Timedelta('5m4s').to_timedelta64()]) |
| 105 | + def test_td64series_pow_invalid(self, scalar_td, box): |
| 106 | + td1 = pd.Series([timedelta(minutes=5, seconds=3)] * 3) |
| 107 | + td1.iloc[2] = np.nan |
| 108 | + |
| 109 | + td1 = tm.box_expected(td1, box) |
| 110 | + |
| 111 | + # check that we are getting a TypeError |
| 112 | + # with 'operate' (from core/ops.py) for the ops that are not |
| 113 | + # defined |
| 114 | + pattern = 'operate|unsupported|cannot|not supported' |
| 115 | + with tm.assert_raises_regex(TypeError, pattern): |
| 116 | + scalar_td ** td1 |
| 117 | + |
| 118 | + with tm.assert_raises_regex(TypeError, pattern): |
| 119 | + td1 ** scalar_td |
0 commit comments