@@ -166,18 +166,27 @@ def test_first_last_nth(self):
166
166
# tests for first / last / nth
167
167
grouped = self .df .groupby ('A' )
168
168
first = grouped .first ()
169
- expected = self .df .ix [[1 , 0 ], ['B' , 'C' , 'D' ]]
170
- expected .index = ['bar' , 'foo' ]
171
- assert_frame_equal (first , expected , check_names = False )
169
+ expected = self .df .ix [[1 , 0 ], ['B' ,'C' ,'D' ]]
170
+ expected .index = Index (['bar' , 'foo' ],name = 'A' )
171
+ expected = expected .sort_index ()
172
+ assert_frame_equal (first , expected )
173
+
174
+ nth = grouped .nth (0 )
175
+ assert_frame_equal (nth , expected )
172
176
173
177
last = grouped .last ()
174
- expected = self .df .ix [[5 , 7 ], ['B' , 'C' , 'D' ]]
175
- expected .index = ['bar' , 'foo' ]
176
- assert_frame_equal (last , expected , check_names = False )
178
+ expected = self .df .ix [[5 , 7 ], ['B' ,'C' ,'D' ]]
179
+ expected .index = Index (['bar' , 'foo' ],name = 'A' )
180
+ assert_frame_equal (last , expected )
181
+
182
+ nth = grouped .nth (- 1 )
183
+ assert_frame_equal (nth , expected )
177
184
178
185
nth = grouped .nth (1 )
179
- expected = self .df .iloc [[2 , 3 ]]
180
- assert_frame_equal (nth , expected , check_names = False )
186
+ expected = self .df .ix [[2 , 3 ],['B' ,'C' ,'D' ]].copy ()
187
+ expected .index = Index (['foo' , 'bar' ],name = 'A' )
188
+ expected = expected .sort_index ()
189
+ assert_frame_equal (nth , expected )
181
190
182
191
# it works!
183
192
grouped ['B' ].first ()
@@ -189,6 +198,17 @@ def test_first_last_nth(self):
189
198
self .assert_ (com .isnull (grouped ['B' ].last ()['foo' ]))
190
199
self .assert_ (com .isnull (grouped ['B' ].nth (0 )[0 ])) # not sure what this is testing
191
200
201
+ # v0.14.0 whatsnew
202
+ df = DataFrame ([[1 , np .nan ], [1 , 4 ], [5 , 6 ]], columns = ['A' , 'B' ])
203
+ g = df .groupby ('A' )
204
+ result = g .first ()
205
+ expected = df .iloc [[1 ,2 ]].set_index ('A' )
206
+ assert_frame_equal (result , expected )
207
+
208
+ expected = df .iloc [[1 ,2 ]].set_index ('A' )
209
+ result = g .nth (0 ,dropna = 'any' )
210
+ assert_frame_equal (result , expected )
211
+
192
212
def test_first_last_nth_dtypes (self ):
193
213
194
214
df = self .df_mixed_floats .copy ()
@@ -199,17 +219,21 @@ def test_first_last_nth_dtypes(self):
199
219
grouped = df .groupby ('A' )
200
220
first = grouped .first ()
201
221
expected = df .ix [[1 , 0 ], ['B' , 'C' , 'D' , 'E' , 'F' ]]
202
- expected .index = ['bar' , 'foo' ]
203
- assert_frame_equal (first , expected , check_names = False )
222
+ expected .index = Index (['bar' , 'foo' ], name = 'A' )
223
+ expected = expected .sort_index ()
224
+ assert_frame_equal (first , expected )
204
225
205
226
last = grouped .last ()
206
227
expected = df .ix [[5 , 7 ], ['B' , 'C' , 'D' , 'E' , 'F' ]]
207
- expected .index = ['bar' , 'foo' ]
208
- assert_frame_equal (last , expected , check_names = False )
228
+ expected .index = Index (['bar' , 'foo' ], name = 'A' )
229
+ expected = expected .sort_index ()
230
+ assert_frame_equal (last , expected )
209
231
210
232
nth = grouped .nth (1 )
211
- expected = df .iloc [[2 , 3 ]]
212
- assert_frame_equal (nth , expected , check_names = False )
233
+ expected = df .ix [[3 , 2 ],['B' , 'C' , 'D' , 'E' , 'F' ]]
234
+ expected .index = Index (['bar' , 'foo' ], name = 'A' )
235
+ expected = expected .sort_index ()
236
+ assert_frame_equal (nth , expected )
213
237
214
238
# GH 2763, first/last shifting dtypes
215
239
idx = lrange (10 )
@@ -223,15 +247,15 @@ def test_nth(self):
223
247
df = DataFrame ([[1 , np .nan ], [1 , 4 ], [5 , 6 ]], columns = ['A' , 'B' ])
224
248
g = df .groupby ('A' )
225
249
226
- assert_frame_equal (g .nth (0 ), df .iloc [[0 , 2 ]])
227
- assert_frame_equal (g .nth (1 ), df .iloc [[1 ]])
228
- assert_frame_equal (g .nth (2 ), df .loc [[]])
229
- assert_frame_equal (g .nth (- 1 ), df .iloc [[1 , 2 ]])
230
- assert_frame_equal (g .nth (- 2 ), df .iloc [[0 ]])
231
- assert_frame_equal (g .nth (- 3 ), df .loc [[]])
250
+ assert_frame_equal (g .nth (0 ), df .iloc [[0 , 2 ]]. set_index ( 'A' ) )
251
+ assert_frame_equal (g .nth (1 ), df .iloc [[1 ]]. set_index ( 'A' ) )
252
+ assert_frame_equal (g .nth (2 ), df .loc [[],[ 'B' ] ])
253
+ assert_frame_equal (g .nth (- 1 ), df .iloc [[1 , 2 ]]. set_index ( 'A' ) )
254
+ assert_frame_equal (g .nth (- 2 ), df .iloc [[0 ]]. set_index ( 'A' ) )
255
+ assert_frame_equal (g .nth (- 3 ), df .loc [[],[ 'B' ] ])
232
256
assert_series_equal (g .B .nth (0 ), df .B .iloc [[0 , 2 ]])
233
257
assert_series_equal (g .B .nth (1 ), df .B .iloc [[1 ]])
234
- assert_frame_equal (g [['B' ]].nth (0 ), df .ix [[0 , 2 ], ['B' ]])
258
+ assert_frame_equal (g [['B' ]].nth (0 ), df .ix [[0 , 2 ], ['A' , ' B' ]]. set_index ( 'A' ) )
235
259
236
260
exp = df .set_index ('A' )
237
261
assert_frame_equal (g .nth (0 , dropna = 'any' ), exp .iloc [[1 , 2 ]])
0 commit comments