|
9 | 9 | period_range, Period, PeriodIndex,
|
10 | 10 | _np_version_under1p10)
|
11 | 11 | import pandas.core.indexes.period as period
|
| 12 | +from pandas.errors import PerformanceWarning |
12 | 13 |
|
13 | 14 |
|
14 | 15 | _common_mismatch = [pd.offsets.YearBegin(2),
|
@@ -254,32 +255,57 @@ def test_comp_nat(self, dtype):
|
254 | 255 |
|
255 | 256 |
|
256 | 257 | class TestPeriodIndexArithmetic(object):
|
257 |
| - def test_pi_add_offset_array(self): |
| 258 | + @pytest.mark.parametrize('box', [np.array, pd.Index]) |
| 259 | + def test_pi_add_offset_array(self, box): |
258 | 260 | # GH#18849
|
259 | 261 | pi = pd.PeriodIndex([pd.Period('2015Q1'), pd.Period('2016Q2')])
|
260 |
| - offs = np.array([pd.offsets.QuarterEnd(n=1, startingMonth=12), |
261 |
| - pd.offsets.QuarterEnd(n=-2, startingMonth=12)]) |
262 |
| - res = pi + offs |
| 262 | + offs = box([pd.offsets.QuarterEnd(n=1, startingMonth=12), |
| 263 | + pd.offsets.QuarterEnd(n=-2, startingMonth=12)]) |
263 | 264 | expected = pd.PeriodIndex([pd.Period('2015Q2'), pd.Period('2015Q4')])
|
| 265 | + |
| 266 | + with tm.assert_produces_warning(PerformanceWarning): |
| 267 | + res = pi + offs |
264 | 268 | tm.assert_index_equal(res, expected)
|
265 | 269 |
|
| 270 | + with tm.assert_produces_warning(PerformanceWarning): |
| 271 | + res2 = offs + pi |
| 272 | + tm.assert_index_equal(res2, expected) |
| 273 | + |
266 | 274 | unanchored = np.array([pd.offsets.Hour(n=1),
|
267 | 275 | pd.offsets.Minute(n=-2)])
|
| 276 | + # addition/subtraction ops with incompatible offsets should issue |
| 277 | + # a PerformanceWarning and _then_ raise a TypeError. |
268 | 278 | with pytest.raises(period.IncompatibleFrequency):
|
269 |
| - pi + unanchored |
270 |
| - with pytest.raises(TypeError): |
271 |
| - unanchored + pi |
| 279 | + with tm.assert_produces_warning(PerformanceWarning): |
| 280 | + pi + unanchored |
| 281 | + with pytest.raises(period.IncompatibleFrequency): |
| 282 | + with tm.assert_produces_warning(PerformanceWarning): |
| 283 | + unanchored + pi |
272 | 284 |
|
273 |
| - @pytest.mark.xfail(reason='GH#18824 radd doesnt implement this case') |
274 |
| - def test_pi_radd_offset_array(self): |
275 |
| - # GH#18849 |
| 285 | + @pytest.mark.parametrize('box', [np.array, pd.Index]) |
| 286 | + def test_pi_sub_offset_array(self, box): |
| 287 | + # GH#18824 |
276 | 288 | pi = pd.PeriodIndex([pd.Period('2015Q1'), pd.Period('2016Q2')])
|
277 |
| - offs = np.array([pd.offsets.QuarterEnd(n=1, startingMonth=12), |
278 |
| - pd.offsets.QuarterEnd(n=-2, startingMonth=12)]) |
279 |
| - res = offs + pi |
280 |
| - expected = pd.PeriodIndex([pd.Period('2015Q2'), pd.Period('2015Q4')]) |
| 289 | + other = box([pd.offsets.QuarterEnd(n=1, startingMonth=12), |
| 290 | + pd.offsets.QuarterEnd(n=-2, startingMonth=12)]) |
| 291 | + |
| 292 | + expected = PeriodIndex([pi[n] - other[n] for n in range(len(pi))]) |
| 293 | + |
| 294 | + with tm.assert_produces_warning(PerformanceWarning): |
| 295 | + res = pi - other |
281 | 296 | tm.assert_index_equal(res, expected)
|
282 | 297 |
|
| 298 | + anchored = box([pd.offsets.MonthEnd(), pd.offsets.Day(n=2)]) |
| 299 | + |
| 300 | + # addition/subtraction ops with anchored offsets should issue |
| 301 | + # a PerformanceWarning and _then_ raise a TypeError. |
| 302 | + with pytest.raises(period.IncompatibleFrequency): |
| 303 | + with tm.assert_produces_warning(PerformanceWarning): |
| 304 | + pi - anchored |
| 305 | + with pytest.raises(period.IncompatibleFrequency): |
| 306 | + with tm.assert_produces_warning(PerformanceWarning): |
| 307 | + anchored - pi |
| 308 | + |
283 | 309 | def test_pi_add_iadd_pi_raises(self):
|
284 | 310 | rng = pd.period_range('1/1/2000', freq='D', periods=5)
|
285 | 311 | other = pd.period_range('1/6/2000', freq='D', periods=5)
|
|
0 commit comments