Skip to content

FIX filter selects selected columns #6593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2529,7 +2529,7 @@ def filter(self, func, dropna=True, *args, **kwargs):

indices = []

obj = self._obj_with_exclusions
obj = self._selected_obj
gen = self.grouper.get_iterator(obj, axis=self.axis)

fast_path, slow_path = self._define_paths(func, *args, **kwargs)
Expand Down
15 changes: 13 additions & 2 deletions pandas/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -3438,6 +3438,13 @@ def test_filter_and_transform_with_non_unique_string_index(self):
actual = grouped_df.pid.transform(len)
assert_series_equal(actual, expected)

def test_filter_has_access_to_grouped_cols(self):
df = DataFrame([[1, 2], [1, 3], [5, 6]], columns=['A', 'B'])
g = df.groupby('A')
# previously didn't have access to col A #????
filt = g.filter(lambda x: x['A'].sum() == 2)
assert_frame_equal(filt, df.iloc[[0, 1]])

def test_index_label_overlaps_location(self):
# checking we don't have any label/location confusion in the
# the wake of GH5375
Expand Down Expand Up @@ -3486,7 +3493,8 @@ def test_groupby_selection_with_methods(self):
'idxmin', 'idxmax',
'ffill', 'bfill',
'pct_change',
'tshift'
'tshift',
#'ohlc'
]

for m in methods:
Expand All @@ -3501,8 +3509,11 @@ def test_groupby_selection_with_methods(self):
g_exp.apply(lambda x: x.sum()))

assert_frame_equal(g.resample('D'), g_exp.resample('D'))
assert_frame_equal(g.resample('D', how='ohlc'),
g_exp.resample('D', how='ohlc'))


assert_frame_equal(g.filter(lambda x: len(x) == 3),
g_exp.filter(lambda x: len(x) == 3))

def test_groupby_whitelist(self):
from string import ascii_lowercase
Expand Down