Skip to content
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

BUG: Issue with groupby agg with a single function and a a mixed-type frame (GH6337) #6338

Merged
merged 1 commit into from Feb 13, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/release.rst
Expand Up @@ -91,6 +91,7 @@ Bug Fixes
- ``HDFStore.select_as_multiple`` handles start and stop the same way as ``select`` (:issue:`6177`)
- ``HDFStore.select_as_coordinates`` and ``select_column`` works where clauses that result in filters (:issue:`6177`)
- Regression in join of non_unique_indexes (:issue:`6329`)
- Issue with groupby ``agg`` with a single function and a a mixed-type frame (:issue:`6337`)

pandas 0.13.1
-------------
Expand Down
9 changes: 9 additions & 0 deletions pandas/core/groupby.py
Expand Up @@ -2123,6 +2123,7 @@ def _aggregate_item_by_item(self, func, *args, **kwargs):
obj = self._obj_with_exclusions
result = {}
cannot_agg = []
errors=None
for item in obj:
try:
data = obj[item]
Expand All @@ -2133,11 +2134,19 @@ def _aggregate_item_by_item(self, func, *args, **kwargs):
except ValueError:
cannot_agg.append(item)
continue
except TypeError as e:
cannot_agg.append(item)
errors=e
continue

result_columns = obj.columns
if cannot_agg:
result_columns = result_columns.drop(cannot_agg)

# GH6337
if not len(result_columns) and errors is not None:
raise errors

return DataFrame(result, columns=result_columns)

def _decide_output_index(self, output, labels):
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/test_groupby.py
Expand Up @@ -370,6 +370,26 @@ def f(grp):
e.name = None
assert_series_equal(result,e)

def test_agg_api(self):

# GH 6337
# http://stackoverflow.com/questions/21706030/pandas-groupby-agg-function-column-dtype-error
# different api for agg when passed custom function with mixed frame

df = DataFrame({'data1':np.random.randn(5),
'data2':np.random.randn(5),
'key1':['a','a','b','b','a'],
'key2':['one','two','one','two','one']})
grouped = df.groupby('key1')

def peak_to_peak(arr):
return arr.max() - arr.min()

expected = grouped.agg([peak_to_peak])
expected.columns=['data1','data2']
result = grouped.agg(peak_to_peak)
assert_frame_equal(result,expected)

def test_agg_regression1(self):
grouped = self.tsframe.groupby([lambda x: x.year, lambda x: x.month])
result = grouped.agg(np.mean)
Expand Down