|  | 
| 11 | 11 | 
 | 
| 12 | 12 | import pandas as pd | 
| 13 | 13 | from pandas import DataFrame, Series, Timestamp | 
|  | 14 | +from pandas.core.indexes.base import InvalidIndexError | 
| 14 | 15 | from pandas.core.indexes.datetimes import date_range | 
| 15 | 16 | from pandas.core.indexes.period import Period, PeriodIndex, period_range | 
| 16 | 17 | from pandas.core.resample import _get_period_range_edges | 
| @@ -72,17 +73,19 @@ def test_asfreq_fill_value(self, series): | 
| 72 | 73 | 
 | 
| 73 | 74 |  @pytest.mark.parametrize('freq', ['H', '12H', '2D', 'W']) | 
| 74 | 75 |  @pytest.mark.parametrize('kind', [None, 'period', 'timestamp']) | 
| 75 |  | - def test_selection(self, index, freq, kind): | 
|  | 76 | + @pytest.mark.parametrize('kwargs', [dict(on='date'), dict(level='d')]) | 
|  | 77 | + def test_selection(self, index, freq, kind, kwargs): | 
| 76 | 78 |  # This is a bug, these should be implemented | 
| 77 | 79 |  # GH 14008 | 
| 78 | 80 |  rng = np.arange(len(index), dtype=np.int64) | 
| 79 | 81 |  df = DataFrame({'date': index, 'a': rng}, | 
| 80 | 82 |  index=pd.MultiIndex.from_arrays([rng, index], | 
| 81 | 83 |  names=['v', 'd'])) | 
| 82 |  | - with pytest.raises(NotImplementedError): | 
| 83 |  | - df.resample(freq, on='date', kind=kind) | 
| 84 |  | - with pytest.raises(NotImplementedError): | 
| 85 |  | - df.resample(freq, level='d', kind=kind) | 
|  | 84 | + msg = ("Resampling from level= or on= selection with a PeriodIndex is" | 
|  | 85 | + r" not currently supported, use \.set_index\(\.\.\.\) to" | 
|  | 86 | + " explicitly set index") | 
|  | 87 | + with pytest.raises(NotImplementedError, match=msg): | 
|  | 88 | + df.resample(freq, kind=kind, **kwargs) | 
| 86 | 89 | 
 | 
| 87 | 90 |  @pytest.mark.parametrize('month', MONTHS) | 
| 88 | 91 |  @pytest.mark.parametrize('meth', ['ffill', 'bfill']) | 
| @@ -110,13 +113,20 @@ def test_basic_downsample(self, simple_period_range_series): | 
| 110 | 113 |  assert_series_equal(ts.resample('a-dec').mean(), result) | 
| 111 | 114 |  assert_series_equal(ts.resample('a').mean(), result) | 
| 112 | 115 | 
 | 
| 113 |  | - def test_not_subperiod(self, simple_period_range_series): | 
|  | 116 | + @pytest.mark.parametrize('rule,expected_error_msg', [ | 
|  | 117 | + ('a-dec', '<YearEnd: month=12>'), | 
|  | 118 | + ('q-mar', '<QuarterEnd: startingMonth=3>'), | 
|  | 119 | + ('M', '<MonthEnd>'), | 
|  | 120 | + ('w-thu', '<Week: weekday=3>') | 
|  | 121 | + ]) | 
|  | 122 | + def test_not_subperiod( | 
|  | 123 | + self, simple_period_range_series, rule, expected_error_msg): | 
| 114 | 124 |  # These are incompatible period rules for resampling | 
| 115 | 125 |  ts = simple_period_range_series('1/1/1990', '6/30/1995', freq='w-wed') | 
| 116 |  | - pytest.raises(ValueError, lambda: ts.resample('a-dec').mean()) | 
| 117 |  | - pytest.raises(ValueError, lambda: ts.resample('q-mar').mean()) | 
| 118 |  | - pytest.raises(ValueError, lambda: ts.resample('M').mean()) | 
| 119 |  | - pytest.raises(ValueError, lambda: ts.resample('w-thu').mean()) | 
|  | 126 | + msg = ("Frequency <Week: weekday=2> cannot be resampled to {}, as they" | 
|  | 127 | +  " are not sub or super periods").format(expected_error_msg) | 
|  | 128 | + with pytest.raises(IncompatibleFrequency, match=msg): | 
|  | 129 | +  ts.resample(rule).mean() | 
| 120 | 130 | 
 | 
| 121 | 131 |  @pytest.mark.parametrize('freq', ['D', '2D']) | 
| 122 | 132 |  def test_basic_upsample(self, freq, simple_period_range_series): | 
| @@ -212,8 +222,9 @@ def test_resample_same_freq(self, resample_method): | 
| 212 | 222 |  assert_series_equal(result, expected) | 
| 213 | 223 | 
 | 
| 214 | 224 |  def test_resample_incompat_freq(self): | 
| 215 |  | - | 
| 216 |  | - with pytest.raises(IncompatibleFrequency): | 
|  | 225 | + msg = ("Frequency <MonthEnd> cannot be resampled to <Week: weekday=6>," | 
|  | 226 | + " as they are not sub or super periods") | 
|  | 227 | + with pytest.raises(IncompatibleFrequency, match=msg): | 
| 217 | 228 |  Series(range(3), index=pd.period_range( | 
| 218 | 229 |  start='2000', periods=3, freq='M')).resample('W').mean() | 
| 219 | 230 | 
 | 
| @@ -373,7 +384,9 @@ def test_resample_fill_missing(self): | 
| 373 | 384 |  def test_cant_fill_missing_dups(self): | 
| 374 | 385 |  rng = PeriodIndex([2000, 2005, 2005, 2007, 2007], freq='A') | 
| 375 | 386 |  s = Series(np.random.randn(5), index=rng) | 
| 376 |  | - pytest.raises(Exception, lambda: s.resample('A').ffill()) | 
|  | 387 | + msg = "Reindexing only valid with uniquely valued Index objects" | 
|  | 388 | + with pytest.raises(InvalidIndexError, match=msg): | 
|  | 389 | + s.resample('A').ffill() | 
| 377 | 390 | 
 | 
| 378 | 391 |  @pytest.mark.parametrize('freq', ['5min']) | 
| 379 | 392 |  @pytest.mark.parametrize('kind', ['period', None, 'timestamp']) | 
|  | 
0 commit comments