|
10 | 10 | from pandas.errors import PerformanceWarning
|
11 | 11 |
|
12 | 12 | import pandas as pd
|
13 |
| -from pandas import Period, PeriodIndex, Series, period_range |
| 13 | +from pandas import Period, PeriodIndex, Series, TimedeltaIndex, Timestamp, period_range |
14 | 14 | import pandas._testing as tm
|
15 | 15 | from pandas.core import ops
|
16 | 16 | from pandas.core.arrays import TimedeltaArray
|
@@ -730,13 +730,13 @@ def test_pi_add_sub_td64_array_non_tick_raises(self):
|
730 | 730 | tdi = pd.TimedeltaIndex(["-1 Day", "-1 Day", "-1 Day"])
|
731 | 731 | tdarr = tdi.values
|
732 | 732 |
|
733 |
| - msg = r"Input has different freq=None from PeriodArray\(freq=Q-DEC\)" |
734 |
| - with pytest.raises(IncompatibleFrequency, match=msg): |
| 733 | + msg = r"Cannot add or subtract timedelta64\[ns\] dtype from period\[Q-DEC\]" |
| 734 | + with pytest.raises(TypeError, match=msg): |
735 | 735 | rng + tdarr
|
736 |
| - with pytest.raises(IncompatibleFrequency, match=msg): |
| 736 | + with pytest.raises(TypeError, match=msg): |
737 | 737 | tdarr + rng
|
738 | 738 |
|
739 |
| - with pytest.raises(IncompatibleFrequency, match=msg): |
| 739 | + with pytest.raises(TypeError, match=msg): |
740 | 740 | rng - tdarr
|
741 | 741 | msg = r"cannot subtract PeriodArray from timedelta64\[ns\]"
|
742 | 742 | with pytest.raises(TypeError, match=msg):
|
@@ -773,6 +773,48 @@ def test_pi_add_sub_td64_array_tick(self):
|
773 | 773 | with pytest.raises(TypeError, match=msg):
|
774 | 774 | tdi - rng
|
775 | 775 |
|
| 776 | + @pytest.mark.parametrize("pi_freq", ["D", "W", "Q", "H"]) |
| 777 | + @pytest.mark.parametrize("tdi_freq", [None, "H"]) |
| 778 | + def test_parr_sub_td64array(self, box_with_array, tdi_freq, pi_freq): |
| 779 | + box = box_with_array |
| 780 | + xbox = box if box is not tm.to_array else pd.Index |
| 781 | + |
| 782 | + tdi = TimedeltaIndex(["1 hours", "2 hours"], freq=tdi_freq) |
| 783 | + dti = Timestamp("2018-03-07 17:16:40") + tdi |
| 784 | + pi = dti.to_period(pi_freq) |
| 785 | + |
| 786 | + # TODO: parametrize over box for pi? |
| 787 | + td64obj = tm.box_expected(tdi, box) |
| 788 | + |
| 789 | + if pi_freq == "H": |
| 790 | + result = pi - td64obj |
| 791 | + expected = (pi.to_timestamp("S") - tdi).to_period(pi_freq) |
| 792 | + expected = tm.box_expected(expected, xbox) |
| 793 | + tm.assert_equal(result, expected) |
| 794 | + |
| 795 | + # Subtract from scalar |
| 796 | + result = pi[0] - td64obj |
| 797 | + expected = (pi[0].to_timestamp("S") - tdi).to_period(pi_freq) |
| 798 | + expected = tm.box_expected(expected, box) |
| 799 | + tm.assert_equal(result, expected) |
| 800 | + |
| 801 | + elif pi_freq == "D": |
| 802 | + # Tick, but non-compatible |
| 803 | + msg = "Input has different freq=None from PeriodArray" |
| 804 | + with pytest.raises(IncompatibleFrequency, match=msg): |
| 805 | + pi - td64obj |
| 806 | + with pytest.raises(IncompatibleFrequency, match=msg): |
| 807 | + pi[0] - td64obj |
| 808 | + |
| 809 | + else: |
| 810 | + # With non-Tick freq, we could not add timedelta64 array regardless |
| 811 | + # of what its resolution is |
| 812 | + msg = "Cannot add or subtract timedelta64" |
| 813 | + with pytest.raises(TypeError, match=msg): |
| 814 | + pi - td64obj |
| 815 | + with pytest.raises(TypeError, match=msg): |
| 816 | + pi[0] - td64obj |
| 817 | + |
776 | 818 | # -----------------------------------------------------------------
|
777 | 819 | # operations with array/Index of DateOffset objects
|
778 | 820 |
|
|
0 commit comments