@@ -94,21 +94,28 @@ def test_groupby_cumprod_nan_influences_other_columns():
94
94
95
95
def test_cummin (dtypes_for_minmax ):
96
96
dtype = dtypes_for_minmax [0 ]
97
- min_val = dtypes_for_minmax [1 ]
98
97
99
98
# GH 15048
100
99
base_df = DataFrame ({"A" : [1 , 1 , 1 , 1 , 2 , 2 , 2 , 2 ], "B" : [3 , 4 , 3 , 2 , 2 , 3 , 2 , 1 ]})
101
100
expected_mins = [3 , 3 , 3 , 2 , 2 , 2 , 2 , 1 ]
102
101
103
102
df = base_df .astype (dtype )
104
-
105
103
expected = DataFrame ({"B" : expected_mins }).astype (dtype )
106
104
result = df .groupby ("A" ).cummin ()
107
105
tm .assert_frame_equal (result , expected )
108
106
result = df .groupby ("A" , group_keys = False ).B .apply (lambda x : x .cummin ()).to_frame ()
109
107
tm .assert_frame_equal (result , expected )
110
108
111
- # Test w/ min value for dtype
109
+
110
+ def test_cummin_min_value_for_dtype (dtypes_for_minmax ):
111
+ dtype = dtypes_for_minmax [0 ]
112
+ min_val = dtypes_for_minmax [1 ]
113
+
114
+ # GH 15048
115
+ base_df = DataFrame ({"A" : [1 , 1 , 1 , 1 , 2 , 2 , 2 , 2 ], "B" : [3 , 4 , 3 , 2 , 2 , 3 , 2 , 1 ]})
116
+ expected_mins = [3 , 3 , 3 , 2 , 2 , 2 , 2 , 1 ]
117
+ expected = DataFrame ({"B" : expected_mins }).astype (dtype )
118
+ df = base_df .astype (dtype )
112
119
df .loc [[2 , 6 ], "B" ] = min_val
113
120
df .loc [[1 , 5 ], "B" ] = min_val + 1
114
121
expected .loc [[2 , 3 , 6 , 7 ], "B" ] = min_val
@@ -120,8 +127,10 @@ def test_cummin(dtypes_for_minmax):
120
127
)
121
128
tm .assert_frame_equal (result , expected , check_exact = True )
122
129
123
- # Test nan in some values
130
+
131
+ def test_cummin_nan_in_some_values (dtypes_for_minmax ):
124
132
# Explicit cast to float to avoid implicit cast when setting nan
133
+ base_df = DataFrame ({"A" : [1 , 1 , 1 , 1 , 2 , 2 , 2 , 2 ], "B" : [3 , 4 , 3 , 2 , 2 , 3 , 2 , 1 ]})
125
134
base_df = base_df .astype ({"B" : "float" })
126
135
base_df .loc [[0 , 2 , 4 , 6 ], "B" ] = np .nan
127
136
expected = DataFrame ({"B" : [np .nan , 4 , np .nan , 2 , np .nan , 3 , np .nan , 1 ]})
@@ -132,13 +141,17 @@ def test_cummin(dtypes_for_minmax):
132
141
)
133
142
tm .assert_frame_equal (result , expected )
134
143
144
+
145
+ def test_cummin_datetime ():
135
146
# GH 15561
136
147
df = DataFrame ({"a" : [1 ], "b" : pd .to_datetime (["2001" ])})
137
148
expected = Series (pd .to_datetime ("2001" ), index = [0 ], name = "b" )
138
149
139
150
result = df .groupby ("a" )["b" ].cummin ()
140
151
tm .assert_series_equal (expected , result )
141
152
153
+
154
+ def test_cummin_getattr_series ():
142
155
# GH 15635
143
156
df = DataFrame ({"a" : [1 , 2 , 1 ], "b" : [1 , 2 , 2 ]})
144
157
result = df .groupby ("a" ).b .cummin ()
@@ -163,7 +176,6 @@ def test_cummin_max_all_nan_column(method, dtype):
163
176
164
177
def test_cummax (dtypes_for_minmax ):
165
178
dtype = dtypes_for_minmax [0 ]
166
- max_val = dtypes_for_minmax [2 ]
167
179
168
180
# GH 15048
169
181
base_df = DataFrame ({"A" : [1 , 1 , 1 , 1 , 2 , 2 , 2 , 2 ], "B" : [3 , 4 , 3 , 2 , 2 , 3 , 2 , 1 ]})
@@ -177,8 +189,18 @@ def test_cummax(dtypes_for_minmax):
177
189
result = df .groupby ("A" , group_keys = False ).B .apply (lambda x : x .cummax ()).to_frame ()
178
190
tm .assert_frame_equal (result , expected )
179
191
180
- # Test w/ max value for dtype
192
+
193
+ def test_cummax_min_value_for_dtype (dtypes_for_minmax ):
194
+ dtype = dtypes_for_minmax [0 ]
195
+ max_val = dtypes_for_minmax [2 ]
196
+
197
+ # GH 15048
198
+ base_df = DataFrame ({"A" : [1 , 1 , 1 , 1 , 2 , 2 , 2 , 2 ], "B" : [3 , 4 , 3 , 2 , 2 , 3 , 2 , 1 ]})
199
+ expected_maxs = [3 , 4 , 4 , 4 , 2 , 3 , 3 , 3 ]
200
+
201
+ df = base_df .astype (dtype )
181
202
df .loc [[2 , 6 ], "B" ] = max_val
203
+ expected = DataFrame ({"B" : expected_maxs }).astype (dtype )
182
204
expected .loc [[2 , 3 , 6 , 7 ], "B" ] = max_val
183
205
result = df .groupby ("A" ).cummax ()
184
206
tm .assert_frame_equal (result , expected )
@@ -187,8 +209,11 @@ def test_cummax(dtypes_for_minmax):
187
209
)
188
210
tm .assert_frame_equal (result , expected )
189
211
212
+
213
+ def test_cummax_nan_in_some_values (dtypes_for_minmax ):
190
214
# Test nan in some values
191
215
# Explicit cast to float to avoid implicit cast when setting nan
216
+ base_df = DataFrame ({"A" : [1 , 1 , 1 , 1 , 2 , 2 , 2 , 2 ], "B" : [3 , 4 , 3 , 2 , 2 , 3 , 2 , 1 ]})
192
217
base_df = base_df .astype ({"B" : "float" })
193
218
base_df .loc [[0 , 2 , 4 , 6 ], "B" ] = np .nan
194
219
expected = DataFrame ({"B" : [np .nan , 4 , np .nan , 4 , np .nan , 3 , np .nan , 3 ]})
@@ -199,13 +224,17 @@ def test_cummax(dtypes_for_minmax):
199
224
)
200
225
tm .assert_frame_equal (result , expected )
201
226
227
+
228
+ def test_cummax_datetime ():
202
229
# GH 15561
203
230
df = DataFrame ({"a" : [1 ], "b" : pd .to_datetime (["2001" ])})
204
231
expected = Series (pd .to_datetime ("2001" ), index = [0 ], name = "b" )
205
232
206
233
result = df .groupby ("a" )["b" ].cummax ()
207
234
tm .assert_series_equal (expected , result )
208
235
236
+
237
+ def test_cummax_getattr_series ():
209
238
# GH 15635
210
239
df = DataFrame ({"a" : [1 , 2 , 1 ], "b" : [2 , 1 , 1 ]})
211
240
result = df .groupby ("a" ).b .cummax ()
@@ -292,15 +321,12 @@ def test_nullable_int_not_cast_as_float(method, dtype, val):
292
321
tm .assert_frame_equal (result , expected )
293
322
294
323
295
- def test_cython_api2 ():
324
+ def test_cython_api2 (as_index ):
296
325
# this takes the fast apply path
297
326
298
327
# cumsum (GH5614)
328
+ # GH 5755 - cumsum is a transformer and should ignore as_index
299
329
df = DataFrame ([[1 , 2 , np .nan ], [1 , np .nan , 9 ], [3 , 4 , 9 ]], columns = ["A" , "B" , "C" ])
300
330
expected = DataFrame ([[2 , np .nan ], [np .nan , 9 ], [4 , 9 ]], columns = ["B" , "C" ])
301
- result = df .groupby ("A" ).cumsum ()
302
- tm .assert_frame_equal (result , expected )
303
-
304
- # GH 5755 - cumsum is a transformer and should ignore as_index
305
- result = df .groupby ("A" , as_index = False ).cumsum ()
331
+ result = df .groupby ("A" , as_index = as_index ).cumsum ()
306
332
tm .assert_frame_equal (result , expected )
0 commit comments