20
20
}
21
21
22
22
23
- def _check_accum_op (name , series , check_dtype = True ):
24
- func = getattr (np , name )
25
- tm .assert_numpy_array_equal (
26
- func (series ).values , func (np .array (series )), check_dtype = check_dtype
27
- )
28
-
29
- # with missing values
30
- ts = series .copy ()
31
- ts [::2 ] = np .NaN
32
-
33
- result = func (ts )[1 ::2 ]
34
- expected = func (np .array (ts .dropna ()))
35
-
36
- tm .assert_numpy_array_equal (result .values , expected , check_dtype = False )
23
+ class TestSeriesCumulativeOps :
24
+ @pytest .mark .parametrize ("func" , [np .cumsum , np .cumprod ])
25
+ def test_datetime_series (self , datetime_series , func ):
26
+ tm .assert_numpy_array_equal (
27
+ func (datetime_series ).values ,
28
+ func (np .array (datetime_series )),
29
+ check_dtype = True ,
30
+ )
37
31
32
+ # with missing values
33
+ ts = datetime_series .copy ()
34
+ ts [::2 ] = np .NaN
38
35
39
- class TestSeriesCumulativeOps :
40
- def test_cumsum (self , datetime_series ):
41
- _check_accum_op ("cumsum" , datetime_series )
36
+ result = func (ts )[1 ::2 ]
37
+ expected = func (np .array (ts .dropna ()))
42
38
43
- def test_cumprod (self , datetime_series ):
44
- _check_accum_op ("cumprod" , datetime_series )
39
+ tm .assert_numpy_array_equal (result .values , expected , check_dtype = False )
45
40
46
41
@pytest .mark .parametrize ("method" , ["cummin" , "cummax" ])
47
42
def test_cummin_cummax (self , datetime_series , method ):
@@ -67,64 +62,70 @@ def test_cummin_cummax(self, datetime_series, method):
67
62
pd .Timestamp ("1999-12-31" ).tz_localize ("US/Pacific" ),
68
63
],
69
64
)
70
- def test_cummin_cummax_datetimelike (self , ts ):
65
+ @pytest .mark .parametrize (
66
+ "method, skipna, exp_tdi" ,
67
+ [
68
+ ["cummax" , True , ["NaT" , "2 days" , "NaT" , "2 days" , "NaT" , "3 days" ]],
69
+ ["cummin" , True , ["NaT" , "2 days" , "NaT" , "1 days" , "NaT" , "1 days" ]],
70
+ [
71
+ "cummax" ,
72
+ False ,
73
+ ["NaT" , "2 days" , "2 days" , "2 days" , "2 days" , "3 days" ],
74
+ ],
75
+ [
76
+ "cummin" ,
77
+ False ,
78
+ ["NaT" , "2 days" , "2 days" , "1 days" , "1 days" , "1 days" ],
79
+ ],
80
+ ],
81
+ )
82
+ def test_cummin_cummax_datetimelike (self , ts , method , skipna , exp_tdi ):
71
83
# with ts==pd.Timedelta(0), we are testing td64; with naive Timestamp
72
84
# we are testing datetime64[ns]; with Timestamp[US/Pacific]
73
85
# we are testing dt64tz
74
86
tdi = pd .to_timedelta (["NaT" , "2 days" , "NaT" , "1 days" , "NaT" , "3 days" ])
75
87
ser = pd .Series (tdi + ts )
76
88
77
- exp_tdi = pd .to_timedelta (["NaT" , "2 days" , "NaT" , "2 days" , "NaT" , "3 days" ])
78
- expected = pd .Series (exp_tdi + ts )
79
- result = ser .cummax (skipna = True )
80
- tm .assert_series_equal (expected , result )
81
-
82
- exp_tdi = pd .to_timedelta (["NaT" , "2 days" , "NaT" , "1 days" , "NaT" , "1 days" ])
83
- expected = pd .Series (exp_tdi + ts )
84
- result = ser .cummin (skipna = True )
85
- tm .assert_series_equal (expected , result )
86
-
87
- exp_tdi = pd .to_timedelta (
88
- ["NaT" , "2 days" , "2 days" , "2 days" , "2 days" , "3 days" ]
89
- )
89
+ exp_tdi = pd .to_timedelta (exp_tdi )
90
90
expected = pd .Series (exp_tdi + ts )
91
- result = ser . cummax (skipna = False )
91
+ result = getattr ( ser , method ) (skipna = skipna )
92
92
tm .assert_series_equal (expected , result )
93
93
94
- exp_tdi = pd .to_timedelta (
95
- ["NaT" , "2 days" , "2 days" , "1 days" , "1 days" , "1 days" ]
96
- )
97
- expected = pd .Series (exp_tdi + ts )
98
- result = ser .cummin (skipna = False )
99
- tm .assert_series_equal (expected , result )
100
-
101
- def test_cummethods_bool (self ):
94
+ @pytest .mark .parametrize (
95
+ "arg" ,
96
+ [
97
+ [False , False , False , True , True , False , False ],
98
+ [False , False , False , False , False , False , False ],
99
+ ],
100
+ )
101
+ @pytest .mark .parametrize (
102
+ "func" , [lambda x : x , lambda x : ~ x ], ids = ["identity" , "inverse" ]
103
+ )
104
+ @pytest .mark .parametrize ("method" , methods .keys ())
105
+ def test_cummethods_bool (self , arg , func , method ):
102
106
# GH#6270
103
107
# checking Series method vs the ufunc applied to the values
104
108
105
- a = pd .Series ([False , False , False , True , True , False , False ])
106
- c = pd .Series ([False ] * len (a ))
107
-
108
- for method in methods :
109
- for ser in [a , ~ a , c , ~ c ]:
110
- ufunc = methods [method ]
111
-
112
- exp_vals = ufunc (ser .values )
113
- expected = pd .Series (exp_vals )
109
+ ser = func (pd .Series (arg ))
110
+ ufunc = methods [method ]
114
111
115
- result = getattr (ser , method )()
112
+ exp_vals = ufunc (ser .values )
113
+ expected = pd .Series (exp_vals )
116
114
117
- tm . assert_series_equal ( result , expected )
115
+ result = getattr ( ser , method )( )
118
116
119
- def test_cummethods_bool_in_object_dtype ( self ):
117
+ tm . assert_series_equal ( result , expected )
120
118
119
+ @pytest .mark .parametrize (
120
+ "method, expected" ,
121
+ [
122
+ ["cumsum" , pd .Series ([0 , 1 , np .nan , 1 ], dtype = object )],
123
+ ["cumprod" , pd .Series ([False , 0 , np .nan , 0 ])],
124
+ ["cummin" , pd .Series ([False , False , np .nan , False ])],
125
+ ["cummax" , pd .Series ([False , True , np .nan , True ])],
126
+ ],
127
+ )
128
+ def test_cummethods_bool_in_object_dtype (self , method , expected ):
121
129
ser = pd .Series ([False , True , np .nan , False ])
122
- cse = pd .Series ([0 , 1 , np .nan , 1 ], dtype = object )
123
- cpe = pd .Series ([False , 0 , np .nan , 0 ])
124
- cmin = pd .Series ([False , False , np .nan , False ])
125
- cmax = pd .Series ([False , True , np .nan , True ])
126
- expecteds = {"cumsum" : cse , "cumprod" : cpe , "cummin" : cmin , "cummax" : cmax }
127
-
128
- for method in methods :
129
- res = getattr (ser , method )()
130
- tm .assert_series_equal (res , expecteds [method ])
130
+ result = getattr (ser , method )()
131
+ tm .assert_series_equal (result , expected )
0 commit comments