@@ -494,7 +494,6 @@ def test_groupby_dict_mapping(self):
494
494
assert_series_equal (result , expected2 )
495
495
496
496
def test_groupby_bounds_check (self ):
497
- import pandas as pd
498
497
# groupby_X is code-generated, so if one variant
499
498
# does, the rest probably do to
500
499
a = np .array ([1 ,2 ],dtype = 'object' )
@@ -3979,7 +3978,6 @@ def test_groupby_datetime64_32_bit(self):
3979
3978
assert_series_equal (result ,expected )
3980
3979
3981
3980
def test_groupby_categorical_unequal_len (self ):
3982
- import pandas as pd
3983
3981
#GH3011
3984
3982
series = Series ([np .nan , np .nan , 1 , 1 , 2 , 2 , 3 , 3 , 4 , 4 ])
3985
3983
# The raises only happens with categorical, not with series of types category
@@ -4037,7 +4035,6 @@ def noddy(value, weight):
4037
4035
no_toes = df_grouped .apply (lambda x : noddy (x .value , x .weight ))
4038
4036
4039
4037
def test_groupby_with_empty (self ):
4040
- import pandas as pd
4041
4038
index = pd .DatetimeIndex (())
4042
4039
data = ()
4043
4040
series = pd .Series (data , index )
@@ -4376,7 +4373,6 @@ def test_cumcount_groupby_not_col(self):
4376
4373
assert_series_equal (expected , sg .cumcount ())
4377
4374
4378
4375
def test_filter_series (self ):
4379
- import pandas as pd
4380
4376
s = pd .Series ([1 , 3 , 20 , 5 , 22 , 24 , 7 ])
4381
4377
expected_odd = pd .Series ([1 , 3 , 5 , 7 ], index = [0 , 1 , 3 , 6 ])
4382
4378
expected_even = pd .Series ([20 , 22 , 24 ], index = [2 , 4 , 5 ])
@@ -4395,7 +4391,6 @@ def test_filter_series(self):
4395
4391
expected_even .reindex (s .index ))
4396
4392
4397
4393
def test_filter_single_column_df (self ):
4398
- import pandas as pd
4399
4394
df = pd .DataFrame ([1 , 3 , 20 , 5 , 22 , 24 , 7 ])
4400
4395
expected_odd = pd .DataFrame ([1 , 3 , 5 , 7 ], index = [0 , 1 , 3 , 6 ])
4401
4396
expected_even = pd .DataFrame ([20 , 22 , 24 ], index = [2 , 4 , 5 ])
@@ -4414,7 +4409,6 @@ def test_filter_single_column_df(self):
4414
4409
expected_even .reindex (df .index ))
4415
4410
4416
4411
def test_filter_multi_column_df (self ):
4417
- import pandas as pd
4418
4412
df = pd .DataFrame ({'A' : [1 , 12 , 12 , 1 ], 'B' : [1 , 1 , 1 , 1 ]})
4419
4413
grouper = df ['A' ].apply (lambda x : x % 2 )
4420
4414
grouped = df .groupby (grouper )
@@ -4423,7 +4417,6 @@ def test_filter_multi_column_df(self):
4423
4417
grouped .filter (lambda x : x ['A' ].sum () - x ['B' ].sum () > 10 ), expected )
4424
4418
4425
4419
def test_filter_mixed_df (self ):
4426
- import pandas as pd
4427
4420
df = pd .DataFrame ({'A' : [1 , 12 , 12 , 1 ], 'B' : 'a b c d' .split ()})
4428
4421
grouper = df ['A' ].apply (lambda x : x % 2 )
4429
4422
grouped = df .groupby (grouper )
@@ -4433,7 +4426,6 @@ def test_filter_mixed_df(self):
4433
4426
grouped .filter (lambda x : x ['A' ].sum () > 10 ), expected )
4434
4427
4435
4428
def test_filter_out_all_groups (self ):
4436
- import pandas as pd
4437
4429
s = pd .Series ([1 , 3 , 20 , 5 , 22 , 24 , 7 ])
4438
4430
grouper = s .apply (lambda x : x % 2 )
4439
4431
grouped = s .groupby (grouper )
@@ -4446,7 +4438,6 @@ def test_filter_out_all_groups(self):
4446
4438
grouped .filter (lambda x : x ['A' ].sum () > 1000 ), df .ix [[]])
4447
4439
4448
4440
def test_filter_out_no_groups (self ):
4449
- import pandas as pd
4450
4441
s = pd .Series ([1 , 3 , 20 , 5 , 22 , 24 , 7 ])
4451
4442
grouper = s .apply (lambda x : x % 2 )
4452
4443
grouped = s .groupby (grouper )
@@ -4459,7 +4450,6 @@ def test_filter_out_no_groups(self):
4459
4450
assert_frame_equal (filtered , df )
4460
4451
4461
4452
def test_filter_condition_raises (self ):
4462
- import pandas as pd
4463
4453
def raise_if_sum_is_zero (x ):
4464
4454
if x .sum () == 0 :
4465
4455
raise ValueError
@@ -4471,6 +4461,14 @@ def raise_if_sum_is_zero(x):
4471
4461
self .assertRaises (TypeError ,
4472
4462
lambda : grouped .filter (raise_if_sum_is_zero ))
4473
4463
4464
+ def test_filter_with_axis_in_groupby (self ):
4465
+ # issue 11041
4466
+ index = pd .MultiIndex .from_product ([range (10 ), [0 , 1 ]])
4467
+ data = pd .DataFrame (np .arange (100 ).reshape (- 1 , 20 ), columns = index , dtype = 'int64' )
4468
+ result = data .groupby (level = 0 , axis = 1 ).filter (lambda x : x .iloc [0 , 0 ] > 10 )
4469
+ expected = data .iloc [:,12 :20 ]
4470
+ assert_frame_equal (result , expected )
4471
+
4474
4472
def test_filter_bad_shapes (self ):
4475
4473
df = DataFrame ({'A' : np .arange (8 ), 'B' : list ('aabbbbcc' ), 'C' : np .arange (8 )})
4476
4474
s = df ['B' ]
0 commit comments