17
17
18
18
import pandas .util .testing as tm
19
19
20
- # unittest.TestCase
21
-
22
20
def commonSetUp (self ):
23
21
self .dateRange = DateRange ('1/1/2005' , periods = 250 , offset = dt .bday )
24
22
self .stringIndex = Index ([rands (8 ).upper () for x in xrange (250 )])
@@ -36,19 +34,6 @@ def commonSetUp(self):
36
34
self .timeMatrix = DataFrame (randMat , columns = self .columnIndex ,
37
35
index = self .dateRange )
38
36
39
-
40
- class GroupByTestCase (unittest .TestCase ):
41
- setUp = commonSetUp
42
-
43
- def test_python_grouper (self ):
44
- groupFunc = self .groupDict .get
45
- groups = groupby (self .stringIndex , groupFunc )
46
- setDict = dict ((k , set (v )) for k , v in groups .iteritems ())
47
- for idx in self .stringIndex :
48
- key = groupFunc (idx )
49
- groupSet = setDict [key ]
50
- assert (idx in groupSet )
51
-
52
37
class TestGroupBy (unittest .TestCase ):
53
38
54
39
def setUp (self ):
@@ -122,6 +107,33 @@ def test_series_agg_corner(self):
122
107
result = self .ts .groupby (self .ts * np .nan ).sum ()
123
108
assert_series_equal (result , Series ([]))
124
109
110
+ def test_len (self ):
111
+ df = tm .makeTimeDataFrame ()
112
+ grouped = df .groupby ([lambda x : x .year ,
113
+ lambda x : x .month ,
114
+ lambda x : x .day ])
115
+ self .assertEquals (len (grouped ), len (df ))
116
+
117
+ grouped = df .groupby ([lambda x : x .year ,
118
+ lambda x : x .month ])
119
+ expected = len (set ([(x .year , x .month ) for x in df .index ]))
120
+ self .assertEquals (len (grouped ), expected )
121
+
122
+ def test_groups (self ):
123
+ grouped = self .df .groupby (['A' ])
124
+ groups = grouped .groups
125
+ self .assert_ (groups is grouped .groups ) # caching works
126
+
127
+ for k , v in grouped .groups .iteritems ():
128
+ self .assert_ ((self .df .ix [v ]['A' ] == k ).all ())
129
+
130
+ grouped = self .df .groupby (['A' , 'B' ])
131
+ groups = grouped .groups
132
+ self .assert_ (groups is grouped .groups ) # caching works
133
+ for k , v in grouped .groups .iteritems ():
134
+ self .assert_ ((self .df .ix [v ]['A' ] == k [0 ]).all ())
135
+ self .assert_ ((self .df .ix [v ]['B' ] == k [1 ]).all ())
136
+
125
137
def test_aggregate_str_func (self ):
126
138
def _check_results (grouped ):
127
139
# single series
@@ -221,10 +233,21 @@ def test_attr_wrapper(self):
221
233
self .assertRaises (AttributeError , getattr , grouped , 'foo' )
222
234
223
235
def test_series_describe_multikey (self ):
224
- raise nose .SkipTest
225
236
ts = tm .makeTimeSeries ()
226
237
grouped = ts .groupby ([lambda x : x .year , lambda x : x .month ])
227
- grouped .describe ()
238
+ result = grouped .describe ()
239
+ assert_series_equal (result ['mean' ], grouped .mean ())
240
+ assert_series_equal (result ['std' ], grouped .std ())
241
+ assert_series_equal (result ['min' ], grouped .min ())
242
+
243
+ def test_frame_describe_multikey (self ):
244
+ grouped = self .tsframe .groupby ([lambda x : x .year ,
245
+ lambda x : x .month ])
246
+ result = grouped .describe ()
247
+
248
+ for col , ts in self .tsframe .iteritems ():
249
+ expected = grouped [col ].describe ()
250
+ assert_series_equal (result ['A' ].unstack (), expected )
228
251
229
252
def test_frame_groupby (self ):
230
253
grouped = self .tsframe .groupby (lambda x : x .weekday ())
0 commit comments