6
6
import xarray as xr
7
7
from xarray .core .resample_cftime import CFTimeGrouper
8
8
9
+
9
10
pytest .importorskip ('cftime' )
10
11
pytest .importorskip ('pandas' , minversion = '0.24' )
11
12
12
13
13
- @pytest .fixture (
14
- params = [
15
- dict (start = '2004-01-01T12:07:01' , periods = 91 , freq = '3D' ),
16
- dict (start = '1892-01-03T12:07:01' , periods = 15 , freq = '41987T' ),
17
- dict (start = '2004-01-01T12:07:01' , periods = 7 , freq = '3Q-AUG' ),
18
- dict (start = '1892-01-03T12:07:01' , periods = 10 , freq = '3AS-JUN' )
19
- ],
20
- ids = ['3D' , '41987T' , '3Q_AUG' , '3AS_JUN' ]
21
- )
22
- def time_range_kwargs (request ):
23
- return request .param
24
-
25
-
26
- @pytest .fixture ()
27
- def datetime_index (time_range_kwargs ):
28
- return pd .date_range (** time_range_kwargs )
29
-
30
-
31
- @pytest .fixture ()
32
- def cftime_index (time_range_kwargs ):
33
- return xr .cftime_range (** time_range_kwargs )
14
+ # Create a list of pairs of similar-length initial and resample frequencies
15
+ # that cover:
16
+ # - Resampling from shorter to longer frequencies
17
+ # - Resampling from longer to shorter frequencies
18
+ # - Resampling from one initial frequency to another.
19
+ # These are used to test the cftime version of resample against pandas
20
+ # with a standard calendar.
21
+ FREQS = [
22
+ ('8003D' , '4001D' ),
23
+ ('8003D' , '16006D' ),
24
+ ('8003D' , '21AS' ),
25
+ ('6H' , '3H' ),
26
+ ('6H' , '12H' ),
27
+ ('6H' , '400T' ),
28
+ ('3D' , 'D' ),
29
+ ('3D' , '6D' ),
30
+ ('11D' , 'MS' ),
31
+ ('3MS' , 'MS' ),
32
+ ('3MS' , '6MS' ),
33
+ ('3MS' , '85D' ),
34
+ ('7M' , '3M' ),
35
+ ('7M' , '14M' ),
36
+ ('7M' , '2QS-APR' ),
37
+ ('43QS-AUG' , '21QS-AUG' ),
38
+ ('43QS-AUG' , '86QS-AUG' ),
39
+ ('43QS-AUG' , '11A-JUN' ),
40
+ ('11Q-JUN' , '5Q-JUN' ),
41
+ ('11Q-JUN' , '22Q-JUN' ),
42
+ ('11Q-JUN' , '51MS' ),
43
+ ('3AS-MAR' , 'AS-MAR' ),
44
+ ('3AS-MAR' , '6AS-MAR' ),
45
+ ('3AS-MAR' , '14Q-FEB' ),
46
+ ('7A-MAY' , '3A-MAY' ),
47
+ ('7A-MAY' , '14A-MAY' ),
48
+ ('7A-MAY' , '85M' )
49
+ ]
34
50
35
51
36
52
def da (index ):
37
53
return xr .DataArray (np .arange (100. , 100. + index .size ),
38
54
coords = [index ], dims = ['time' ])
39
55
40
56
41
- @pytest .mark .parametrize ('freq' , [
42
- '700T' , '8001T' ,
43
- '12H' , '8001H' ,
44
- '8D' , '8001D' ,
45
- '2MS' , '3MS' ,
46
- '2QS-AUG' , '3QS-SEP' ,
47
- '3AS-MAR' , '4A-MAY' ])
48
- @pytest .mark .parametrize ('closed' , [None , 'right' ])
49
- @pytest .mark .parametrize ('label' , [None , 'right' ])
57
+ @pytest .mark .parametrize ('freqs' , FREQS , ids = lambda x : '{}->{}' .format (* x ))
58
+ @pytest .mark .parametrize ('closed' , [None , 'left' , 'right' ])
59
+ @pytest .mark .parametrize ('label' , [None , 'left' , 'right' ])
50
60
@pytest .mark .parametrize ('base' , [24 , 31 ])
51
- def test_resampler (freq , closed , label , base ,
52
- datetime_index , cftime_index ):
53
- # Fairly extensive testing for standard/proleptic Gregorian calendar
54
- # For any frequencies which are not greater-than-day and anchored
55
- # at the end, the default values for closed and label are 'left'.
56
- loffset = '12H'
57
- try :
58
- da_datetime = da (datetime_index ).resample (
59
- time = freq , closed = closed , label = label , base = base ,
60
- loffset = loffset ).mean ()
61
- except ValueError :
62
- with pytest .raises (ValueError ):
63
- da (cftime_index ).resample (
64
- time = freq , closed = closed , label = label , base = base ,
65
- loffset = loffset ).mean ()
66
- else :
67
- da_cftime = da (cftime_index ).resample (time = freq , closed = closed ,
68
- label = label , base = base ,
69
- loffset = loffset ).mean ()
70
- da_cftime ['time' ] = da_cftime .indexes ['time' ].to_datetimeindex ()
71
- xr .testing .assert_identical (da_cftime , da_datetime )
72
-
61
+ def test_resample (freqs , closed , label , base ):
62
+ initial_freq , resample_freq = freqs
63
+ start = '2000-01-01T12:07:01'
64
+ index_kwargs = dict (start = start , periods = 5 , freq = initial_freq )
65
+ datetime_index = pd .date_range (** index_kwargs )
66
+ cftime_index = xr .cftime_range (** index_kwargs )
73
67
74
- @pytest .mark .parametrize ('freq' , [
75
- '2M' , '3M' ,
76
- '2Q-JUN' , '3Q-JUL' ,
77
- '3A-FEB' , '4A-APR' ])
78
- @pytest .mark .parametrize ('closed' , ['left' , None ])
79
- @pytest .mark .parametrize ('label' , ['left' , None ])
80
- @pytest .mark .parametrize ('base' , [17 , 24 ])
81
- def test_resampler_end_super_day (freq , closed , label , base ,
82
- datetime_index , cftime_index ):
83
- # Fairly extensive testing for standard/proleptic Gregorian calendar.
84
- # For greater-than-day frequencies anchored at the end, the default values
85
- # for closed and label are 'right'.
86
68
loffset = '12H'
87
69
try :
88
70
da_datetime = da (datetime_index ).resample (
89
- time = freq , closed = closed , label = label , base = base ,
71
+ time = resample_freq , closed = closed , label = label , base = base ,
90
72
loffset = loffset ).mean ()
91
73
except ValueError :
92
74
with pytest .raises (ValueError ):
93
75
da (cftime_index ).resample (
94
- time = freq , closed = closed , label = label , base = base ,
76
+ time = resample_freq , closed = closed , label = label , base = base ,
95
77
loffset = loffset ).mean ()
96
78
else :
97
- da_cftime = da (cftime_index ).resample (time = freq , closed = closed ,
98
- label = label , base = base ,
99
- loffset = loffset ).mean ()
79
+ da_cftime = da (cftime_index ).resample (
80
+ time = resample_freq , closed = closed ,
81
+ label = label , base = base , loffset = loffset ).mean ()
100
82
da_cftime ['time' ] = da_cftime .indexes ['time' ].to_datetimeindex ()
101
83
xr .testing .assert_identical (da_cftime , da_datetime )
102
84
@@ -111,6 +93,7 @@ def test_closed_label_defaults(freq, expected):
111
93
assert CFTimeGrouper (freq = freq ).label == expected
112
94
113
95
96
+ @pytest .mark .filterwarnings ('ignore:Converting a CFTimeIndex' )
114
97
@pytest .mark .parametrize ('calendar' , ['gregorian' , 'noleap' , 'all_leap' ,
115
98
'360_day' , 'julian' ])
116
99
def test_calendars (calendar ):
0 commit comments