|
9 | 9 | from pandas import (DatetimeIndex, PeriodIndex, Series, Timestamp,
|
10 | 10 | date_range, _np_version_under1p10, Index,
|
11 | 11 | bdate_range)
|
12 |
| -from pandas.tseries.offsets import BMonthEnd, CDay, BDay |
| 12 | +from pandas.tseries.offsets import BMonthEnd, CDay, BDay, Day, Hour |
13 | 13 | from pandas.tests.test_base import Ops
|
| 14 | +from pandas.core.dtypes.generic import ABCDateOffset |
14 | 15 |
|
15 | 16 |
|
16 | 17 | @pytest.fixture(params=[None, 'UTC', 'Asia/Tokyo', 'US/Eastern',
|
@@ -405,6 +406,38 @@ def test_equals(self):
|
405 | 406 | assert not idx.equals(list(idx3))
|
406 | 407 | assert not idx.equals(pd.Series(idx3))
|
407 | 408 |
|
| 409 | + @pytest.mark.parametrize('values', [ |
| 410 | + ['20180101', '20180103', '20180105'], []]) |
| 411 | + @pytest.mark.parametrize('freq', [ |
| 412 | + '2D', Day(2), '2B', BDay(2), '48H', Hour(48)]) |
| 413 | + @pytest.mark.parametrize('tz', [None, 'US/Eastern']) |
| 414 | + def test_freq_setter(self, values, freq, tz): |
| 415 | + # GH 20678 |
| 416 | + idx = DatetimeIndex(values, tz=tz) |
| 417 | + |
| 418 | + # can set to an offset, converting from string if necessary |
| 419 | + idx.freq = freq |
| 420 | + assert idx.freq == freq |
| 421 | + assert isinstance(idx.freq, ABCDateOffset) |
| 422 | + |
| 423 | + # can reset to None |
| 424 | + idx.freq = None |
| 425 | + assert idx.freq is None |
| 426 | + |
| 427 | + def test_freq_setter_errors(self): |
| 428 | + # GH 20678 |
| 429 | + idx = DatetimeIndex(['20180101', '20180103', '20180105']) |
| 430 | + |
| 431 | + # setting with an incompatible freq |
| 432 | + msg = ('Inferred frequency 2D from passed values does not conform to ' |
| 433 | + 'passed frequency 5D') |
| 434 | + with tm.assert_raises_regex(ValueError, msg): |
| 435 | + idx.freq = '5D' |
| 436 | + |
| 437 | + # setting with non-freq string |
| 438 | + with tm.assert_raises_regex(ValueError, 'Invalid frequency'): |
| 439 | + idx.freq = 'foo' |
| 440 | + |
408 | 441 | def test_offset_deprecated(self):
|
409 | 442 | # GH 20716
|
410 | 443 | idx = pd.DatetimeIndex(['20180101', '20180102'])
|
|
0 commit comments