15
15
from pandas .core .series import Series
16
16
from pandas .util .testing import (assert_panel_equal , assert_frame_equal ,
17
17
assert_series_equal , assert_almost_equal ,
18
- assert_index_equal )
18
+ assert_index_equal , assertRaisesRegexp )
19
19
from pandas .compat import (
20
20
range , long , lrange , StringIO , lmap , lzip , map ,
21
21
zip , builtins , OrderedDict
33
33
import pandas as pd
34
34
from numpy .testing import assert_equal
35
35
36
+
36
37
def commonSetUp (self ):
37
38
self .dateRange = bdate_range ('1/1/2005' , periods = 250 )
38
39
self .stringIndex = Index ([rands (8 ).upper () for x in range (250 )])
@@ -75,7 +76,8 @@ def setUp(self):
75
76
'B' : ['one' , 'one' , 'two' , 'three' ,
76
77
'two' , 'two' , 'one' , 'three' ],
77
78
'C' : np .random .randn (8 ),
78
- 'D' : np .array (np .random .randn (8 ),dtype = 'float32' )})
79
+ 'D' : np .array (np .random .randn (8 ),
80
+ dtype = 'float32' )})
79
81
80
82
index = MultiIndex (levels = [['foo' , 'bar' , 'baz' , 'qux' ],
81
83
['one' , 'two' , 'three' ]],
@@ -117,7 +119,7 @@ def checkit(dtype):
117
119
118
120
assert_series_equal (agged , grouped .agg (np .mean )) # shorthand
119
121
assert_series_equal (agged , grouped .mean ())
120
- assert_series_equal (grouped .agg (np .sum ),grouped .sum ())
122
+ assert_series_equal (grouped .agg (np .sum ), grouped .sum ())
121
123
122
124
transformed = grouped .transform (lambda x : x * x .sum ())
123
125
self .assertEqual (transformed [7 ], 12 )
@@ -141,10 +143,20 @@ def checkit(dtype):
141
143
# corner cases
142
144
self .assertRaises (Exception , grouped .aggregate , lambda x : x * 2 )
143
145
144
-
145
- for dtype in ['int64' ,'int32' ,'float64' ,'float32' ]:
146
+ for dtype in ['int64' , 'int32' , 'float64' , 'float32' ]:
146
147
checkit (dtype )
147
148
149
+ def test_select_bad_cols (self ):
150
+ df = DataFrame ([[1 , 2 ]], columns = ['A' , 'B' ])
151
+ g = df .groupby ('A' )
152
+ self .assertRaises (KeyError , g .__getitem__ , ['C' ]) # g[['C']]
153
+
154
+ self .assertRaises (KeyError , g .__getitem__ , ['A' , 'C' ]) # g[['A', 'C']]
155
+ with assertRaisesRegexp (KeyError , '^[^A]+$' ):
156
+ # A should not be referenced as a bad column...
157
+ # will have to rethink regex if you change message!
158
+ g [['A' , 'C' ]]
159
+
148
160
def test_first_last_nth (self ):
149
161
# tests for first / last / nth
150
162
grouped = self .df .groupby ('A' )
0 commit comments