From b9a920e1a9012e88719cc96e8113bb877279c854 Mon Sep 17 00:00:00 2001 From: Spencer Clark Date: Thu, 11 Apr 2019 07:42:08 -0400 Subject: [PATCH] Reduce length of cftime resample tests (#2879) * Reduce length of cftime resample tests * Update per @jwenfai's suggestions; silence to_datetimeindex warnings * Restore 91% coverage for resample_cftime.py --- xarray/tests/test_cftimeindex_resample.py | 121 ++++++++++------------ 1 file changed, 52 insertions(+), 69 deletions(-) diff --git a/xarray/tests/test_cftimeindex_resample.py b/xarray/tests/test_cftimeindex_resample.py index 636f9ef7b0e..7aca4492680 100644 --- a/xarray/tests/test_cftimeindex_resample.py +++ b/xarray/tests/test_cftimeindex_resample.py @@ -6,31 +6,47 @@ import xarray as xr from xarray.core.resample_cftime import CFTimeGrouper + pytest.importorskip('cftime') pytest.importorskip('pandas', minversion='0.24') -@pytest.fixture( - params=[ - dict(start='2004-01-01T12:07:01', periods=91, freq='3D'), - dict(start='1892-01-03T12:07:01', periods=15, freq='41987T'), - dict(start='2004-01-01T12:07:01', periods=7, freq='3Q-AUG'), - dict(start='1892-01-03T12:07:01', periods=10, freq='3AS-JUN') - ], - ids=['3D', '41987T', '3Q_AUG', '3AS_JUN'] -) -def time_range_kwargs(request): - return request.param - - -@pytest.fixture() -def datetime_index(time_range_kwargs): - return pd.date_range(**time_range_kwargs) - - -@pytest.fixture() -def cftime_index(time_range_kwargs): - return xr.cftime_range(**time_range_kwargs) +# Create a list of pairs of similar-length initial and resample frequencies +# that cover: +# - Resampling from shorter to longer frequencies +# - Resampling from longer to shorter frequencies +# - Resampling from one initial frequency to another. +# These are used to test the cftime version of resample against pandas +# with a standard calendar. +FREQS = [ + ('8003D', '4001D'), + ('8003D', '16006D'), + ('8003D', '21AS'), + ('6H', '3H'), + ('6H', '12H'), + ('6H', '400T'), + ('3D', 'D'), + ('3D', '6D'), + ('11D', 'MS'), + ('3MS', 'MS'), + ('3MS', '6MS'), + ('3MS', '85D'), + ('7M', '3M'), + ('7M', '14M'), + ('7M', '2QS-APR'), + ('43QS-AUG', '21QS-AUG'), + ('43QS-AUG', '86QS-AUG'), + ('43QS-AUG', '11A-JUN'), + ('11Q-JUN', '5Q-JUN'), + ('11Q-JUN', '22Q-JUN'), + ('11Q-JUN', '51MS'), + ('3AS-MAR', 'AS-MAR'), + ('3AS-MAR', '6AS-MAR'), + ('3AS-MAR', '14Q-FEB'), + ('7A-MAY', '3A-MAY'), + ('7A-MAY', '14A-MAY'), + ('7A-MAY', '85M') +] def da(index): @@ -38,65 +54,31 @@ def da(index): coords=[index], dims=['time']) -@pytest.mark.parametrize('freq', [ - '700T', '8001T', - '12H', '8001H', - '8D', '8001D', - '2MS', '3MS', - '2QS-AUG', '3QS-SEP', - '3AS-MAR', '4A-MAY']) -@pytest.mark.parametrize('closed', [None, 'right']) -@pytest.mark.parametrize('label', [None, 'right']) +@pytest.mark.parametrize('freqs', FREQS, ids=lambda x: '{}->{}'.format(*x)) +@pytest.mark.parametrize('closed', [None, 'left', 'right']) +@pytest.mark.parametrize('label', [None, 'left', 'right']) @pytest.mark.parametrize('base', [24, 31]) -def test_resampler(freq, closed, label, base, - datetime_index, cftime_index): - # Fairly extensive testing for standard/proleptic Gregorian calendar - # For any frequencies which are not greater-than-day and anchored - # at the end, the default values for closed and label are 'left'. - loffset = '12H' - try: - da_datetime = da(datetime_index).resample( - time=freq, closed=closed, label=label, base=base, - loffset=loffset).mean() - except ValueError: - with pytest.raises(ValueError): - da(cftime_index).resample( - time=freq, closed=closed, label=label, base=base, - loffset=loffset).mean() - else: - da_cftime = da(cftime_index).resample(time=freq, closed=closed, - label=label, base=base, - loffset=loffset).mean() - da_cftime['time'] = da_cftime.indexes['time'].to_datetimeindex() - xr.testing.assert_identical(da_cftime, da_datetime) - +def test_resample(freqs, closed, label, base): + initial_freq, resample_freq = freqs + start = '2000-01-01T12:07:01' + index_kwargs = dict(start=start, periods=5, freq=initial_freq) + datetime_index = pd.date_range(**index_kwargs) + cftime_index = xr.cftime_range(**index_kwargs) -@pytest.mark.parametrize('freq', [ - '2M', '3M', - '2Q-JUN', '3Q-JUL', - '3A-FEB', '4A-APR']) -@pytest.mark.parametrize('closed', ['left', None]) -@pytest.mark.parametrize('label', ['left', None]) -@pytest.mark.parametrize('base', [17, 24]) -def test_resampler_end_super_day(freq, closed, label, base, - datetime_index, cftime_index): - # Fairly extensive testing for standard/proleptic Gregorian calendar. - # For greater-than-day frequencies anchored at the end, the default values - # for closed and label are 'right'. loffset = '12H' try: da_datetime = da(datetime_index).resample( - time=freq, closed=closed, label=label, base=base, + time=resample_freq, closed=closed, label=label, base=base, loffset=loffset).mean() except ValueError: with pytest.raises(ValueError): da(cftime_index).resample( - time=freq, closed=closed, label=label, base=base, + time=resample_freq, closed=closed, label=label, base=base, loffset=loffset).mean() else: - da_cftime = da(cftime_index).resample(time=freq, closed=closed, - label=label, base=base, - loffset=loffset).mean() + da_cftime = da(cftime_index).resample( + time=resample_freq, closed=closed, + label=label, base=base, loffset=loffset).mean() da_cftime['time'] = da_cftime.indexes['time'].to_datetimeindex() xr.testing.assert_identical(da_cftime, da_datetime) @@ -111,6 +93,7 @@ def test_closed_label_defaults(freq, expected): assert CFTimeGrouper(freq=freq).label == expected +@pytest.mark.filterwarnings('ignore:Converting a CFTimeIndex') @pytest.mark.parametrize('calendar', ['gregorian', 'noleap', 'all_leap', '360_day', 'julian']) def test_calendars(calendar):