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