Skip to content

Commit

Permalink
Reduce length of cftime resample tests (#2879)
Browse files Browse the repository at this point in the history
* Reduce length of cftime resample tests

* Update per @jwenfai's suggestions; silence to_datetimeindex warnings

* Restore 91% coverage for resample_cftime.py
  • Loading branch information
spencerkclark authored Apr 11, 2019
1 parent 3fb22cb commit b9a920e
Showing 1 changed file with 52 additions and 69 deletions.
121 changes: 52 additions & 69 deletions xarray/tests/test_cftimeindex_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,97 +6,79 @@
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):
return xr.DataArray(np.arange(100., 100. + index.size),
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)

Expand All @@ -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):
Expand Down

0 comments on commit b9a920e

Please sign in to comment.